• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

Passing parameters to SQL - MS Access

Here is my code :

Dim cmdPubs As ADODB.Command
Set cmdPubs = New ADODB.Command
Set cmdPubs.ActiveConnection = DtEnviromentFly.FlyConnection

  With cmdPubs
        .CommandType = adCmdStoredProc
        .CommandText = "{Call SQL_Acesso (?)}"
        .Parameters(0).Value = Popup.cmbName.Text
        .Parameters(0).Direction = adParamInput
End With

The code is returning a invalid SQL statement errror, when it executes .Parameters(0)..........

Did I forget something ? How could I pass parameters to a SQL in MS Access ?
  • 2
1 Solution
Try this:
    cmdpubs.CommandText = "select * from ParamQry"
    Dim par As New ADODB.Parameter
    Set par = cmdpubs.CreateParameter("param", adInteger)
    cmdpubs.Parameters.Append par
    par.Value = Popup.cmbName.Text
   Set rs = cmdpubs.Execute
There is a document in the ADO help that covers this.  There are two ways to get there:

While in VB's IDE, and in the "With" clause of your code, enter:


and then place your cursor over Append and press F1.  This should bring up the ADO help file on the "Append Method" page.  Click on the "example" link and you should be taken to the "Append and CreateParameter Methods Example".

If that doesn't work, open C:\<OS>\Ado210.chm and look in:

Microsoft ActiveX Data Objects (ADO)
  Microsoft ADO Programmer's Reference
    Learning ADO | ADO Code Examples
      ADO Method Examples

This "book" contains the "Append and CreateParameter Methods Example" document.

This page explains how to handle parameters and avoid this error.

Good luck!
Oh, I should've clarified that "<OS>" in the file path represents either "Windows" or "WinNT" depending on your system.  Plus, the path should have been:


Sorry for the confusion.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now