chaostheory
Registered User.
- Local time
- Today, 14:37
- Joined
- Sep 30, 2008
- Messages
- 69
I have a spreadsheet, a ever gorwing complex spreadsheet. Anyhow, i have a query in access that is pulling data from a table. The database is returning results. But the query when i put it into excel, is simply not returning anything. I have it messageboxing me the sql query im passing to access and it looks right to me, but i just get the hourglass, the column headers, and no data. Here is what i got.
The only difference is the Where statement in access looks like
Screenshot attached of SQL statement being passed to access.
The only difference is the Where statement in access looks like
Code:
WHERE (((dbo_View_TK_TicketInfo.LotNumber) Like "*80525*"))
and the one in my sql statement in excel looks like
"WHERE (((dbo_View_TK_TicketInfo.LotNumber) Like " & lotNum & ")) "
and lotNum = '*80525*'
Code:
Sub lotRetrieval()
Dim varConn As String
Dim varSQL As String
Dim lotNum As String
Dim count As Integer
Dim numLots As Integer
Dim p As Integer, q As String, x As Integer
Dim totalRows As Integer
Dim colHeader As String, colHeader2 As String
Application.ScreenUpdating = False
Range("a2:K30000").ClearContents
numLots = Application.WorksheetFunction.CountA(Range("O:O"))
For x = 2 To numLots
count = Application.WorksheetFunction.CountA(Range("A:A"))
lotNum = Range("O" & x)
[COLOR=seagreen] 'shows up as 80525 which is correct[/COLOR]
lotNum = "'*" & lotNum & "*'"
[COLOR=seagreen] 'changes it to '*805258*' which is what i need for sql right since i have to use the regular quotes at the beginning of the line...[/COLOR]
varConn = "ODBC;DBQ=Z:\_AC_LBP\PPAP\2009 PPAP\Database\DataWarehouse.mdb;Driver={Driver do Microsoft Access (*.mdb)} "
varSQL = "SELECT dbo_View_TK_TicketInfo.PIN, dbo_View_TK_TicketInfo.LoadDttm, dbo_View_TK_TicketInfo.Green_Ticket_Number, dbo_View_TK_TicketInfo.Product_Code, dbo_View_TK_TicketInfo.Pieces, dbo_View_TK_TicketInfo.Car, dbo_View_TK_TicketInfo.Fired_Ticket_Number, dbo_View_TK_TicketInfo.LotNumber, dbo_View_TK_TicketInfo.UnloadDttm, dbo_View_TK_TicketInfo.UnloadQty, dbo_View_TK_TicketInfo.FinQty " & _
"FROM dbo_View_TK_TicketInfo " & _
"WHERE (((dbo_View_TK_TicketInfo.LotNumber) Like " & lotNum & ")) " & _
"ORDER BY dbo_View_TK_TicketInfo.PIN;"
MsgBox (varSQL)
With ActiveSheet.QueryTables.Add(Connection:=varConn, Destination:=Range("A" & count + 1))
.CommandText = varSQL
.Name = "Query-39008"
.FieldNames = False
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
Next x
Application.ScreenUpdating = True
End Sub
Attachments
Last edited: