Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

make field name in code variable for vb code loop

I have a form with 20 fields on it with the following names:  COL_1, COL_2...COL_20  
I need to check each one of these dropdown fields to see if a predefined text has been selected.  Problem is I can't figure out how to make the number part of the field name a variable or what to Dim it as.

    Dim ColumnNum As ??????, intCount As Integer
    intCount = 1
    Do Until intCount = 20
        ColumnNum = COL_ & intCount
        If ColumnNum = "CompToRcvd" Then
            MsgBox "yes"
        Else
            MsgBox "no"
        End If
        intCount = intCount + 1
    Loop
0
woodan1
Asked:
woodan1
  • 2
1 Solution
 
Steve BinkCommented:
Are these fields in the underlying recordset, or are they controls on the form?  It makes a difference.  Here's a basic template:

For x = 1 to 20
    Me.Controls("COL_" & x) '<--- references a control COL_x
    Me.Recordset.Fields("COL_" & x)  '<--- references a field in the underlying data
Next
0
 
McDougallCommented:
Dim c As Control
For Each c In Me.Controls
If c.ControlType = acTextBox Then
    MsgBox c.name & " " & c.Value
End If
Next c

if combo use:
acComboBox

if list box
acListBox
0
 
woodan1Author Commented:
That's the answer.  Thanks.

    Dim x As Integer
    x = 1
    For x = 1 To 20
        If Me.Controls("COL_" & x) = "CompToRcvd" Then
            Debug.Print "Yes " & "COL_" & x
        Else
            Debug.Print "No " & "COL_" & x
        End If
    Next
0
 
woodan1Author Commented:
Sorry McDougall, didn't catch your response until after I accepted.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now