Access Query in VBA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Deepalis
    New Member
    • Aug 2007
    • 1

    Access Query in VBA

    Can we pass a variable in any access query?I am stuck in a VBA code where I am passing variable from excel and using Access Queries to extract Data from Linked Table.This query works in Access but when I plug the code in VBA it starts giving error ........Can anyone help with access queries in VBA
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Can you show us the code?

    Generally though, what causes people problems in this sort of situation is that people include the variable name in the query. You need to insert the variable value instead.

    For example, let's assume you have a textbox called txtSurname, into which you've typed the name SMITH. Sorry if I'm oversimplifying , but this does seem to be a common problem...
    [CODE=vb]
    ' Wrong...
    strSQL = "SELECT * FROM MyTable WHERE Surname = 'txtSurname.Tex t'"
    ' Resulting query...
    SELECT * FROM MyTable WHERE Surname = 'txtSurname.Tex t'
    ' In my opinion, you are unlikely to find anyone with that name.

    ' Right...
    strSQL = "SELECT * FROM MyTable WHERE Surname = '" & txtSurname.Text & "'"
    ' Resulting query...
    SELECT * FROM MyTable WHERE Surname = 'SMITH'[/CODE]

    Comment

    Working...