PRMiller2
Technical User
- Jul 30, 2010
- 123
I have a form for data entry. If a user hits Escape or close, the program checks to see if data has been changed. If it has, then it asks the user if they want to save their changes. If they do want to save, a SQL statement is executed to insert data from the unbound form into the database.
For the purposes of this post, let's assume it's always a new record, not changes to an existing one.
I am using the form's Unload event to drive the prompt. However, I have found that if the user presses Escape while a text box still has focus, the data in that text box will not save. They need to press the TAB key or select another control so that the text box loses focus. Once it does lose focus, the data in that text box will also save.
I tried programmatically moving the focus to a button in the Unload event, but that doesn't seem to help.
Recommendations?
For the purposes of this post, let's assume it's always a new record, not changes to an existing one.
I am using the form's Unload event to drive the prompt. However, I have found that if the user presses Escape while a text box still has focus, the data in that text box will not save. They need to press the TAB key or select another control so that the text box loses focus. Once it does lose focus, the data in that text box will also save.
I tried programmatically moving the focus to a button in the Unload event, but that doesn't seem to help.
Recommendations?
Code:
Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Err_Handler
Dim intButtons As Integer
Dim intSelectedButton As Integer
Dim strQuestion As String
Dim strSQL As String
Dim strTitle As String
Me.cmdSave.SetFocus
If TempVars("tblnDataChanged").Value = True Then
intButtons = vbYesNoCancel + vbQuestion + vbDefaultButton1
strQuestion = "Save changes to this pharmacy contact record?"
strTitle = "Save changes?"
intSelectedButton = MsgBox(prompt:=strQuestion, Buttons:=intButtons, Title:=strTitle)
Select Case intSelectedButton
Case 2 'Cancel
Cancel = 1
Case 6 'Yes
SavePharmacyContact
End Select
End If
Exit_Handler:
Exit Sub
Err_Handler:
Call LogError(Err.Number, Err.Description, "frmCallTracking.Form_Unload()")
Resume Exit_Handler
End Sub
Code:
Private Sub SavePharmacyContact()
On Error GoTo Err_Handler
Dim dbCurrent As DAO.Database
Dim strSQL As String
strSQL = "INSERT INTO ContactHistory" & _
" (ClaimDetailID_fk, UserID_fk, NABP, ContactDate, ContactName, ContactPhone, ContactEmail," & _
" AwareOfRequirementID_fk, EducationProvidedID_fk, CallResultID_fk, CallErrorReasonID_fk," & _
" Notes" & _
" Values(" & "FILLME" & ", " & glngUserID & ", " & Me.txtClaimNCPDPNumber & ", #" & Date & "#," & _
" '" & Me.txtPharmacyContactName & "', " & Me.txtPharmacyContactPhone & ", '" & Me.txtPharmacyContactEmail & "'," & _
" " & Me.cboAwareOfRequirement.Value & ", " & Me.cboEducationProvided.Value & ", " & Me.cboCallResult.Value & "," & _
" " & Me.cboReasonForError.Value & ", '" & Me.txtPharmacyResponse & "');"
Stop
TempVars("tblDataChanged") = False
Exit_Handler:
Exit Sub
Err_Handler:
Call LogError(Err.Number, Err.Description, "frmCallTracking.SavePharmacyContact()")
Resume Exit_Handler
End Sub