Problem to make query in Excel from SQL

Ashk49

New Member
Joined
May 27, 2008
Messages
31
Hi Guys,
I have a problem to create query in excel when try to get Data from SQL server.
First-I wrote a code to connect to ODBC with DSN=MS Access database.
code is here:
Code:
 With ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
       "ODBC;DSN=MS Access Database;DBQ=" & fn & ";DefaultDir=;D" _
        ), Array("riverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;")), _
        Destination:=Range("A1"))
        .CommandText = Array( _
        "SELECT PL.LineID, R.RunID, R.ProductID, M.MaterialName, PL.UnitsPerMin, RLDR.UnitWt_SET, LR.ActualUnits, LR.ActualWt, LR.DateOpened, LR.DateClosed," _
        , _
        "PL.ProductID, LDSL.DTReasonID, R.ProductID,L.LineName, LDSL.DateOpened, LDSL.DateClosed" & Chr(13) & "" & Chr(10) & "FROM  LineDeviceStateLog LDSL, LineRun LR, Material M, Product P,Pr" _
        , _
        "oductLine PL, RunLineDeviceRecipe RLDR ,Line L ,Run R " & Chr(13) & "" & Chr(10) & "WHERE M.Material" _
        , _
        "ID = P.MaterialID AND PL.ProductID = R.ProductID AND RLDR.LineID = LDSL.LineID AND RLDR.RunID = R.RunID AND  PL.LineID = LR.LineID AND " _
        , _
        "LR.LineID = L.LineID AND R.RunID = LR.RunID AND P.ProductID = R.ProductID  AND RLDR.DeviceID = LDSL.DeviceID AND LR.LineID = LDSL.LineID " _
         , _
        "And (LDSL.DateOpened<{ts '" & End_Of_Day & "'}) And (LDSL.DateClosed>{ts '" & Begin_Of_Day & "'}) And (LR.DateClosed>LDSL.DateOpened) And " _
        , _
        "(LR.DateOpened<LDSL.DateClosed) And ((LR.DateOpened>={ts '" & Begin_Of_Day & "'} And LR.DateOpened<{ts '" & End_Of_Day & "'}))" & Chr(13) & "" & Chr(10) & "ORDER BY L" _
        , _
        ".LineID, R.ProductID, R.RunID" _
        )

This code works fine ,Here is my problem:
Try to connect with this connection :
Code:
 With ActiveSheet.QueryTables.Add(Connection:= _
        "ODBC;DRIVER=SQL Server;SERVER=ASHKAN;UID=;APP=Microsoft Office 2003;WSID=ASHKAN;DATABASE=Ogden_SQL_DB;Trusted_Connection=Yes" _
        , Destination:=Range("A1"))
and same query ,but I can not get the data .!!!....If i delete the filtering part it will give me some data !!!!:banghead:

Please help me !? How can I make it ?
Thanks & Regards
Ashkan
 
Last edited:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,225,375
Messages
6,184,611
Members
453,247
Latest member
scouterjames

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top