mresann
Registered User.
- Local time
- Today, 02:41
- Joined
- Jan 11, 2005
- Messages
- 357
Just a small hint.
When a query is created in VBA code, the smallest errors or ommissions can cause much time to be lost in troubleshooting, particularly with syntax. My suggestion is to create a string variable to hold the SQL string so it can be tested independently of the actual process. Include a Debug.print statement directly after the SQL variable asignment to display the SQL string. During operation, the sql statement will print in the Debug or Immediate window (depending on Access version). You can usually find any syntax problems faster this way, whether it be VBA or SQL syntax.
Incidentally, It is good practice to create a string variable to hold a code generated string itself in the first place. Rather then
db.execute "SELECT Field1, Field 2 FROM Table1 WHERE Field1 = " & myData
Use
Dim strSQL As String
strSQL = "SELECT Field1, Field 2 FROM Table1 WHERE Field1 = " & myData
debug.print strSQL
db.execute strSQL
Now if your query generates an error, you can easily test the query in a query grid by copying the SQL statement from the Debug/Immediate winto into the SQL box of a new query.
When a query is created in VBA code, the smallest errors or ommissions can cause much time to be lost in troubleshooting, particularly with syntax. My suggestion is to create a string variable to hold the SQL string so it can be tested independently of the actual process. Include a Debug.print statement directly after the SQL variable asignment to display the SQL string. During operation, the sql statement will print in the Debug or Immediate window (depending on Access version). You can usually find any syntax problems faster this way, whether it be VBA or SQL syntax.
Incidentally, It is good practice to create a string variable to hold a code generated string itself in the first place. Rather then
db.execute "SELECT Field1, Field 2 FROM Table1 WHERE Field1 = " & myData
Use
Dim strSQL As String
strSQL = "SELECT Field1, Field 2 FROM Table1 WHERE Field1 = " & myData
debug.print strSQL
db.execute strSQL
Now if your query generates an error, you can easily test the query in a query grid by copying the SQL statement from the Debug/Immediate winto into the SQL box of a new query.