troubleshooting Question

VBA Error "Invalid SQL Statement" on recordset.Open

Avatar of Chris
ChrisFlag for United States of America asked on
Microsoft AccessVisual Basic ClassicSQL
2 Comments1 Solution883 ViewsLast Modified:
Hello.  I am trying to pass a parameter to a parameterized query, and then save the resulting recordset to a CSV file.  I get the error message "Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'" when the code hits rst.Open cmd.

The name of the parameterized query is "Outstanding Online Donations."

Thank you for your help!

Dim cmd As ADODB.Command
    Dim rst As ADODB.Recordset
    Dim prm As ADODB.Parameter
    Dim tower As String
    tower = "ABC"
    Dim filePath As String
    filePath = "C:\myfile.csv"
    Set rst = New ADODB.Recordset
    rst.CursorLocation = adUseClient
    Set cmd = New Command
    cmd.ActiveConnection = CurrentProject.Connection
    cmd.CommandText = "Outstanding Online Donations"
    Set prm = cmd.CreateParameter
    prm.Type = adChar
    prm.Size = 25
    cmd.Parameters.Append prm
    cmd.Parameters(0).Value = tower
    rst.Open cmd
    rst.Save filePath, adPersistADTG
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros