Check Boxes Help

CBG2112

Registered User.
Local time
Yesterday, 22:54
Joined
Aug 4, 2009
Messages
32
I have a form with one combobox and several check boxes. When a user selects a certain value on the combobox, i would like for a messagebox to display if the user does not mark one or more of the check boxes. Here's my attempt but it still saved the record without displaying a messagebox. Thanks in advance for the help.

cboStat.SetFocus

If cboStat.Text = "" Then
MsgBox "Please select a status.", vbInformation
Exit Sub
ElseIf cboStat.Text = "Type X" Then
If AFL = False And BFL = False And CFL = False And DFL= False And EFL= False And FFL = False Then
MsgBox "Please select one or more checkboxes.", vbInformation
Me.cboStatus = ""
Exit Sub
End If
 
Which event did you put it on? It needs to be in the Form's BEFORE UPDATE event.

And if the validation fails you use

Cancel = True

to stop the update.
 
I hadthe validation code on the click event of the button. I changed it to the before_update event of the form but it still goes to the confirmation messagebox. I'm wondering if the logic to the validation code is incorrect.
Code:
   cboStat.SetFocus

    If cboStat.Text = "" Then
        MsgBox "Please select a status.", vbInformation
        Exit Sub
    ElseIf cboStat.Text = "Type X" Then
        If AFL = False And BFL = False And CFL = False And DFL= False And EFL= False And FFL = False Then
                MsgBox "Please select one or more checkboxes.", vbInformation
                Me.cboStatus = ""
                Cancel = True
        End If                      

    cboStat.SetFocus

    Beep

    If MsgBox("Are you sure that you want to update this?", _
        vbYesNo + vbQuestion, "Confirmation") = vbYes Then

    DoCmd.RunCommand acCmdSaveRecord
 
Okay, a couple of things -

1. You don't include the DoCmd.RunCommand acCmdSaveRecord in the before update event because it is already in the process of saving when you do that.

2. Second, you have to escape since you have the message box there and use NO because if you set cancel = true there if they say no it will be canceled, otherwise you just let it pass through if they say yes.

3. Also, you don't use .TEXT for things as that requires focus and you can't always give it the focus. So, just use the value or if the combo box has the text on the second column and the first column is the ID which is the bound column, you can use what I've put in below. (it is zero based so (0) is the first field, (1) is the second and so on.

Code:
    If cboStat[B][COLOR=red].Column(1)[/COLOR][/B] = "" Then
        MsgBox "Please select a status.", vbInformation
        Exit Sub
    ElseIf cboStat[COLOR=red][B].Column(1)[/B][/COLOR] = "Type X" Then
        If AFL = False And BFL = False And CFL = False And DFL= False And EFL= False And FFL = False Then
                MsgBox "Please select one or more checkboxes.", vbInformation
                Me.cboStatus = ""
                Cancel = True
                [B][COLOR=red]Exit Sub[/COLOR][/B]
        End If                      

    cboStat.SetFocus

    Beep

    If MsgBox("Are you sure that you want to update this?", _
        vbYesNo + vbQuestion, "Confirmation") = [COLOR=red][B]vbNo[/B][/COLOR] Then
        [COLOR=red][B]Cancel = True[/B][/COLOR]
[COLOR=red][B]        Me.Undo[/B][/COLOR]
[COLOR=red][B]   End If[/B][/COLOR]
 

Users who are viewing this thread

Back
Top Bottom