SQL in VBA

G

Guest

Can anyone tell me what is wrong with the following code. Instead of having a
cmd button call a query. I am trying to hard code the SQL directly into the
cmd procedure

Here is the "onclick procedure"

Private Sub Command42_Click()
DoCmd.RunSQL "SELECT CorplinkData.[Acct#] " & _
"FROM CorplinkData " & _
"WHERE CorplinkData.[Acct#] = 'EP*';"
End Sub

It gives me the following error "A RunSQL action requires an argument
consisting of an SQL statement"

I have tried this as well....only to have the same error:

Private Sub Command42_Click()
Dim stSQL as String

strSQL = "SELECT CorplinkData.[Acct#] " & _
"FROM CorplinkData " & _
"WHERE CorplinkData.[Acct#] = 'EP*';"

Docmd.RunSQL (strSQL)
End Sub
 
M

Michel Walsh

Hi,


You cannot "run" a SELECT query. You can run an action query, such as
DELETE, INSERT, CREATE, but not SELECT.

You have to "put" the returned records, from a SELECT, somewhere. As
example, you can assign the RowSource of a list box:



Me.ListBoxName.RowSource = "SELECT ... "



Hoping it may help,
Vanderghast, Access MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top