Custom navigation buttons

Jeff H
3StarLounger
Posts: 316
Joined: 31 Oct 2017, 20:07

Custom navigation buttons

Post by Jeff H »

This is not critically important but I’d like to know if there’s a way to make it work. On a data entry form, I want users to be able to scroll back and forth within the set of records they create in one session. That way they can check them over and make corrections.

I also want to get rid of the search option on the navigation bar. So I found what seems like a really nice, portable button configuration online. The idea is that the click events for custom navigation buttons are functions in a separate module.

But there is a quirk. After starting a new record the Previous button skips the most recently saved record. E.g. if I create and save 2 records then I’m working on a third, the Previous button skips to record 1 instead of going to record 2. Likewise, if I’ve started record 4 the button skips to record 2. After that I can scroll backward and forward hitting all the records.

The code for the Previous function is below. I added the first bit about undoing the current record because the 'Save & Next' action pre-populates the new record with the same patient and volunteer as the last record, so you can’t browse backward without certain required data.

Code: Select all

Public Function GoToPrevious()
If Screen.ActiveForm.NewRecord Then Screen.ActiveForm.Undo
On Error GoTo OnFirstRecord

Screen.ActiveForm.AllowAdditions = False
DoCmd.RunCommand acCmdRecordsGoToPrevious
GoTo WayOut
    
OnFirstRecord:
MsgBox "You're on the first record"
    
WayOut:
On Error GoTo 0
    
End Function

User avatar
HansV
Administrator
Posts: 69449
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Custom navigation buttons

Post by HansV »

Can you make a sample database available?
Regards,
Hans

Jeff H
3StarLounger
Posts: 316
Joined: 31 Oct 2017, 20:07

Re: Custom navigation buttons

Post by Jeff H »

Ok. As before I'm getting the error that 670kb is too large a file to upload to the forum, so I'll email it.

- Jeff

User avatar
HansV
Administrator
Posts: 69449
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Custom navigation buttons

Post by HansV »

I'll have to study it, I'll have to figure out what to do first.
Regards,
Hans

User avatar
HansV
Administrator
Posts: 69449
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Custom navigation buttons

Post by HansV »

As far as I can tell, the line that you added

Code: Select all

If Screen.ActiveForm.NewRecord Then Screen.ActiveForm.Undo
is the cause of the problem. If I remove that line, it appears to work as intended.

Instead of populating PatientID etc., set their DefaultValue property. When the user goes back, you won't have to undo.
Regards,
Hans

Jeff H
3StarLounger
Posts: 316
Joined: 31 Oct 2017, 20:07

Re: Custom navigation buttons

Post by Jeff H »

Thanks Hans. I will check that out soon.

I think you're suggesting that as the code opens a new record, it should set the DefaultValue of those two fields to the desired values, instead of directly populating the fields. Then on Unload (I think) I should clear the DefaultValue properties for those fields.

I'll let you know how it goes.

Thanks!

User avatar
HansV
Administrator
Posts: 69449
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Custom navigation buttons

Post by HansV »

Jeff H wrote:
15 Oct 2020, 21:22
I think you're suggesting that as the code opens a new record, it should set the DefaultValue of those two fields to the desired values, instead of directly populating the fields.
Yes, indeed.
Regards,
Hans

Jeff H
3StarLounger
Posts: 316
Joined: 31 Oct 2017, 20:07

Re: Custom navigation buttons

Post by Jeff H »

Unfortunately, I can't seem to get this to work. I rem'd out that line and set the Default Values before opening a new record, but the only difference I'm seeing is that the new record doesn't populate the names associated with the patient and volunteer IDs until I update another control. But the 'Previous' button still skips over the most recently updated record.

I'm wondering if I've set up the Save & Next code (below) improperly. (If you look at that code in the db I sent you, you'll see I've cleaned it up from what it was.)

Also, I realized I have some confusion about populating the patient and volunteer fields. In the original code I directly populated the fields "PatientID" and "VolunteerID". But using the Default Value property I saw that I needed to put those ID values (which are the Auto Number IDs) into the comboboxes (cmbPID and cmbVID). I'm not sure why it worked the other way or what different effect this might have now.

Code: Select all

Private Sub cmdSave_Click()
Dim msgTxt As String
Dim IDP As Long 'Patient ID
Dim IDV As Long 'Volunteer ID
Dim SVC As Long 'Service month (yymm)

Forms!frmServiceEvents.SetFocus
On Error GoTo ErrHandler
SVC = Me.SvcMonth
IDP = Me.PatientID
IDV = Me.VolunteerID

'===Missing data
If IDP = 0 Or IDV = 0 Or Me.TotHours = 0 _
    Or Not IsDate(Me.SvcDate) Then
    
    msgTxt = "Can't save due to missing data." & _
        vbCrLf & vbCrLf & "Remove current data?"
    
    If MsgBox(msgTxt, vbYesNo + vbQuestion + vbDefaultButton2, "REMOVE  THIS DATA?") _
        = vbYes Then
        
        If Me.NewRecord Then
            Me.Undo
        Else
            DeleteRecord
        End If
    End If

'===Required data present    
Else
    Me.cmbPID.DefaultValue = IDP
    Me.cmbVID.DefaultValue = IDV
    Me.SvcMonth.DefaultValue = SVC
    
    Me.Refresh
    DoCmd.Save acForm, Me.Name
    blnSaved = True
    Me.AllowAdditions = True
    Me.Recordset.AddNew
    
    DoCmd.GoToControl (Me.cmbPID.Name)
    Me.cmdClear.Enabled = True
End If

Exit Sub

ErrHandler:
msgTxt = "The following run-time error occurred:" & vbCrLf
    
MsgBox msgTxt & Err.Number & ": " & Err.Description

'Me.PatientID = IDP
'Me.VolunteerID = IDV
'Me.SvcMonth = SVC

DoCmd.GoToControl (Me.cmbPID.Name)

End Sub

User avatar
HansV
Administrator
Posts: 69449
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Custom navigation buttons

Post by HansV »

I'll be away from my computer for a few hours; I will investigate it when I get back.
Regards,
Hans

User avatar
HansV
Administrator
Posts: 69449
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Custom navigation buttons

Post by HansV »

I think the problem was caused by the order in these two lines:

Code: Select all

    Screen.ActiveForm.AllowAdditions = False
    RunCommand acCmdRecordsGoToPrevious
The first line cancels the new record, so the form goes back to the last record.
The second line then goes back another record.

By switching these lines, the problem disappears.

Let me know if this works for you.
Regards,
Hans

Jeff H
3StarLounger
Posts: 316
Joined: 31 Oct 2017, 20:07

Re: Custom navigation buttons

Post by Jeff H »

Yep, that'll do it!! :fanfare:

So can you clarify for me what's going on here? The author of the code I got online (Andy Brown) says this about "Allow Additions":
The reason I keep resetting the AllowAdditions property of the form is this. When a user is on the last record of a form in Access and tries to go to the next one, what will normally happen is that Access will create a new record and display that. To avoid this behaviour, I allow additions only when a user has specifically clicked on the button to add a new record.
His code has the AllowAdditions line first. And it is included in all the functions to make sure that whatever you did last didn't leave it in the wrong setting. So my question is, should I reverse the order in all instances? Is the fact that it didn't work for me a matter of my 'Save & Next' code?

User avatar
HansV
Administrator
Posts: 69449
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Custom navigation buttons

Post by HansV »

In the other functions/procedures that set AllowAdditions, you can leave it as it is.
Regards,
Hans

Jeff H
3StarLounger
Posts: 316
Joined: 31 Oct 2017, 20:07

Re: Custom navigation buttons

Post by Jeff H »

Ok.
Nice detective work! Thanks again.