Home > Software design >  Access VBA-Loop through sequential textfields and do if not empty
Access VBA-Loop through sequential textfields and do if not empty

Time:06-30

I need some help to optimize an access form for data entry. On my form I have many different fields amongst them are 7 text fields. I would like to loop through 6 of these which I have named sequentially and do something if they are not empty. I am using access VBA

Tried to show in pseudo code:

for i = 1 to 6
    if hardness_measurement_i > 0 then
        <do something>
    else
        <do nothing>
    end if
next i

My problem is formulating this part of the loop: "hardness_measurement_i"

Is there a good way to loop through form fields named sequentially?

CodePudding user response:

Try to get the contol by name using the Controls collection property of the form.

If Me.Controls("hardness_measurement_" & i).Value > 0 Then
    '...

To check if the textbox is empty, you can call the IsNull() method passing the control's Value.

If IsNull(Me.Controls("hardness_measurement_" & i).Value) Then
    '...
  • Related