Do loop

DaniBoy

Registered User.
Local time
Today, 06:49
Joined
Nov 18, 2001
Messages
174
Hello,

I have a form that updates a field on the event "On Current".

I like to make a command botton that will go through all the records on this form, as if I am pressing the goto next record botton automaticaly.

I know I have to do a do loop but I dont know the syntax, can you send me the code so when I click on this botton ot will go through all the records until the end? Please

Daniboy
 
From what you've said here it sounds like you want to update every record in your data source. Scrolling through the record in a form is not the way to do it. Far better to run an update query. If your update is based on some information that maybe you enter on the form then you can still reference this in your query.

Is the above is not the case perhaps you could explain a little more about why you want to do this.
Stopher
 
It would be better to use a query, but you can fo it the following way:

First create a command button, in this case call in Command1.

Copy all of this code into the modual:

Code:
Command1_Click()
'Declare/Assign variables.
   Dim x as Integer
   x = 0
   Dim recordcountnum as Integer
'Assign recordcountnum to the number of records present.
   recordcountnum = Me.RecordsetClone.RecordCount

'Loop nextrec for the numbeer of times as there are records.
For x = 1 to recordcountnum
nextrec
'YOU WOULD NEED TO PUT YOUR UPDATE FIELD CODE HERE, BELOW THIS LINE IS AN EXAMPLE:
Me.Form.CustomerName = "Bob"
next x
End Sub
'As can be see as above, every record present in the dataset will be updated with Bob in the CustomerName field.


Sub nextrec()
On Error GoTo Err_Command32_Click

    DoCmd.GoToRecord , , acNext

Exit_Command32_Click:
    Exit Sub

Err_Command32_Click:
    MsgBox "You cannot go to the selected record!", vbCritical, "Database Error"
        Resume Exit_Command32_Click
End Sub
 

Users who are viewing this thread

Back
Top Bottom