Problem with If/Then/Else code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rleepac
    New Member
    • Nov 2009
    • 22

    #1

    Problem with If/Then/Else code

    I have a form with a tabbed control on it. I am trying to set certain criteria for some of the tabs to show only when info from other fields meet criteria.

    Specifically, I have two fields. Both are combo boxes (using tables as the row source).

    What I want is that when empType combo box is set to Prehire (8 in the table) AND the empPosition combo box is set to Police Officer then the Police_MSK tab is visible. But if both of those conditions aren't met then I don't want that tab showing.

    What might complicate this is that for all other Prehires (non Police Officer's) I have a different tab that I want to be visible called Prehire_MSK.

    Here is what my code looks like:

    Code:
    Private Sub empType_AfterUpdate()
     
     If Me.empType = "8" Then
            Me.Prehire_MSK.Visible = True
        Else
            Me.Prehire_MSK.Visible = False
        End If
     
     If Me.empType = "8" And Me.empPosition = "Police Officer" Then
            Me.Police_MSK.Visible = True
        Else
            Me.Police_MSK.Visible = False
        End If
     
    End Sub
    So after updating the empType field to Prehire I get the Prehire_MSK tab to show up and when I change that field to any other type the Prehire_MSK tab goes away like it's supposed to.

    But it doesn't seem to be looking at the empPosition field and I can't get the Police_MSK tab to show up when it is a Prehire Police Officer. Can someone help me and tell me what's wrong with my code?

    Am I better off using a Select Case instead of the If/Then/Else?

    I hope I explained well enough what my problem is...

    Thanks!
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    I think your problem is in your logic. Try this instead ....

    Code:
    Private Sub empType_AfterUpdate()
     
        If Me.empType = "8" And Me.empPosition = "Police Officer" Then
            Me.Police_MSK.Visible = True
            Me.Prehire_MSK.Visible = False
        ElseIf Me.empType = "8" Then
            Me.Police_MSK.Visible = False
            Me.Prehire_MSK.Visible = True
        Else
            Me.Police_MSK.Visible = False
            Me.Prehire_MSK.Visible = False
        End If
     
    End Sub

    Comment

    • rleepac
      New Member
      • Nov 2009
      • 22

      #3
      Thank you that is much "cleaner" logic.

      I figured out my problem...I was using Police Officer where I should have been using the ID number from the table that is was using as a row source.

      Problem solved!

      Thanks again...

      Comment

      Working...