Full Version: Has a button been pushed?
UtterAccess Forums > Microsoft® Access > Access Forms
dmfrog
Hi
I have a form that has a "cancel" button. The form has several fields, one of which has an OnExit sub. If the user enters something into that field and then presses the cancel button, the form still tries to do the OnExit for the field. Is there a way to identify, inside the OnExit' if the cancel button has been pushed, thereby skipping the rest of the OnExit sub?
I'm sure there is, I'm just not seeing the obvious today. sad.gif
DM
kbrewster
Have you tried entering Cancel=True in your cancel button code?
dmfrog
Hi Kristen
Tried that, but the OnExit code for the field is executed prior to the code behind the cancel button. I'd like the OnExit code for the field to be able to identify that the user has pressed the cancel button and skip the OnExit sub and just go do the code for the cancel button. Hope that makes sense.
DM.
Bob_L
What is being done in the OnExit event? It might be better done elsewhere, but hard to say without having knowledge of what you are trying.
kbrewster
Can you post the code your are running in the onexit event?
dmfrog
Here's the OnExit code
CODE
  
Private Sub ubPOSTitle_exit(Cancel As Integer)
' Check to see if the entered position already exists.
nbsp;   On Error GoTo Sub_Err

    If ((IsNull(Me.ubPOSTitle)) Or (Me.ubPOSTitle = "")) Then
        ' nothing to check - exit
        GoTo Sub_Exit
    End If
    
    Dim RMSDatabase As Database
    Dim RMSQueryDef As QueryDef
    Dim where As Variant
    Dim iResult As Integer
    Set RMSDatabase = CurrentDb()
    Call deleteTmpQrys("z_qryPOSTitle")
    where = Null
    where = "(((tblPosition.POSTitle) Like " + "'" + Me.[ubPOSTitle] + "')"
    where = where & " AND ((tblPosition.POSEndDate) Is Null))"
    
    Set RMSQueryDef = RMSDatabase.CreateQueryDef("z_qryPOSTitle", _
                      "Select * from tblPosition where " & where & ";")
        
    iResult = DCount("1", "z_qryPOSTitle")
    
    If iResult = 0 Then
        ' OK to create the new position
        Me.cboPositionUp.Enabled = True
        Me.cboPositionUp.SetFocus
        bAddNew = True
        
    Else
        MsgBox "Position Title: " & (Me.ubPOSTitle.Value) & " is already in the Database." _
        & vbCrLf & "Please enter another title or search for" _
        & vbCrLf & "      and modify the existing record.", _
        vbCritical, "Record exists"
        Me.ubPOSTitle.Value = ""
        Me.ubPOSTitle.SetFocus
        Cancel = True                       ' cancels the TAB or ENTER
    End If
Sub_Exit:
    ' clean up the temporary query
    Call deleteTmpQrys("z_qryPOSTitle")
    Exit Sub
Sub_Err:
    Select Case err.Number
        Case Else
            Select Case ErrorDisplay(err.Number, Error$, FNAME, VERSION, "ubPOSTitle_exit")
                Case errContinue
                    Resume Next
                Case errexit
                    Resume Sub_Exit
            End Select
    End Select
End Sub 'ubPOSTitle_AfterUpdate
'

And this is the cancel button code:
CODE
  
Private Sub cmdCancel_Click()
    On Error GoTo Sub_Err
    Me.cmdAddPosition.Visible = True
    Me.cmdAddPosition.Enabled = True
    Me.cmdAddPerson.Visible = True
    Me.cmdAddPerson.Enabled = True
    
    Me.cmdMod.Visible = True
    Me.cmdMod.Enabled = True
    Me.cmdView.Visible = True
    Me.cmdView.Enabled = True
        
    Me.cmdAddPosition.SetFocus
    
    Me.cmdCancel.Visible = False
    Me.cmdCancel.Enabled = False
    
    Me.cmdSaveNew.Visible = False
    Me.cmdSaveNew.Enabled = False
    
    Me.cmdSaveMod.Visible = False
    Me.cmdSaveMod.Enabled = False
    
    Me.lblPosTitle.Visible = False
    Me.ubPOSTitle.Visible = False
    Me.ubPOSTitle.Value = Null
    Me.lblReq1.Visible = False
    Me.lblReq2.Visible = False
    
    Me.lblPosModify.Visible = False
    Me.cboPositionMod.Visible = False
    Me.cboPositionMod.Locked = False
    Me.cboPositionMod.Enabled = True
    Me.cboPositionMod.BackColor = 16777215
    Me.cboPositionMod = Null
    
    Me.cboPositionUp.Visible = False
    Me.cboPositionUp = Null
    
    Me.lblUp.Visible = False
    Me.cboPositionUp.Visible = False
    Me.cboPositionUp = Null
    
    Me.lblCreateDate.Visible = False
    Me.ubPOSCreateDate.Visible = False
    Me.ubPOSCreateDate = Null
    Me.ubPOSCreateDate.BackColor = 16777215
    
    Me.lblEndDate.Visible = False
    Me.ubPOSEndDate.Visible = False
    Me.ubPOSEndDate = Null
    
    Me.lblLinkDate.Visible = False
    Me.ubLinkDate.Visible = False
    Me.ubLinkDate = Null
        
    Me.lblUnlinkDate.Visible = False
    Me.ubUnlinkDate.Visible = False
    Me.ubUnlinkDate = Null
    
    bAddNew = False
    
    Me.lblHelp1.Visible = False
    Me.lblHelp2.Visible = False
    Me.lblHeader.Caption = FHEADER
    
Sub_Exit:
    Exit Sub
Sub_Err:
    Select Case err.Number
        Case Else
            Select Case ErrorDisplay(err.Number, Error$, FNAME, VERSION, "cmdCancel_Click")
                Case errContinue
                    Resume Next
                Case errexit
                    Resume Sub_Exit
            End Select
    End Select
End Sub 'cmdCancel_Click
'

Thanks
DM
Bob_L
I would not put the code in the On Exit event but instead use the BEFORE UPDATE event.
dmfrog
Hi - I'll try using the different event. Thanks!
M
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.