Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2005
    Posts
    31

    Exclamation Unanswered: Converting IF-Then to Select-Case. Can you help?

    Hello. I usually know how to work a simple select case statement however I can't figure this one out. Now this code works fine until I try to add yet another IF Statement for the Manager field. MS Access doesn't return an error message, it simply ignores the manager statement completely??? I hope you can help.

    Private Sub cmdSaveAndClose_Click()

    If IsNull(Me.Manager) Or Me.Manager = "" Then
    MsgBox "You must Select a Manager.", vbOKOnly, " Required Data"
    Me.Manager.SetFocus
    Exit Sub


    Else

    If IsNull(Me.Supervisor) Or Me.Supervisor = "" Then
    MsgBox "You must Select a Supervisor.", vbOKOnly, " Required Data"
    Me.Supervisor.SetFocus
    Exit Sub

    Else

    If IsNull(Me.StatusActive) Or Me.StatusActive = "" Then
    MsgBox "You must Define Agent's Status.", vbOKOnly, " Required Data"
    Me.StatusActive.SetFocus
    Exit Sub

    Else

    If IsNull(Me.AgentID) Or Me.AgentID = "" Then
    MsgBox "You must Enter Agent's Employee ID.", vbOKOnly, " Required Data"
    Me.AgentID.SetFocus
    Exit Sub

    Else

    If IsNull(Me.FirstName) Or Me.FirstName = "" Then
    MsgBox "You must Enter Agent's First Name.", vbOKOnly, " Required Data"
    Me.FirstName.SetFocus
    Exit Sub

    Else

    If IsNull(Me.LastName) Or Me.LastName = "" Then
    MsgBox "You must Enter Agent's Last Name.", vbOKOnly, " Required Data"
    Me.LastName.SetFocus
    Exit Sub

    Else

    If IsNull(Me.Extension) Or Me.Extension = "" Then
    MsgBox "You must Enter Agent's Extension.", vbOKOnly, " Required Data"
    Me.Extension.SetFocus
    Exit Sub

    Else

    If IsNull(Me.IEXID) Or Me.IEXID = "" Then
    MsgBox "You must Enter Agent's IEX ID.", vbOKOnly, " Required Data"
    Me.IEXID.SetFocus
    Exit Sub

    Else

    If IsNull(Me.HireDate) Or Me.HireDate = "" Then
    MsgBox "You must Enter Agent's Hire Date.", vbOKOnly, " Required Data"
    Me.HireDate.SetFocus
    Exit Sub

    End If
    End If
    End If
    End If
    End If
    End If
    End If
    End If

    DoCmd.Close

    Forms!SupervisorView!AgentListbox.SetFocus
    Forms!SupervisorView!AgentListbox.Requery

    End Sub

  2. #2
    Join Date
    Mar 2003
    Location
    The Bottom of The Barrel
    Posts
    6,102
    Provided Answers: 1
    Your first issue is you're not using elseif.

    Basically you're saying:

    Code:
    if somestuff Then
       some other stuff
    Else
       if some stuff Then
          some other stuff
       else
          if some stuff Then
             some other stuff
          End If
       End If
    End If
    etc etc.

    What you want is:
    Code:
    if somestuff Then
       some other stuff
    elseif somestuff Then
       some other stuff
    elseif somestuff Then
       some other stuff
    End If
    Also, have you tried debugging right after the manager statement to see what value it holds? Perhaps it does pass the if statement when you expect it to fail.
    Last edited by Teddy; 02-12-05 at 23:13.
    oh yeah... documentation... I have heard of that.

    *** What Do You Want In The MS Access Forum? ***

  3. #3
    Join Date
    Jan 2005
    Posts
    68
    rather than writing a long set of if then statements or select case you might try cycling through the controls on your form. this code will work, but you may have to tweak a few control names to make sure that they will make sense in the error message.

    Private Sub cmd_Click()
    Dim ctl As Control
    For Each ctl In Me.Controls
    'if you have combo boxes as well add Or acComboBox to the
    'line below
    If ctl.ControlType = acTextBox Then
    If IsNull(ctl) Or Len(ctl) = 0 Then
    GoTo ErrorMessage
    End If
    End If
    Next
    ExitHere:
    Exit Sub
    ErrorMessage:
    'to use this sub you may have to rename a few controls
    'to make sure that their names will work in the error message
    MsgBox "You must enter a value for " & _
    ctl.name, vbExclamation + vbOKOnly, " Required Data"
    ctl.SetFocus
    End Sub

    hope this helps
    Last edited by Frosty1; 02-13-05 at 06:56.

Posting Permissions

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