CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2009
    Posts
    10

    Help on Run - time error 91

    Hi guys. I just wanted to ask you help in this tricky little thingy. I'm trying to create a log in action which takes the username and password from an access .mdb file and verifies it with the ones entered in a text box. but somehow I seem to be getting a Run - time error 91 (Object variable or With block variable not set)

    The code is as below and the problematic line of coding has been highlighted.
    Code:
    Private Sub login_btn_Click()
    Set rs = db.OpenRecordset("Select emp_username from employee where emp_username = '" & username_txt.Text & "'")
    If Not rs.RecordCount = 0 Then
        Set rs = db.OpenRecordset("Select emp_pass from employee where emp_pass = '" & password_txt.Text & "'")
        If Not rs.RecordCount = 0 Then
            login_frame.Visible = False 'hide login frame
            welcome_frame.Visible = True 'unhide welcome frame
            btn_home.Visible = False 'unhide buttons
            btn_rent_car.Visible = False
            btn_customer.Visible = False
            btn_car.Visible = False
            btn_account.Visible = False
            btn_employee.Visible = False
            
            
        Else
            MsgBox "Password incorrect"
            password_txt.SetFocus
        End If
    Else
        MsgBox "Username incorrect"
        username_txt.SetFocus
    End If
    
    End Sub
    I have added a reference to the ADO and also created a module which goes like this
    Code:
    Public rs As DAO.Recordset
    Public db As DAO.Database
    
    Public Sub Main()
    Set db = OpenDatabase(App.Path + "/EasyRent.mdb")
    main_form.Show
    End Sub
    I'm still doing my diploma so any help at all would be greatly appreciated. Thanking you guys i advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Help on Run - time error 91

    Take a look at this article, and the attached sample. It shows how to open a connection and query a table.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Help on Run - time error 91

    Erm... you were saying, you are referring to the ADO, but making use od DAO objects?

  4. #4
    Join Date
    Aug 2009
    Posts
    10

    Re: Help on Run - time error 91

    Thanks to dglienna. it really is a very informative post. But by doing that I would have to change the entire structure of my coding. So I' keeping that as a last resort. Thank you very, very much. at least I have a plan B

    To WoF sorry. I was kinda confused DAO with ADO. I put a reference for DAO actually. Truly sorry for confusing you. Do you know why I'm having this problem? Any help would be greatly appreciated

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Help on Run - time error 91

    Depends on where you expect to be able to install and run it. Might not have old references available for x64 Windows 7 / Windows Server 2008 R2
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Aug 2009
    Posts
    10

    Re: Help on Run - time error 91

    Sorry dglienna. But could you kinda like explain that. I'm trully sorry for bothering you with this trivial question. But I'm using an x64 Windows 7. Do you think that's the reason why I'm getting this error during run time?

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Help on Run - time error 91

    Well, my code runs on x64, if that's what you mean. Might now have DAO either, as that is old
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    May 2009
    Location
    London
    Posts
    51

    Re: Help on Run - time error 91

    Looks like the problem is that the database object referred to by 'db' hasn't been opened. The fact it is coming up with error 91 means that db has been defined as a database but either hasn't been opened or has been closed.

    Normally with access DAO you would use someting like...

    Code:
    Set db = OpenDatabase("c:\dbname.mdb")
    Also I would point out that if you are using DAO to link to an Access database then try to avoid using the RecordCount property as this can be inaccurate if there are deleted records in the table.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured