Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Move focus before unload to prevent loss of data

Status
Not open for further replies.

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?

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
 
OK, I am confused. If a user hits escape they are trying to avoid saving the change, but you want to save the change??
 
Not in this case. We're trying to make this as efficient as possible for the user. If they hit the escape key and no changes have been made, the form of course closes. However, after they have entered data in 8 fields (having tabbed between each), the goal is to let them hit the escape key, enter when prompted to save, and then the program moves on to the next record requiring data entry.

Not the norm, I guess, but they've got a lot of records to move through so I thought this might be the best route in this particular case -- keep their keys on the keyboard rather than shifting between keyboard and mouse, or several tabs to the save button, etc.

Or am I just way off track from a user experience perspective in general?
 
How are ya PRMiller2 . . .

Typically (new record or previously saved) ... hitting the [blue]ESC[/blue] key returns the record to the state it was in before any edits were made ... that is if any edits were made! ... otherwise nothing changes! [blue]Why save a record thats already been saved?[/blue]

Typically [blue]ESC[/blue] is used to back out of any edits to a record (again new record or previously saved). Using [blue]ESC[/blue] as you've prescribed, how could a user say ... backout of editing the wrong record?

I believe you simply need to restructure your logic as far as what constitutes a save!

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
well, I would normally agree, except for the speed of entry needed for these tasks... and the fact that this is actually an unbound form, that will be doing inserts or updates to the database.

Still, perhaps I should think on it overnight! I could always have the user tab one more time to the Save button and, with an Enter keystroke, the record is inserted/updated and the form is closed.

Hmm...
 
How are ya PRMiller2 . . .

Since the form is [blue]UnBound[/blue] ... why not just add a [blue]Save Button?[/blue]

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Already have one, and I now have it set to the last tab stop after data entry, thus fulfilling the purpose of making this as fast as possible for my users.

I'll sleep on it, but I'll probably end up removing the code that currently checks for changes before allowing the Escape key to close the form.

Thanks for the feedback everyone, and Happy New Year!
 
PRMiller2 . . .

Have you considered a [blue]hotkey[/blue] combination ... say ([blue]Ctrl + Alt + S[/blue])?

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
with an Enter keystroke
Set the Default property of your CommandButton to Yes

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Makes sense, gentlemen.

Question: How then to treat the form differently depending on whether the user presses the Escape key or the form close X on the form caption bar?

If the user presses Escape then, as AceMan said, let them escape without prompting to save changes. If, however, they hit X, use the Form_Unload event to prompt if they want to save changes, discard or cancel the unload/close event?

Form_Unload works as desired... now how to skip the prompt for changes if the user presses Escape?
 
PRMiller2 . . .

I have can provide info as far as the unbound form is concerned. However ... in an attempt to steer you in the right direction I have too ask ...

Why an [blue]UnBound Form?[/blue] ... instead of [green]Bound![/green]

[blue]Your Thoughts? . . .[/blue]



See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top