Solved

Message box cancel NOT working

Posted on 2004-10-07
5
311 Views
Last Modified: 2011-10-03
I have the following code which should prevent a user from saving the record if all of the product checkboxes are empty-they must select one-but is isn't working correctly. Thanks and appreciate any help

Private Sub cmdSave_Click()
On Error GoTo Err_cmdSave_Click

If IsNull(Me.SPC) And IsNull(Me.SPS) And IsNull(Me.CheckFree) And IsNull(SCS) And IsNull(SIM) Then
MsgBox "You must select a Product Type", vbCancel
vbCancel = True
Else
    DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
    DoCmd.Close
 
Exit_cmdSave_Click:
    Exit Sub
End If

Err_cmdSave_Click:
    MsgBox Err.Description
    Resume Exit_cmdSave_Click
   
End Sub
0
Comment
Question by:ssmith94015
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 8

Expert Comment

by:bramsquad
ID: 12253427
your code is pretty crazy.  goto statements make the mind go numb


try this

Private Sub cmdSave_Click()
    On Error GoTo Err_cmdSave_Click

    If IsNull(Me.SPC) And IsNull(Me.SPS) And IsNull(Me.CheckFree) And IsNull(SCS) And IsNull(SIM) Then
        'if its all null then just exit out of the function to give them another shot!
        MsgBox "You must select a Product Type", vbOKOnly, "Error"
        Exit Sub
    Else
        'success
        DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
        DoCmd.Close
        Exit Sub
    End If

Err_cmdSave_Click:
    MsgBox Err.Description
End Sub

~b
0
 

Author Comment

by:ssmith94015
ID: 12255332
Thanks for the suggestion and I just relaized something.  The IsNull may not work as the six boxes are checkboxes and as my husband stands here yelling at me. I am totally confused about the syntax of using checkboxes.  I tried the below for just one checkbox, but it does not work either.

Private Sub Form_Close()

    On Error GoTo Err_cmdSave_Click
   
    If Me.SPC.Checkboxes.Checked = False Then
        'if its all null then just exit out of the function to give them another shot!
        MsgBox "You must select a Product Type", vbOKOnly, "Error"
        DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
    Else
        'success
        DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
        DoCmd.Close
   
        Exit Sub
    End If

Err_cmdSave_Click:
    MsgBox Err.Description

End Sub
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 50 total points
ID: 12255759
take it that the controls SPC, SPS, CheckFree, SCS and SIM are all checkboxes?

This is how you check the value of a checkbox:

   If Not SPC.Checked  AND Not SPS.Checked and NOT CheckFree.Checked and NOT SCS.CHecked and NOT SIM.Checked Then
        'if its all null then just exit out of the function to give them another shot!
        MsgBox "You must select a Product Type", vbOKOnly, "Error"
        DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
    Else
        'success
        DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
        DoCmd.Close
   
        Exit Sub
    End If

The Checked Property of a CheckBox is TRUE if the checkbox IS Checked, and FALSE if is ths NOT checked (these are BOOLEAN values, True and False) so you do not need to EXPLICITLY test ("If SPS.CHecked = True Then"  is IDENTICAL in function to "If SPS.Checked Then")

AW

0
 

Expert Comment

by:huyntminh
ID: 12255911
Private Sub Form_Close()

    On Error GoTo Err_cmdSave_Click
   
    If Not (SPC.Checked And SPS.Checked And CheckFree.Checked And SCS.Checked And SIM.Checked) Then
        'if its all null then just exit out of the function to give them another shot!
        MsgBox "You must select a Product Type", vbOKOnly, "Error"
        DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
    Else
        'success
        DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
        DoCmd.Close
   
        Exit Sub
    End If

Err_cmdSave_Click:
    MsgBox Err.Description

End Sub
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 12302429
glad to be of assistance

AW
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question