For the code listed below, it runs fine except for the first SQL query. I'm pulling address and state information from the workbook, and running a query on the information to find the count of how many times the address appears in the table. If I run the code and stop it before the query is sent to Access, I can pull the query command from the Immediate window, go to Access, and run the query no problem. However, if I just run the VBA program and have it send the query to Access, I keep getting 0 for the result. So long story short, the query will run in Access and provide the correct result, but when Excel VBA sends the query to Access, I keep getting zero for the result (and no error messages). Any help would be greatly appreciated.
Dim DatabaseFileName As String, connectionstring As String
connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & DatabaseFileName & "; Persist Security Info=False;"
Dim conn As New ADODB.Connection
conn.Open connectionstring
Dim rs As New ADODB.Recordset, SQL As String
Dim ExecSQL As New ADODB.Command
With ThisWorkbook.Sheets(1)
For I = 2 To 1235
SQL = ""
If .Cells(I, 7) <> "" Then
SQL = "SELECT Count(VRSC_CUSTOMER_SITES.SITE_ID) AS GCOUNT into [GVRCount1] "
SQL = SQL & "FROM (VRSC_CUSTOMER_SITES) "
SQL = SQL & "WHERE ((VRSC_CUSTOMER_SITES.SITE_STREET Like " & Chr(34) & .Cells(I, 7) & Chr(34) & ") AND ((VRSC_CUSTOMER_SITES.SITE_ST)="
SQL = SQL & Chr(34) & .Cells(I, 5) & Chr(34) & ") AND ((VRSC_CUSTOMER_SITES.SITE_PHONE) Not Like ""999*""));"
rs.Open SQL, conn
SQL = "SELECT * FROM [GVRCount1]"
rs.Open SQL
.Cells(I, 8).CopyFromRecordset rs
End If
Next
End With
With ThisWorkbook.Sheets(2)
.Range("A1").CopyFromRecordset rs
End With
conn.Close
End Sub
SELECT * FROM [VRSC_CUSTOMER_SITES$]first to see if it's the SQL or your connection. If that works, I would comment out and test your more complex SQL statement line by line un-commenting it (with modifications to make it still work). I don't query against MS-Access ever, but when I write my SQL queries to go against an Excel workbook (used as a database), I have my tables in SQL written as:[Table$]with the "$" at the end. Double-check?