repeat code with each field ms access (1 Viewer)

Safari

Member
Local time
Today, 04:21
Joined
Jun 14, 2021
Messages
95
hi all

i want to know how to repeat code with each field in column

for example when i use if it only apply for the 1st record only
i want to apply it for all records

ex :

if me.cash = 100 then
msgbox"good"
end if

how to repeat this code in each filed in recod

thanks
 

GPGeorge

Grover Park George
Local time
Yesterday, 19:21
Joined
Nov 25, 2004
Messages
1,873
Well, technically, an update query would do this.

Update tblYourTableNameGoesHere
SET YourValueJudgementFieldNameGoesHere = "Good"
WHERE tblYourTableNameGoesHere.Cash = 100

However, that actually indicates a problem with table design to do that, so let's get a clearer description of the table itself and the basis for wanting to do this so we can help you correct the table design.
 

GPGeorge

Grover Park George
Local time
Yesterday, 19:21
Joined
Nov 25, 2004
Messages
1,873
On the other hand, you asked a different question, as well.

"...how to repeat this code in each filed in recod[sic]"

And that is another table design problem on the face of it. Again, details about the table and the process are important to understanding how to correct the table design.
 

Safari

Member
Local time
Today, 04:21
Joined
Jun 14, 2021
Messages
95
is it possible to make it with :

do while ?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 21:21
Joined
Feb 28, 2001
Messages
27,189
GPGeorge is absolutely correct.

However, it is possible that you have a reason for thinking VBA is the way to go. Tell us the type of database structure you are using including where you thought you might want to use that code snippet you showed us. There is always more than one way to skin a cat, so it helps to know which kind of cat we are skinning today.
 

Eugene-LS

Registered User.
Local time
Today, 05:21
Joined
Dec 7, 2018
Messages
481
how to repeat this code in each filed in recod
Code:
Dim ctrl As Control

    For Each ctrl In Me.Controls
        Select Case ctrl.ControlType
            Case acTextBox, acComboBox ', acCheckBox
                'Debug.Print ctrl.Name & " = "; ctrl.Value
               if ctrl.Value = 100 then
                    msgbox"good"
               end if
            Case Else
                ' ...
            End Select
    Next ctrl
 

GPGeorge

Grover Park George
Local time
Yesterday, 19:21
Joined
Nov 25, 2004
Messages
1,873
Code:
Dim ctrl As Control

    For Each ctrl In Me.Controls
        Select Case ctrl.ControlType
            Case acTextBox, acComboBox ', acCheckBox
                'Debug.Print ctrl.Name & " = "; ctrl.Value
               if ctrl.Value = 100 then
                    msgbox"good"
               end if
            Case Else
                ' ...
            End Select
    Next ctrl
Over the years, I've learned the hard way that providing answers that technically are accurate is less valuable than providing answers that address the real, unspoken, problem.
 

Eugene-LS

Registered User.
Local time
Today, 05:21
Joined
Dec 7, 2018
Messages
481
Over the years, I've learned the hard way that providing answers that technically are accurate is less valuable than providing answers ...
How thoughtfully you wrote! :unsure:
 

Safari

Member
Local time
Today, 04:21
Joined
Jun 14, 2021
Messages
95
if no 1 is more than 300
the gray with red line is unlock .. its ok

when i move to the no 2 the 2nd record .. filed it is not apply the code
and only work with no 1
 

Attachments

  • 1234.png
    1234.png
    130.7 KB · Views: 87

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 21:21
Joined
Feb 28, 2001
Messages
27,189
This problem comes up now and then. Do a search on this subject: Access continuous form current record

I have found a few articles but this is not something I have used often.
 

Safari

Member
Local time
Today, 04:21
Joined
Jun 14, 2021
Messages
95
This problem comes up now and then. Do a search on this subject: Access continuous form current record

I have found a few articles but this is not something I have used often.
thanks
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 22:21
Joined
Feb 19, 2002
Messages
43,293
@Safari, either use conditional formatting or put your code into the Current event of the subform so it runs for each new record.

I'm pretty sure you don't actually want to pop up a message box for each record so you need to tell us EXACTLY what you are trying to make happen. Keep in mind that a continuous form or form in DS view is simply a single record form that you can see multiple instances of at one time. They do not work any differently than single view forms. Code run in the form affects the CURRENT record. But, with the multiple instance views, Access keeps only ONE set of properties so if you have unbound controls or if you set properties with code, ALL visible records will exhibit the same properties. That is what Conditional Formatting was intended to get around.

For example, if you want to highlight a record that has an error, you might have the following code in the form's Current event.
Code:
If IsNull(Me.SomeField) = true then
   Me.txtSomeField.BackColor = vbRed
Else
    Me.txtSomeField.BackColor = Me.BackColor
End If
So the form opens and the first record is fine. You click into the third record where you see an error and all instances of that field go red. That is because Access keeps only one instance of the form properties for use by ALL visible rows. This is a case where Conditional Formatting will solve the problem.

If you want to lock or unlock. Then you can do that with code because you do not care at all what the state of the other records are. All you care about is the state of the CURRENT record. If it is locked, you can't update it but if it is unlocked, you can.

Here's a picture of how Conditional Formatting works.
PB_ProjectStatus.JPG
 
Last edited:

Safari

Member
Local time
Today, 04:21
Joined
Jun 14, 2021
Messages
95
@Safari, either use conditional formatting or put your code into the Current event of the subform so it runs for each new record.

I'm pretty sure you don't actually want to pop up a message box for each record so you need to tell us EXACTLY what you are trying to make happen. Keep in mind that a continuous form or form in DS view is simply a single record form that you can see multiple instances of at one time. They do not work any differently than single view forms. Code run in the form affects the CURRENT record. But, with the multiple instance views, Access keeps only ONE set of properties so if you have unbound controls or if you set properties with code, ALL visible records will exhibit the same properties. That is what Conditional Formatting was intended to get around.

For example, if you want to highlight a record that has an error, you might have the following code in the form's Current event.
Code:
If IsNull(Me.SomeField) = true then
   Me.txtSomeField.BackColor = vbRed
Else
    Me.txtSomeField.BackColor = Me.BackColor
End If
So the form opens and the first record is fine. You click into the third record where you see an error and all instances of that field go red. That is because Access keeps only one instance of the form properties for use by ALL visible rows. This is a case where Conditional Formatting will solve the problem.

If you want to lock or unlock. Then you can do that with code because you do not care at all what the state of the other records are. All you care about is the state of the CURRENT record. If it is locked, you can't update it but if it is unlocked, you can.

Here's a picture of how Conditional Formatting works.
View attachment 98790
thank you very much
 

Users who are viewing this thread

Top Bottom