Results 1 to 5 of 5
  1. #1
    jj1 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2014
    Posts
    128

    code not working


    I don't know what I am doing wrong when I login as user Y= 3 then it gives me error 2467. "the expression you entered refers to an object that is closed or does not exist."
    and take me to the code line highlighted below.
    I think it's confusing Y for 1, 2 and 3 as if I comment 1 and 2 code then 3 works fine and vice versa.
    Code:
    Private Sub Command10_Click()
    If IsNull(txtUsername) Then
            MsgBox "Invalid Username"
            Exit Sub
    End If
        If IsNull(txtPassword) Then
            MsgBox "Invalid Password"
            Exit Sub
    End If
    Dim X As Long
    X = Nz(DLookup("UserID", "UserT", "Username='" & txtUsername & "' AND Password='" & txtPassword & "'"))
    If X > 0 Then
     Dim Y As Long
        Y = Nz(DLookup("GroupID", "GroupXUserT", "UserID='" & X & "'"))
        
        'MsgBox "" & Y & ""
        'We have a valid user
        If Y = 1 Or 2 Then
            DoCmd.OpenForm "MainMenu"
            Forms!MainMenu!txtUserID = X
            LogIt X, "User Logon"
            Forms!MainMenu!txtUsername = txtUsername
            DoCmd.Close acForm, "LoginF"
        End If
    Else
        MsgBox "Invalid Logon"
    End If
    If Y = 3 Then
            DoCmd.OpenForm "MainMenu"
            Forms!MainMenu!txtUserID = X
            LogIt X, "User Logon"
            Forms!MainMenu!txtUsername = txtUsername        
    DoCmd.Close acForm, "LoginF"
            Forms!MainMenu!Command27.Visible = False
        End If

  2. #2
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

  3. #3
    Rawb is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    I'm only guessing here, but you're confusing Access by referencing a Form Control without using the Me! prefix. It's working before that line because before that point, you only have one Form Control with the name txtUserName. As soon as you open the MainMenu Form, however, Access gets confused (because there's a txtUsername control on both the current Form and the MainMenu Form now). When referencing Controls on a local Form, you should always use Me!ControlName instead of just ControlName.

    Code:
    Private Sub Command10_Click()
    If IsNull(Me!txtUsername) Then
            MsgBox "Invalid Username"
            Exit Sub
    End If
        If IsNull(Me!txtPassword) Then
            MsgBox "Invalid Password"
            Exit Sub
    End If
    Dim X As Long
    X = Nz(DLookup("UserID", "UserT", "Username='" & Me!txtUsername & "' AND Password='" & Me!txtPassword & "'"))
    If X > 0 Then
     Dim Y As Long
        Y = Nz(DLookup("GroupID", "GroupXUserT", "UserID='" & X & "'"))
        
        'MsgBox "" & Y & ""
        'We have a valid user
        If Y = 1 Or 2 Then
            DoCmd.OpenForm "MainMenu"
            Forms!MainMenu!txtUserID = X
            LogIt X, "User Logon"
            Forms!MainMenu!txtUsername = Me!txtUsername
            DoCmd.Close acForm, "LoginF"
        End If
    Else
        MsgBox "Invalid Logon"
    End If
    If Y = 3 Then
            DoCmd.OpenForm "MainMenu"
            Forms!MainMenu!txtUserID = X
            LogIt X, "User Logon"
            Forms!MainMenu!txtUsername = Me!txtUsername        
    DoCmd.Close acForm, "LoginF"
            Forms!MainMenu!Command27.Visible = False
        End If
    It's also possible that there's not a txtUsername Control on the MainMenu Form.

  4. #4
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Your problem may be here: If Y = 1 Or 2 Then

    You have the syntax wrong - it has to be : If Y = 1 Or Y = 2 Then


    The expression "1 or 2" evaluates to 3, i.e. non-zero, i.e. True, so "If Y = 1 or 2" always evaluates to True, causing the first If..endif block to be executed.

    But if Y = 3, the second If..Endif is executed AS WELL!

    fix the If and see if that works.

  5. #5
    jj1 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2014
    Posts
    128
    That's the answer. Thanks I got it

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Code not working
    By jj1 in forum Access
    Replies: 1
    Last Post: 12-22-2014, 01:38 PM
  2. VBA Code not working
    By data808 in forum Forms
    Replies: 31
    Last Post: 02-06-2014, 08:55 AM
  3. Replies: 2
    Last Post: 06-28-2013, 12:58 PM
  4. VBA Code not working how it Should
    By Juan4412 in forum Programming
    Replies: 7
    Last Post: 12-07-2010, 01:59 PM
  5. VB code not working
    By cwwaicw311 in forum Programming
    Replies: 17
    Last Post: 04-26-2010, 07:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums