Question : If, Then, Warning Message...?

Hi, I have a field (called Count) on a form which is calculated automatically. When the user clicks the close button I need an If statement to check the value in the field and if greater or less than 0, I need a message box asking the user if they want to exit. If they click yes, then the form closes. If they click no they will be returned to the form to make changes. Of course, if the field value is 0 I would like the form to close without any message box appearing.

Thank you for your help.

Answer : If, Then, Warning Message...?

<the on click event doesn't want to do anything?>
What "OnClick" event?

You specified that this should happen...:
 "When the user clicks the close button" ("The Close button", ...meaning *the* windows standard close button, the little "X" in the upper right side of a window)
... not...
 "When the user clicks *my* close button.

So our codes are on the Close event and the Unload event receptively.

If you want the code on "Your" close button, then use code like this:

Private Sub btmClose_Click()
Dim bytAnswer As String

    If Instock.Value <> 0 Then
        bytAnswer = MsgBox("Exit now?", vbYesNo, "Exit?")
            If bytAnswer = vbYes Then
                'Do nothing
            ElseIf bytAnswer = vbNo Then
                Me.Instock.SetFocus
                Exit Sub
            End If
    Else
        'Do Nothing
    End If
   
    DoCmd.Close
End Sub

Remember here that you have not told us what other code (if any) is present on your close button already, so this code may need to be tweaked.

Finally, remember that when you code your own close button you have to be sure that you are covering all the contingencies for the various states that the data may be left in.

I am sure 2toria can post similar working code.

;-)

JeffCoachman
Random Solutions  
 
programming4us programming4us