Check to see if close button is clicked.

bethless

New member
Local time
Today, 00:51
Joined
Jan 14, 2008
Messages
3
Does anyone know of a way to check to see if a close button I included in my form is clicked. I have a lostfocus sub that checks data and I don't want it to execute the lostfocus sub if the user clicked the close button (docmd.close). I tried to use getfocus, but the lostfocus happens first and that is what I'm trying to avoid.
 
It sounds like you're doing this all in Macros, which isn't really the best solution (or most of the time, it's not even a good one).

The event you're looking for is the command button's OnClick event. You control what happens through VBA. I'd imagine you could play with it enough to get it to somehow work with macros, but I wouldn't suggest doing that.
Code:
Sub YourCommandButtonName_OnClick()

'They've clicked the close button, so do what you want here

End Sub
 
It sounds like you're doing this all in Macros, which isn't really the best solution (or most of the time, it's not even a good one).

The event you're looking for is the command button's OnClick event. You control what happens through VBA. I'd imagine you could play with it enough to get it to somehow work with macros, but I wouldn't suggest doing that.
Code:
Sub YourCommandButtonName_OnClick()

'They've clicked the close button, so do what you want here

End Sub


Thanks for the reply. I am using VBA. Here are the sub's that I have for the fields/buttons.

Code:
Private Sub Close_Click()
     Dim answer
    If DataChanged Then
        answer = MsgBox("Data changed, save data before closing?", 4, "Verify Close Traffic Action")
    Else
        answer = MsgBox("Close Traffic Action?", 4, "Verify Close Traffic Action")
    End If
    
    ' answer "Yes" = 6 answer "No" = 7
    
    If DataChanged And answer = 6 Then
        Call Save_Click
    End If
    If DataChanged Then
        DoCmd.Close , , acSaveYes
        Exit Sub
    End If
    
    If answer = 6 Then
        DoCmd.Close , , acSaveYes
    End If
End Sub

Private Sub OrderNbr_LostFocus()
    'If Me.Close.OnClick = True Then
'    If Me.CloseButton Then
    If CloseForm Then
        Exit Sub
    Else
        Call Check_OrderNbr
    End If
End Sub

Private Sub Close_GotFocus()
    CloseForm = True
End Sub

I have logic in the sub Check_OrderNbr that checks to see if user entered at least 8 characters in the OrderNbr field and if they do it gets the data from the table and loads the form. I don't want to do this if the user clicks the close button. I think because the lostfocus happens before anything else it calls the sub Check_OrderNbr. I wanted to know if there is a command that tells me what the user is trying to do next. Like clicking the close button. I tried some of the Me.Close options, which "Close" is the button name and so far I have found nothing that does what I want. I appreciate any ideas.
 
You'll have to take the code out of the LostFocus event because that's going to fire no matter what. Put the code in the AfterUpdate event instead. The LostFocus is going to fire every time the control has focus and then loses it, no matter what other control you click on. AfterUpdate is only going to fire after the value changes. Clicking on other controls, including the Close button, will not alter that value, and therefore will not fire an event.
 

Users who are viewing this thread

Back
Top Bottom