Stop TAB from entering new record

I didn't know that Bob Larson is a celebrity like Meryl Street, Will Smith etc ;)
 
Screenshot - tblEmployees :D
 
attachment.php

I'm sorry, but that confused the hell out of me. Tabbed like hell and no new record, but it ended up in the table?

Perhaps you're talking about what I just figured out... which is that it wasn't the tabbing that added the record, it was when I closed the form (without hitting the save record button) that saved it. See, I was filling out the form and closing it without hitting the save button, and then going to look in the table. Low and behold the record was there...but it wasn't from the tabbing it was from it being saved when I closed the form.

I apologize very much. Apparently my problem now is that I don't want the record to be saved when closing a form unless the user clicks the save record button.
 
One solution is to disable the X button and have a custom button with the following code.

Code:
Private Sub cmdClose_Click()
On Error GoTo Err_cmdClose_Click
    
    Dim intUserResponse As Integer
    
    If Me.Dirty Then
        
        intUserResponse = MsgBox("You have not saved your Edits.  Are you certain you want to close this form and lose your changes?", vbYesNo + vbQuestion)
        
        If intUserResponse = vbYes Then
            Me.Undo
            DoCmd.Close acForm, Me.Name

        End If
    
    Else
        
        DoCmd.Close acForm, Me.Name
    
    End If
    
Exit_cmdClose_Click:
    Exit Sub

Err_cmdClose_Click:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_cmdClose_Click
    
End Sub
 
One solution is to disable the X button and have a custom button with the following code.

Code:
Private Sub cmdClose_Click()
On Error GoTo Err_cmdClose_Click
    
    Dim intUserResponse As Integer
    
    If Me.Dirty Then
        
        intUserResponse = MsgBox("You have not saved your Edits.  Are you certain you want to close this form and lose your changes?", vbYesNo + vbQuestion)
        
        If intUserResponse = vbYes Then
            Me.Undo
            DoCmd.Close acForm, Me.Name

        End If
    
    Else
        
        DoCmd.Close acForm, Me.Name
    
    End If
    
Exit_cmdClose_Click:
    Exit Sub

Err_cmdClose_Click:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_cmdClose_Click
    
End Sub

ions,

There are several way to bypass your command button code that you posted. Not a very rock solid solution if you are not also using other form events.
 
Last edited:
Apparently my problem now is that I don't want the record to be saved when closing a form unless the user clicks the save record button.
Here's one way.

Declare a variable in the Declarations section of the form:
Code:
Option Compare Database
Option Explicit

Private doSave As Boolean
Save button's Click event:
Code:
Private Sub Button_Click()
On Error GoTo E_Handler

    doSave = True
    DoCmd.RunCommand acCmdSaveRecord
    doSave = False

Exit_E_Handler:
    Exit Sub
    
E_Handler:
    doSave = False
    MsgBox Err.Description
    Resume Exit_E_Handler

End Sub
Form's Before Update event:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    If doSave = False Then
        Cancel = True
        Me.Undo
    End If
End Sub
Form's On Error event:
Code:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
    Const NOSAVE_ON_CLOSE As Long = 2169
    
    If doSave = False Then
        If DataErr = NOSAVE_ON_CLOSE Then
            Response = acDataErrContinue
        End If
    End If
End Sub
 
Last edited:

Users who are viewing this thread

Back
Top Bottom