VBA to update recordset

StuLux

Well-known Member
Joined
Sep 14, 2005
Messages
679
Office Version
  1. 365
Platform
  1. Windows
This is probably fairly basic but despite searching I can't find some simple advice on how to do the following:

I have some VBA code that runs when a form is opened at a specific record - the code updates a field on the form depending on date values in other fields. This code works fine.

What I want to do now is to run the same code but for all records in the table and I don't understand how I write the code to step through each record. I envisage running this code when the database is opened to make sure all records are always up to date, if there is a better way of doing this then please let me know.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
If you're updating the same field based on the same rules, use an Update query instead. It's much more efficient than looping through a recordset.

If you haven't used Update queries before, check out this tutorial.

Denis
 
Upvote 0
Denis

Thanks for the reply. I had thought about using an update query but the code I have built is very lengthy - thsi is it in VBA:

Code:
Public Function UpdatetxtCurrentPhase()
        If IsDate(Me.txtControl_Finish) _
        And (Me.txtControl_Finish) >= Nz(Me.txtImprove_Finish) _
        And (Me.txtControl_Finish) >= Nz(Me.txtAnalyse_Finish) _
        And (Me.txtControl_Finish) >= Nz(Me.txtMeasure_Finish) _
        And (Me.txtControl_Finish) >= Nz(Me.txtDefine_Finish) _
        And Nz(Me.txtImprove_Finish) < Now() _
        And Nz(Me.txtAnalyse_Finish) < Now() _
        And Nz(Me.txtMeasure_Finish) < Now() _
        And Nz(Me.txtDefine_Finish) < Now() Then
        Me.txtCurrentPhase = "Control"
                Else
                If IsDate(Me.txtImprove_Finish) _
                And (Me.txtImprove_Finish) >= Nz(Me.txtAnalyse_Finish) _
                And (Me.txtImprove_Finish) >= Nz(Me.txtMeasure_Finish) _
                And (Me.txtImprove_Finish) >= Nz(Me.txtDefine_Finish) _
                And Nz(Me.txtAnalyse_Finish) < Now() _
                And Nz(Me.txtMeasure_Finish) < Now() _
                And Nz(Me.txtDefine_Finish) < Now() Then
                Me.txtCurrentPhase = "Improve"
                        Else
                        If IsDate(Me.txtAnalyse_Finish) _
                        And (Me.txtAnalyse_Finish) >= Nz(Me.txtMeasure_Finish) _
                        And (Me.txtAnalyse_Finish) >= Nz(Me.txtDefine_Finish) _
                        And Nz(Me.txtMeasure_Finish) < Now() _
                        And Nz(Me.txtDefine_Finish) < Now() Then
                        Me.txtCurrentPhase = "Analyse"
                                Else
                                If IsDate(Me.txtMeasure_Finish) _
                                And (Me.txtMeasure_Finish) >= Nz(Me.txtDefine_Finish) _
                                And Nz(Me.txtDefine_Finish) < Now() Then
                                Me.txtCurrentPhase = "Measure"
                                        Else
                                        If (Me.txtDefineStart) > Now Or IsNull(Me.txtDefineStart) Then
                                        Me.txtCurrentPhase = "Not Started"
                                        Else
                                        Me.txtCurrentPhase = "Define"
                                        End If
                                End If
                        End If
                End If
        End If

I thought this would be quite difficult to replicate in a query - do you still think this is the best way?
 
Upvote 0
If you rewrite the code to leave out the Me. notation, and replace Me.txtCurrentPhase = with UpdatetxtCurrentPhase, you should be able to call the function directly in the Update To row of the query grid.
Try making a copy of the function and altering it. That way, if it misbehaves you haven't lost anything. I can then show you how to loop through the recordset if you have to go that route.

Denis

Denis
 
Upvote 0
I hadn't relaised you could call a function in an update query! - obvious really but I had assumed you could only use Access built in functions. Thanks a lot for your help (again)!
 
Upvote 0

Forum statistics

Threads
1,215,523
Messages
6,125,318
Members
449,218
Latest member
Excel Master

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top