Update All Records With Command Button?

maytime

Registered User.
Local time
Today, 07:55
Joined
Apr 1, 2008
Messages
29
I am trying to make a button for a form to update all the records it shows from its query with one command button, to avoid having to click on each record and click my "Update" button to auto-fill a field. Basically I have an 'if' statement based on whether a checkbox is Yes/No and if it is checked "Yes" then I want the auto-filled field to update.

Here is my code right now showing what the 'Update' button needs to do, but I don't know how to add to it to make it update ALL the records shown on my continuous form based on whether the 'if' statement is satisfied:

Code:
Private Sub Update_Click()

If Me.[Comp_Tag_Same] = -1 Then

Me.[Tag Number] = Me.[Component Number]

End If

End Sub
I tried to do a "RunCommand acCmdRecordsGoToNext" command in a loop but it didn't seem to work for me. Any suggestions would be much appreciated!
 
Code:
If Me.[Comp_Tag_Same] = -1 Then

Me.[Tag Number] = Me.[Component Number]

End If

Create an update query

Code:
Update [TableName] Set [Tag Number] = [Component Number] Where [Comp Tag Same] = True
 
Well I figured out how to tell it to run through my records and do the update for all of them in one command button click. I had to use the DoCmd.GoToRecord commands and create a loop for it. When it ran out of records to go to, I put in the On Error GoTo Err_End statement to eliminate any error messages.

My code now looks like:

Code:
Private Sub Update_Click()
On Error GoTo Err_End

DoCmd.GoToRecord , , acFirst

If Me.[Comp_Tag_Same] = -1 Then

Me.[Tag Number] = Me.[Component Number]

End If

Next_Record:

DoCmd.GoToRecord , , acNext

If Me.[Comp_Tag_Same] = -1 Then

Me.[Tag Number] = Me.[Component Number]

End If

GoTo Next_Record

Err_End:
Exit Sub

End Sub
 
Last edited:

Users who are viewing this thread

Back
Top Bottom