Results 1 to 9 of 9

VBA Code IF/Else statment Compile Error

  1. #1
    JoeJr is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2019
    Posts
    40

    VBA Code IF/Else statment Compile Error


    Hey everyone,

    I am getting a compile Error "Else without If" when I run my login screen. I am stumped as to why. I dont know were I would be missing an "If". I believe it is throwing the error on the very last Else statment that I have. What is supposed to happen is the very first If statment runs. if the user name and or password are blank then it skips all the missile and goes to the last else statement. Could you please just look over it it and make sure I have it correct.

    Thank you.

    Here is the code:

    Code:
    Option Compare Database
    Option Explicit
    
    
    Private Sub btnLogin_Click()
    Dim rs As Recordset
    
    
    Me.lblWrongUN.Visible = False
    Me.lblWrongPas.Visible = False
    
    
    If Not IsNull(Me.TxtUserName) And Not IsNull(Me.TxtPassword) Then
        
        Set rs = CurrentDb.OpenRecordset("SELECT * FROM tblUserPass WHERE UserName='" & Me.TxtUserName & "'", dbOpenSnapshot, dbReadOnly)
        If rs.EOF Then
            Me.lblWrongUN.Visible = True
            Me.TxtUserName.SetFocus
        Else
        If rs!Password <> PE(Me.TxtPassword) Then
            Me.lblWrongPas.Visible = True
            Me.TxtPassword.SetFocus
        Else
           DoCmd.OpenForm "AllTrackMainPage"
           DoCmd.Close acForm, Me.Name
        End If
        
            TempVars("UserType") = rs!UserType_ID.Value
        
        If rs!UserType_ID = 1 Then
            Dim prop As Property
            On Error GoTo SetProperty
            Set prop = CurrentDb.CreateProperty("AllowByPassKey", dbBoolean, False)
        End If
        
        CurrentDb.Properties.Append prop
        
    SetProperty:
        If MsgBox("Would you like to turn on the bypass Key?", vbYesNo, "Allow Bypass") = vbYes Then
            CurrentDb.Properties("AllowBypassKey") = True
        Else
            CurrentDb.Properties("AllowBypassKey") = False
        End If
       
    Else
        MsgBox "Must enter " & IIf(IsNull(Me.TxtUserName), "Username", "Password")
    
    
    End If
    
    
    End Sub

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    6,303
    The way I see it, you have 2 Else statements that are not separated by an IF or End If. Use indentation that defines each block and you will probably spot it.
    This one
    Else
    If rs!Password

    is followed by

    Else
    MsgBox "Must enter

    Code:
    If Not IsNull(Me.TxtUserName) And Not IsNull(Me.TxtPassword) Then
        
      Set rs = CurrentDb.OpenRecordset("SELECT * FROM tblUserPass WHERE UserName='" & Me.TxtUserName & "'", dbOpenSnapshot, dbReadOnly)
      If rs.EOF Then
        Me.lblWrongUN.Visible = True
        Me.TxtUserName.SetFocus
      Else
        If rs!Password <> PE(Me.TxtPassword) Then
          Me.lblWrongPas.Visible = True
          Me.TxtPassword.SetFocus
       Else
          DoCmd.OpenForm "AllTrackMainPage"
          DoCmd.Close acForm, Me.Name
       End If
        
        TempVars("UserType") = rs!UserType_ID.Value
        
        If rs!UserType_ID = 1 Then
          Dim prop As Property
          On Error GoTo SetProperty
          Set prop = CurrentDb.CreateProperty("AllowByPassKey", dbBoolean, False)
        End If
        
        CurrentDb.Properties.Append prop
        
      SetProperty:
        If MsgBox("Would you like to turn on the bypass Key?", vbYesNo, "Allow Bypass") = vbYes Then
          CurrentDb.Properties("AllowBypassKey") = True
        Else
          CurrentDb.Properties("AllowBypassKey") = False
        End If
       
    Else
        MsgBox "Must enter " & IIf(IsNull(Me.TxtUserName), "Username", "Password")
    
    End If
    Last edited by Micron; 10-17-2019 at 10:20 PM. Reason: added code

  3. #3
    JoeJr is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2019
    Posts
    40
    Quote Originally Posted by Micron View Post
    The way I see it, you have 2 Else statements that are not separated by an IF or End If. Use indentation that defines each block and you will probably spot it.
    This one
    Else
    If rs!Password

    is followed by

    Else
    MsgBox "Must enter

    Code:
    If Not IsNull(Me.TxtUserName) And Not IsNull(Me.TxtPassword) Then
        
      Set rs = CurrentDb.OpenRecordset("SELECT * FROM tblUserPass WHERE UserName='" & Me.TxtUserName & "'", dbOpenSnapshot, dbReadOnly)
      If rs.EOF Then
        Me.lblWrongUN.Visible = True
        Me.TxtUserName.SetFocus
      Else
        If rs!Password <> PE(Me.TxtPassword) Then
          Me.lblWrongPas.Visible = True
          Me.TxtPassword.SetFocus
       Else
          DoCmd.OpenForm "AllTrackMainPage"
          DoCmd.Close acForm, Me.Name
       End If
        
        TempVars("UserType") = rs!UserType_ID.Value
        
        If rs!UserType_ID = 1 Then
          Dim prop As Property
          On Error GoTo SetProperty
          Set prop = CurrentDb.CreateProperty("AllowByPassKey", dbBoolean, False)
        End If
        
        CurrentDb.Properties.Append prop
        
      SetProperty:
        If MsgBox("Would you like to turn on the bypass Key?", vbYesNo, "Allow Bypass") = vbYes Then
          CurrentDb.Properties("AllowBypassKey") = True
        Else
          CurrentDb.Properties("AllowBypassKey") = False
        End If
       
    Else
        MsgBox "Must enter " & IIf(IsNull(Me.TxtUserName), "Username", "Password")
    
    End If
    Hey thank you for the reply. Are you saying that I need to get rid of one the else statements? Sorry I am still learning the VBA.

  4. #4
    Ajax is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    7,554
    not sure if it is the works as you require but this

    Code:
       If rs.EOF Then
        Me.lblWrongUN.Visible = True
        Me.TxtUserName.SetFocus
      Else
        If rs!Password <> PE(Me.TxtPassword) Then
          Me.lblWrongPas.Visible = True
          Me.TxtPassword.SetFocus
       Else
          DoCmd.OpenForm "AllTrackMainPage"
          DoCmd.Close acForm, Me.Name
       End If
    should be written

    Code:
      If rs.EOF Then
        Me.lblWrongUN.Visible = True
        Me.TxtUserName.SetFocus
      Elseif rs!Password <> PE(Me.TxtPassword) Then
          Me.lblWrongPas.Visible = True
          Me.TxtPassword.SetFocus
       Else
          DoCmd.OpenForm "AllTrackMainPage"
          DoCmd.Close acForm, Me.Name
       End If

  5. #5
    JoeJr is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2019
    Posts
    40
    Hey Thank you for the reply. It worked for me . What is the difference between "ifelse" and have "else" just above the "if" statement?

  6. #6
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    6,303
    Are you saying that I need to get rid of one the else statements?
    Not sure what you need to do as I wasn't sure I could follow the logic. Ajax is correct (good catch) and it may be that there is more than one thing that needs to be fixed. I was thinking last night that you might also move the password message box out of that nested scenario. You test if the password is provided and if so, do a bunch of stuff. If not, then the message can go after all of that as the inverse of it being true is obviously false - in which case you present the message. Make Ajax's fix and see what happens.

  7. #7
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    6,303
    maybe this will help with the syntax

    https://docs.microsoft.com/en-us/off...lse-statements

  8. #8
    Ajax is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    7,554
    What is the difference between "ifelse" and have "else" just above the "if" statement?
    one is the right way to do it, the other isn't - and it's ElseIf, not IfElese
    Last edited by Ajax; 10-19-2019 at 04:51 AM.

  9. #9
    JoeJr is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2019
    Posts
    40
    Thank you all for the help. #Mircon thanks for the read.

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

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2013, 09:17 AM
  2. Adding multiple SQL Statment to code
    By rlsublime in forum Programming
    Replies: 1
    Last Post: 06-20-2012, 01:22 PM
  3. Compile Error: Can't find out what's wrong with the code.
    By vickan240sx in forum Programming
    Replies: 4
    Last Post: 05-24-2012, 06:25 PM
  4. Compile error: code or function not defined
    By GeorgeBrown in forum Access
    Replies: 1
    Last Post: 09-19-2011, 10:25 AM
  5. Replies: 23
    Last Post: 03-26-2009, 06:50 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
  •  
Tech Forums: Microsoft Office Forums