How do I use OpenRecordset?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mackao
    New Member
    • May 2011
    • 1

    How do I use OpenRecordset?

    Hello experts.

    I'm rather green in the use of VB6 but it seems to be rather fun to make my own .exe files instead of just making MSAccess databases.

    Now to my question:
    In MSAccess I offen use HLookup and for what I can tell I can't use it in VB6
    But I saw in this forum that i can use OpenRecordset to simulate HLookup an get the same result.

    When I run this code I get a stange message:
    Syntax error(missing operator) in query expression 'strUserName= John Smith'.

    My code looks like this:

    Code:
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Set db = DAO.DBEngine(0).OpenDatabase("C:\Users\Marcus\Marcus jobb\Rekvisitioner vb6\Rekvisitioner-97.mdb")
    Set rs = db.OpenRecordset( _
    "Select [strPassword] From tblUsers Where strUserName = txtUser")
    The data I want is from a table called tblUsers, the column is strUserName, and I'm using a listbox called txtUser as a reference.

    Thank you for your help
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    try this

    Code:
    Set rs = db.OpenRecordset( _
    "Select [strPassword] From tblUsers Where strUserName = " & txtUser )

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Don't forget to put quotes around the value, since it's a string.
      Code:
      Set rs = db.OpenRecordset( _
        "Select strPassword From tblUsers Where strUserName = '" & _
         txtUser & "'")
      Note, since apostrophes (') are used as the string delimiters, this simple code is likely to have problems should you encounter a user name which contains an apostrophe. But that's another story.

      Comment

      Working...