Next Button that cycles back to first on Access Form - ms-access

I've been trying a couple things to create a button that navigates back to the first record you are at the last record. I get the "can't go to specified record" error. Here are the two styles of code I have tried for this button:
Private Sub Command161_Click()
On Error Resume Next
If Me.CurrentRecord = acLast Then
DoCmd.GoToRecord , , acFirst
Else
DoCmd.GoToRecord , , acNext
End If
End Sub
Private Sub Command161_Click()
With Me.Recordset
If .AbsolutePosition = .RecordCount - 1 Then
DoCmd.GoToRecord , , acFirst
Else
DoCmd.GoToRecord , , acNext
End If
End With
End Sub
The goal is to loop back around to the first record without allowing the user to create a new record. I've tried this code with "allow additions" set to both yes and now, with the same result. Any help would be appreciated.

I would suggest defining a private predicate function within your form module which returns a boolean value depending on whether or not the active form is displaying the last record in its Record Source.
Such a function might be written:
Private Function LastRecordP() As Boolean
With Me.RecordsetClone
If Not .EOF Then
.MoveLast
.MoveFirst
LastRecordP = Me.CurrentRecord = .RecordCount
End If
End With
End Function
Your OnClick event handler for your button control could then be written more succinctly as:
Private Sub Command161_Click()
If LastRecordP Then
DoCmd.GoToRecord , , acFirst
Else
DoCmd.GoToRecord , , acNext
End If
End Sub
Alternatively, you could allow the function to accept a Form object as an argument and evaluate such function using the Me keyword, e.g.:
Private Function LastRecordP(Frm As Form) As Boolean
With Frm.RecordsetClone
If Not .EOF Then
.MoveLast
.MoveFirst
LastRecordP = Frm.CurrentRecord = .RecordCount
End If
End With
End Function
Private Sub Command20_Click()
If LastRecordP(Me) Then
DoCmd.GoToRecord , , acFirst
Else
DoCmd.GoToRecord , , acNext
End If
End Sub

Related

Open form and set unbound ComboBox to specific value

ISSUE
I have two forms:
frmForms
frmDockRental
I have two controls associated with this issue:
lstOwners on frmForms (unbound)
cboOwner on frmDockRental (unbound)
The second form (frmDockRental) is opened using different listboxes located on frmForms (See Image). I have one such listbox that's giving me grief. It is a filtered list of contacts that when double-clicked it should be opening frmDockRental to a NEW record and set cboOwner, which is unbound, to a specific item in the list. The same item listed in lstOwners from frmForms.
CODE
After a lot of fiddling, I came up with this - except nothing happens at all.
Private Sub lstOwners_DblClick(Cancel As Integer)
On Error GoTo lstOwners_DblClick_Err
On Error Resume Next
If (Form.Dirty) Then
DoCmd.RunCommand acCmdSaveRecord
End If
If (MacroError.Number <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
Exit Sub
End If
On Error GoTo 0
Exit Sub
DoCmd.OpenForm "frmDockRental", acNormal, "", "", acFormAdd, acDialog, Me.lstOwners
DoCmd.Close acForm, Me.Name
lstOwners_DblClick_Exit:
Exit Sub
lstOwners_DblClick_Err:
MsgBox Error$
Resume lstOwners_DblClick_Exit
End Sub
And on frmDockRental, this:
Private Sub Form_Load()
On Error GoTo Form_Load_Err
DoCmd.MoveSize , , 5.3 * 1440, 5.2 * 1440
' If (IsNull(OpenArgs)) Then
' Exit Sub
' End If
If Me.OpenArgs <> vbNullString Then
Me.cboOwner = Me.OpenArgs
End If
If (Not IsNull(OpenArgs)) Then
DoCmd.GoToRecord , "", acNewRec
End If
Form_Load_Exit:
Exit Sub
Form_Load_Err:
MsgBox Error$
Resume Form_Load_Exit
End Sub
I figured OpenArgs would be the best method to accomplish this, but it just doesn't work. Nothing at all happens. No errors, just nothing.
EDIT:
Here's an image of the step debugging.

Record navigation on a form

So I have been tasked with creating a tracking database & I am having an issue which is driving me crazy.
I have created an input form and for ease of use I need to create custom navigation buttons, again I have completed this and they work fine.
I have added code to the current event to disable the buttons if at the first record / last record - again this works fine.
This is where I have gotten stuck, as soon as I disable the built in navigation buttons it disables the custom buttons I have created. If I remove the current event code, again they work but not how I intend.
I would appreciate any help you could provide as I am still new to learning Access & I am sure this is a simple fix & something I am not doing correct - but I just cannot figure it out, I have included my code below.
Thank you in advance for your assistance.
Option Compare Database
Option Explicit
Private Sub cmdBack_Click()
On Error Resume Next
DoCmd.GoToRecord , , acPrevious
End Sub
Private Sub cmdNew_Click()
On Error Resume Next
DoCmd.GoToRecord , , acNewRec
End Sub
Private Sub cmdNext_Click()
On Error Resume Next
DoCmd.GoToRecord , , acNext
End Sub
Private Sub Form_Current()
On Error Resume Next
If Me.CurrentRecord = 1 Then
Me.cmdBack.Enabled = False
Else
Me.cmdBack.Enabled = True
End If
If Me.CurrentRecord >= Me.Recordset.RecordCount Then
Me.cmdNext.Enabled = False
Else
Me.cmdNext.Enabled = True
End If
If Me.NewRecord Then
Me.cmdNew.Enabled = False
Else
Me.cmdNew.Enabled = True
End If
End Sub
You can replace your DoCmd. calls with navigation using the form recordset.
Me.Recordset.MoveNext 'alternative to DoCmd.GoToRecord , , acNext
Me.RecordSet.MovePrevious 'alternative to DoCmd.GoToRecord , , acPrevious
Me.RecordSet.AddNew 'alternative to DoCmd.GoToRecord , , acNewRec
Thank you very much for the updated code - I had to add the following line to get them to work as I intented though.
Me.RecordsetClone.MoveLast

Microsoft Access runtime error DoCmd.GoToRecord , , acNext

I have been trying to get a set of buttons beneath a form for the simplicity for the users. Only the issue i get with the scripting is that i am not able to get the next record button to work. Here I get a Runtime Error 2105.
What it is suppose to do is show only the next existing record, but instead is gives a runtime error and skips all the other records and goes to the last one.
Any ideas of what i am doing wrong?
If Me.ActiveXBestEl92.Enabled = False Then
Me.ActiveXBestEl92.Enabled = True
End If
With Recordset
If .AbsolutePosition = .RecordCount Then
Me.ActiveXBestEl93.Enabled = False
Else
DoCmd.GoToRecord , , acNext
End If
End With
Exit_Next_Record:
Me.ActiveXBestEl93.Enabled = False
Exit Sub
Err_Next_Record:
MsgBox Err.Description
Resume Exit_Next_Record
Thanks in advance
Try this:
With Me.RecordsetClone
If .AbsolutePosition = .RecordCount Then
Me!ActiveXBestEl93.Enabled = False
Else
.MoveNext
End If
End With
or this:
With Me.RecordsetClone
If Me.CurrentRecord = .RecordCount Then
Me!ActiveXBestEl93.Enabled = False
Else
.MoveNext
End If
End With

Private Sub Form_Open(Cancel as Integer)

I have a main form with all of the selection criteria. If someone selects a year, like 2015, inadvertantly, there is NO data in the SQL Server for that year. When a button is pushed to open a subsequent Form, it will produce a Run_Time Error 2105. What would I need to add to the following code to alleviate the aforementioned error:
Private Sub Form_Open(Cancel As Integer)
Me.AllowEdits = True
DoCmd.GoToRecord , , acFirst
End Sub
Something along the lines of
'test for numeric
If IsNumeric(UserForm1.ComboBox1.Value) Then
'convert to long before comparing to year
If CLng(UserForm1.ComboBox1.Value) <= Year(Now) Then
DoCmd.GoToRecord , , acFirst
End If
End If

Access: Move to next record until EOF

I need to loop through a form by moving to the next record in the recordset.
I am using the Form_Current event to loop thru.
I have used a couple of statements and have different outcomes.
This one sometimes crashes and gives the error message: "You can't go to the specified record."
DoCmd.GoToRecord , , acNext
This one only goes upto 72 records and stops.
DoCmd.RunCommand acCmdRecordsGoToNext
This one only goes upto 129 records and stops.
Me.Recordset.MoveNext
Trying to find an instruction that will go to the next record untill it reaches the End of File.
I am using Access 2010 (Access 2002 -2003 file format mdb) as the front end. The recordsource is a SQL Server 2008 linked View.
To loop from current record to the end:
While Me.CurrentRecord < Me.Recordset.RecordCount
' ... do something to current record
' ...
DoCmd.GoToRecord Record:=acNext
Wend
To check if it is possible to go to next record:
If Me.CurrentRecord < Me.Recordset.RecordCount Then
' ...
End If
If (Not IsNull(Me.id.Value)) Then
DoCmd.GoToRecord , , acNext
End If
Hi,
you need to put this in form activate, and have an id field named id...
this way it passes until it reaches the one without id (AKA new one)...
I have done this in the past, and have always used this:
With Me.RecordsetClone
.MoveFirst
Do Until .EOF
If Me.Dirty Then
Me.Dirty = False
End If
.MoveNext
Me.Bookmark = .Bookmark
Loop
End With
Some people would use the form's Recordset, which doesn't require setting the bookmark (i.e., navigating the form's Recordset navigates the form's edit buffer automatically, so the user sees the move immediately), but I prefer the indirection of the RecordsetClone.
Set rs = me.RecordsetClone
rs.Bookmark = me.Bookmark
Do
rs.movenext
Loop until rs.eof
If you want cmd buttons that loop through the form's records, try adding this code to your cmdNext_Click and cmdPrevious_Click VBA.
I have found it works well and copes with BOF / EOF issues:
On Error Resume Next
DoCmd.GoToRecord , , acNext
On Error Goto 0
On Error Resume Next
DoCmd.GoToRecord , , acPrevious
On Error Goto 0
Good luck! PT
Keeping the code simple is always my advice:
If IsNull(Me.Id) = True Then
DoCmd.GoToRecord , , acNext
Else
DoCmd.GoToRecord , , acLast
End If
Add This Code on Form Close Event whether you add new record or delete, it will recreate the Primary Keys from 1 to Last record.This code will not disturb other columns of table.
Sub updatePrimaryKeysOnFormClose()
Dim i, rcount As Integer
'Declare some object variables
Dim dbLib As Database
Dim rsTable1 As Recordset
'Set dbLib to the current database (i.e. LIBRARY)
Set dbLib = CurrentDb
'Open a recordset object for the Table1 table
Set rsTable1 = dbLib.OpenRecordset("Table1")
rcount = rsTable1.RecordCount
'== Add New Record ============================
For i = 1 To rcount
With rsTable1
rsTable1.Edit
rsTable1.Fields(0) = i
rsTable1.Update
'-- Go to Next Record ---
rsTable1.MoveNext
End With
Next
Set rsTable1 = rsTable1
End Sub