Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2012
    Posts
    22

    Unanswered: IF statement to check if all the boxes are filled

    Please help with the IF statement to check if all the boxes are filled, if not then throw "Please complete all fields" when I click on "add record button"
    here is how my code looks and the picture is attached:
    Private Sub btnSaveCommand_Click()
    Me.AppointmentDate.SetFocus
    If Me.AppointmentDate.Text = "" Then
    MsgBox "Please complete all fields"
    ElseIf Me.cboClient.Value Then
    MsgBox "Please complete all fields"
    ElseIf Me.cboSalon.Value Then
    MsgBox "Please complete all fields"
    ElseIf Me.cboSalon.Value Then
    MsgBox "Please complete all fields"
    ElseIf Me.cboStaff.Value Then
    MsgBox "Please complete all fields"
    ElseIf Me.cboService.Value Then
    MsgBox "Please complete all fields"
    ElseIf Me.ProductID.Value Then
    MsgBox "Please complete all fields"
    End If
    Else
    DoCmd.GoToRecord , , acNewRec
    MsgBox "Thanks - Appointment Booked!"

    End Sub
    Attached Thumbnails Attached Thumbnails ifstatement.JPG  

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    your users are going to love that approach, the same message for 7 different error states

    there's lots of ways of handling this
    one is to flip the back colour of each control which is incorrect, works fine providing all users have colour screens, there's no requirement for colour sensistive awareness.

    another is to spot there is a problem and report each line with a fault

    Code:
    dim strErrorMessage as string 'error message to be returned to the user
    strErrorMessage = ""
    Me.AppointmentDate.SetFocus
    If Me.AppointmentDate.Text = "" Then strErrorMessage = "No Appointment date defined" & vbcrlf
    If Me.cboClient.Value Then  strErrorMessage = strErrorMessage & "No Client defined" & vbcrlf
    If Me.cboSalon.Value Then  strErrorMessage = strErrorMessage & "No Salon defined" & vbcrlf
    If Me.cboStaff.Value Then   strErrorMessage = strErrorMessage & "No Satff member defined" & vbcrlf
    If Me.cboService.Value Then  strErrorMessage = strErrorMessage & "No Service defined" & vbcrlf
    If Me.ProductID.Value Then  strErrorMessage = strErrorMessage & "No Client defined" & vbcrlf
    End If
    if strlen(  strErrorMessage) > 0 then 'we have detected errors
      strErrorMessage = strErrorMessage & vbcrlf & "Cannot save appointement untill fixed"
      msgbox(strErrorMessage,"Errors detected", vbvwarning)
    else
      DoCmd.GoToRecord , , acNewRec
      MsgBox "Thanks - Appointment Booked!"
    endif
    vbcrlf is a VB constant that adds a new line (well actually a carriage return (CR) + a linefeed (LF)
    for code brevity I have not used the classic style of if statement, whcih some purists may throw their hands up in horror

    I used
    Code:
    if <boolean statement> then 'do soemthing
    instead of the purists desired syntax of
    Code:
    if <boolean statement> then
      'do soemthing if true
    else 'optional else
      'do somethign if false
    endif
    as its a single piece of code that runs if true I've appended it to the same line as th eif statement, which can make it also easier to understand what is going on
    Last edited by healdem; 05-30-12 at 14:14.
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Apr 2012
    Posts
    22
    Error attached
    Attached Thumbnails Attached Thumbnails healdem.JPG  

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    that error message indicates that the compiler has detected an endif (end of code block) So i'd suggest you look at the code and see if you cna detect and endif that is out of place

    also should read vbwarning not vbvwarning

    code sup0plied was aircode, their may well be errors, and in this case there are errors.
    I'd rather be riding on the Tiger 800 or the Norton

  5. #5
    Join Date
    Apr 2012
    Posts
    22
    Still returning same error, and i checked all spellings, end ifs,etc . no luck. Sure i must use strlen, not len ? I tried and it did not work too.

  6. #6
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    OK
    so count back match all endif's with an opening IF
    the problem arises becuase I used the short form of IF not pursit form of IF
    I'd rather be riding on the Tiger 800 or the Norton

  7. #7
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    When you write an IF statement on a single line, you don't use the End If terminator:
    Code:
    If Me.AppointmentDate.Text = "" Then strErrorMessage = "No Appointment date defined" & vbcrlf
    If Me.cboClient.Value Then  strErrorMessage = strErrorMessage & "No Client defined" & vbcrlf
    If Me.cboSalon.Value Then  strErrorMessage = strErrorMessage & "No Salon defined" & vbcrlf
    If Me.cboStaff.Value Then   strErrorMessage = strErrorMessage & "No Satff member defined" & vbcrlf
    If Me.cboService.Value Then  strErrorMessage = strErrorMessage & "No Service defined" & vbcrlf
    If Me.ProductID.Value Then  strErrorMessage = strErrorMessage & "No Client defined" & vbcrlf
    ' End If  <-- REMOVE.
    It's not about being a purist or not, it's about knowing the syntax of the language
    Have a nice day!

  8. #8
    Join Date
    Apr 2012
    Posts
    22
    Thanks all, it was resolved with the below:

    Private Sub btnSaveCommand_Click()
    Me.AppointmentDate.SetFocus
    If Me.AppointmentDate = "" or isnull(Me.AppointmentDate) Then
    MsgBox "Please complete all fields"
    ElseIf isnull(Me.cboClient) Then
    MsgBox "Please complete all fields"
    ElseIf isnull(Me.cboSalon) Then
    MsgBox "Please complete all fields"
    ElseIf isnull(Me.cboSalon) Then
    MsgBox "Please complete all fields"
    ElseIf isnull(Me.cboStaff) Then
    MsgBox "Please complete all fields"
    ElseIf isnull(Me.cboService) Then
    MsgBox "Please complete all fields"
    ElseIf isnull(Me.ProductID) Then
    MsgBox "Please complete all fields"
    Else
    DoCmd.GoToRecord , , acNewRec
    MsgBox "Thanks - Appointment Booked!"
    End If
    End Sub

Posting Permissions

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