View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Brandon[_2_] Brandon[_2_] is offline
external usenet poster
 
Posts: 8
Default SQL query in Excel 2000 using VBA



Bob,

Thanks again for your help. I tried using the example you posted in my
query to no avail. I am still unable to get the query to use a variable
to read from a cell in a worksheet.

When I add breakpoints to verify individual steps, I can see that the
cell value is being assigned to the variables as needed; however, I am
still not getting the variable into the SQL query properly.

When I remove all variables from the query and replace them with a date
stamp in yyyy-mm-dd hh:mm:ss format, the data is returned as needed.

Here is the complete block of code for this query/process. Could you
please help me once more in getting this to work?

Thanks again,

Brandon

Workbooks("MyBook.xls").Activate
Dim dtTest As Date
Dim dtTest2 As Date
Dim sSQL As String
Worksheets("MySheet").Select
dtest = Workbooks("MyBook.xls").Worksheets("MySheet").Rang e("A3").Value
dtest2 =
Workbooks("MyBook.xls").Worksheets("MySheet").Rang e("A3").Value + 1
Windows("MyBook.xls").Activate
With ActiveSheet.QueryTables.Add(Connection:= _
"ODBC;DSN=SERVER;SRVR=SERVER;DB=DB;UID=LOGIN;PWD=P ASSWORD", _
Destination:=Range("A1"))
.CommandText = Array( _
"SELECT tbl_common_ticket.vendor_code,
tbl_common_ticket.reported_on" & Chr(13) & "" & Chr(10) & "FROM
DB.dbo.tbl_common_ticket tbl_common_ticket" & Chr(13) & "" & Chr(10) &
"WHERE (tbl_common_ticket.reported_on={ts '" & dtest & "'} And
tbl_common" _
, _
"_ticket.reported_on<={ts '" & dtest2 & "'})" & Chr(13) & "" &
Chr(10) & "ORDER BY tbl_common_ticket.vendor_code,
tbl_common_ticket.reported_on" _
)
.Name = "Reported"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlOverwriteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!