Looping in a form

Infinite

More left to learn.
Local time
Today, 08:51
Joined
Mar 16, 2015
Messages
402
I am trying to get a form to loop through after a click the next. I have looked around in this site, and ive not found the answers im looking for. What im trying to do is I have navigation records for my form, but when I reach the last record, I would like it to loop.
Thanks! :D
 
Loop back to the first record? How are your current "Navigation records" working? What are they doing?
 
There just buttons that go to the next record and previous in the form.

And loop back to the first record, so after I reach the last record, it goes back to the first record, kinda like a "Loop"
 
Do You use code or macro's? Can you use code because it is easy to test for the last record in the Recordset?
 
Well, I use macros, but if you have a code (or do I have to give you the table names and such) for it I will use it.
 
Here's code that needs no table names BUT BEWARE I have not tested it.
Code:
Private Sub cmdNext_Click()
On Error GoTo Err_cmdNext_Click

With Me.Recordset
  If .CurrentRecord < .RecordCount Then
    .MoveNext
  Else
    .MoveFirst
  End If
End With

Exit_cmdNext_Click:
    Exit Sub

Err_cmdNext_Click:
    MsgBox Err.Description
    Resume Exit_cmdNext_Click
    
End Sub
 
Object doesnt support this property or method.

That is the error I get when I try to run it.
 
Try this one:
Code:
Private Sub cmdNext_Click()
On Error GoTo Err_cmdNext_Click

With Me.Recordset
  If .[COLOR="Red"]AbsolutePosition [/COLOR]< .RecordCount Then
    .MoveNext
  Else
    .MoveFirst
  End If
End With

Exit_cmdNext_Click:
    Exit Sub

Err_cmdNext_Click:
    MsgBox Err.Description
    Resume Exit_cmdNext_Click
    
End Sub
 
Ok, that works for going to the next record. Would I just change the movenext and first to move previous and move last?
 
Code:
Private Sub cmdPrev_Click()
On Error GoTo Err_mdPrev_Click

With Me.Recordset
  If .AbsolutePosition < .RecordCount Then
    .MovePrevious
  Else
    .MoveLast
  End If
End With

Exit_cmdPrev_Click:
    Exit Sub

Err_cmdPrev_Click:
    MsgBox Err.Description
    Resume Exit_cmdPrev_Click
    
End Sub

That doesnt work for some reason, am I doing something wrong?
the debugger says something is wrong with the first line of code (Private Sub cmdPrev_Click())
 
If .AbsolutePosition = 1 Then
.MoveLast
Else
.MovePrevious
End If
 
Ok, never mind about that last message, I had something messed up. I now have 2 VBA buttons to go back and forth. Now, how do I make it loop?
 
Based on the buttons you have and the code/logic supplid by Allan, the simple answer is click a button. However, if you are trying to loop through a recordset independent of the buttons, but by means of loop construct in vba,

Code:
Here's one that demonstrates opening a recordset and looping through the records and outputting data to the VBA immediate window:

Sub TestVBALoop()
Dim rs AS DAO.Recordset
Set rs = CurrentDb.OpenRecordset ("SELECT * FROM YourTable")
While Not rs.EOF
  Debug.Print rs!yourfield1 & "   " & rs!yourfield2 & "  " & rs!yourFiled3
  rs.MoveNext
Wend
End Sub
 
I tried that (after creating a button called TestVBALoop) and it just breaks the other buttons to go next and previous. What am I doing wrong?
 
Let's go back to What are you trying to do--in plain English?
You have instructions for doing the loop with buttons. The last bit of code was how to do a loop in vba.
What are you trying to do??
 
I have a form, and in that form I have multiple items, I would like to be able to go the the last item, then when I hit the next button, go (or "Loop") to the first record. Then do the same thing for the previous record, when I get the the first record it "Loops" to the last record.
 

Users who are viewing this thread

Back
Top Bottom