Results 1 to 12 of 12
  1. #1
    derek7467 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Posts
    46

    If Else Statement Help

    Hey again! This forum is awesome.

    I have code I want to modify (found on this forum)

    Private Sub Form_Open(Cancel As Integer)
    On Error GoTo Err_Form_Open

    If User.AccessID = 1 Then
    Me.cmdUserSettings.Enabled = True
    Else
    Me.cmdUserSettings.Enabled = False Then
    End If

    Exit_Form_Open:
    Exit Sub
    Err_Form_Open:
    MsgBox Err.Description


    Me.Visible = True
    Resume Exit_Form_Open


    Basically, its a login form, that chooses whether to allow the user or not to view a portion of that form. Lets say my other control is called Command76, how would I throw that in the if statement?:

    If User.AccessID = 2 Then
    Me.Command76.Enabled = True
    Else
    Me.Command76.Enabled = False


    Private Sub Form_Open(Cancel As Integer)
    On Error GoTo Err_Form_Open

    If User.AccessID = 1 Then
    Me.cmdUserSettings.Enabled = True
    Else
    Me.cmdUserSettings.Enabled = False Then
    End If

    Exit_Form_Open:
    Exit Sub
    Err_Form_Open:
    MsgBox Err.Description
    Me.Visible = True
    Resume Exit_Form_Open


    End Sub

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    You can simply add the code block to your procedure, doing the evaluation all on its own. Another option would be to use a select case statement. I usually use Select Case when evaluating User ID values because there can be several options. Not sure what "User" is but, assuming the code you posted works....

    Select Case User.AccessID

    Case 1
    <Do This>
    Case 2
    <do That>
    Case Else
    <Special Error Handler>

    End Select

  3. #3
    derek7467 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Posts
    46
    Would this work?
    Private Sub Form_Open(Cancel As Integer)
    On Error GoTo Err_Form_Open
    Select Case User.AccessID
    Case1
    If User.AccessID = 1 Then
    Me.cmdUserSettings.Enabled = True
    Else
    Me.cmdUserSettings.Enabled = False
    Case1
    If User.AccessID = 1 Then
    Me.Command76.Enabled = True
    Else
    Me.Command76.Enabled = False
    End Select


    End If

    Exit_Form_Open:
    Exit Sub
    Err_Form_Open:
    MsgBox Err.Description
    Me.Visible = True
    Resume Exit_Form_Open

    End Sub

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Set default values for controls to Enabled = False

    Remove the If Then Else statement.

    Enable controls based on the user ID

    Case1
    Me.cmdUserSettings.Enabled = True
    Case 2
    Me.Command76.Enabled = True
    End Select

  5. #5
    derek7467 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Posts
    46
    would I be able to just add to my current code with the Select Case?

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Select case or If then.

    You can nest statements if you want to. I do not see a need here. Setting the controls' default values to False and then enabling them where applicable should suffice.

  7. #7
    derek7467 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Posts
    46
    I don't think I can do that, because its based off of an access level once a user logs in. So once im logged in the form looks at whether im an acess level 1 or 2 and then reads off the true/false values.

  8. #8
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    If then and select case are not that complicated. All you are doing is evaluating. If you want, you can provide your DB for analysis.

  9. #9
    Xipooo's Avatar
    Xipooo is offline Sr. Database Developer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Arizona
    Posts
    332
    Replace your If/End If code with this.
    Code:
    Me.cmdUserSettings.Enabled = False
    Me.Command76.Enabled = False
    Select Case User.AccessID
      Case is = 1
        Me.cmdUserSettings.Enabled = True
      Case is = 2
        Me.Command76.Enabled = True    
    End Select

  10. #10
    derek7467 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Posts
    46
    Xipoo, you DA MAN. Worked! Thank you!

    Ahhhh not working.

    Looks like only Case 1 works. Case 2 remains not clickable, unless I reverse its position as case 1

    Me.cmdUserSettings.Enabled = False
    Me.Command76.Enabled = False
    Select Case User.AccessID = 1
    Case Is = 1
    Me.Command76.Enabled = True
    Case Is = 2
    Me.cmdUserSettings.Enabled = True
    End Select

  11. #11
    Xipooo's Avatar
    Xipooo is offline Sr. Database Developer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Arizona
    Posts
    332
    Quote Originally Posted by derek7467 View Post
    Xipoo, you DA MAN. Worked! Thank you!

    Ahhhh not working.

    Looks like only Case 1 works. Case 2 remains not clickable, unless I reverse its position as case 1

    Me.cmdUserSettings.Enabled = False
    Me.Command76.Enabled = False
    Select Case User.AccessID = 1
    Case Is = 1
    Me.Command76.Enabled = True
    Case Is = 2
    Me.cmdUserSettings.Enabled = True
    End Select
    Your select case should not have "User.AccessID=1"

    You may need to adjust what I've done a bit, but the code I wrote works. The first thing I do is cause both buttons to be disabled by default. If that's not what you want, then you can remove the
    Me.cmdUserSettings.Enabled = False
    Me.Command76.Enabled = False

    The code then grabs the value of User.AccessID and compares it to the first case statement.
    If AccessID is equal to 1 then Me.cmdUserSettings.Enabled is set to true. If AccessID is equal to 2 then Me.Command76.Enabled is set to true.

    If you want both buttons to become available when the AccessID is equal to 2, then you have to put both
    Me.cmdUserSettings.Enabled = True and Me.Command76.Enabled = True in the code below Case is = 2.

  12. #12
    derek7467 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Posts
    46
    Fixed and working.

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

Similar Threads

  1. if statement in sql statement, query builder
    By 54.69.6d.20 in forum Access
    Replies: 4
    Last Post: 09-11-2012, 07:38 AM
  2. IIf statement
    By Rhubie in forum Access
    Replies: 14
    Last Post: 08-31-2012, 03:37 PM
  3. Replies: 1
    Last Post: 01-13-2012, 02:59 PM
  4. Replies: 7
    Last Post: 08-17-2011, 01:49 PM
  5. SQL like statement?
    By Cojack in forum Queries
    Replies: 4
    Last Post: 09-21-2010, 04:45 AM

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