Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access VBA - How would I have a loop in VBA that allows me to loop through control names

I have about 10 text boxes on a form that are actually used for display not entry. They are are named txt_001_Name, txt_002_Title, etc..what kind of loop is used for this.

What kind of VBA should I use to actually loop through the names of the text boxes? So if I was to debug.print it would look like:

txt_001_Title
txt_002_Title
txt_003_Title

This is probably pretty simple to do - all the more reason that I should learn how!

EDIT: Sorry, I should have been more descriptive about this.

Because of the above naming convention, I am looking to iterate through these text boxes so that I can perform something with each. What each of these 10 text boxes actually represent is numeric values, each having a SQL statement behind them in the form's onload event. I also have another set of ten that hold numeric values that are much more static, and finally another ten that use an expression to simply divide each of the first ten, against the relative "second" ten, and the value ends up in the relative 3. So basically it ends up looking like a dashboard table.

'first ten'       'second ten'     'resulting ten'
---------------------------------------------------
txt_001_value      txt_001_calc     txt_001_result
txt_002_value      txt_002_calc     txt_002_result

etc.

So I actually want to use this for the 'resulting' text boxes. I want to loop through the first ten and perform this easy calculation:

 me.txt_001_result = me.txt_001_value / me.txt_001_calc     

All the naming conventions "match up", so I can manually type out the 10 lines of the above for this, but I am sure there is a better way (loop through this), and I should probably learn it.

like image 763
Justin Avatar asked Oct 12 '10 16:10

Justin


People also ask

How do you do a loop in access?

When you set up loops for an Access data file, do the following: Place all header data in one (primary) table. Place all line item data related to the header in a separate table within the same database. Select the line item table when you add the loop in the Mapper.


2 Answers

You can list the names of textbox controls with a simple procedure like this:

Public Sub TextBoxNames(ByRef pfrm As Form)
    Dim ctl As Control
    For Each ctl In pfrm.Controls
        If ctl.ControlType = acTextBox Then
            Debug.Print ctl.Name
        End If
    Next ctl
    Set ctl = Nothing
End Sub

You could call it from the form's Load event:

Private Sub Form_Load()
    TextBoxNames Me
End Sub

However, I don't understand what you're trying to accomplish. I realize you want to do something with ctl.Name other than Debug.Print, but I don't know what that is.

Rather than computing a result for me.txt_001_result and then assigning that value to the text box, consider setting the control source for txt_001_result to txt_001_value / txt_001_calc and let Access put the proper value into txt_001_result for you.

In response to your comments, I'll suggest this procedure as a starting point for you to build upon:

Public Sub MyTextBoxValues()
    Const cintLastTextBoxNum As Integer = 10
    Dim i As Integer
    Dim strValueControl As String
    Dim strCalcControl As String
    Dim strResultControl As String
    Dim strPrefix As String

    For i = 1 To cintLastTextBoxNum
        strPrefix = "txt_" & Format(i, "000")
        'txt_001_value      txt_001_calc     txt_001_result '
        strValueControl = strPrefix & "_value"
        strCalcControl = strPrefix & "_calc"
        strResultControl = strPrefix & "_result"
        'me.txt_001_result = me.txt_001_value / me.txt_001_calc '
        'Debug.Print strResultControl, strValueControl, strCalcControl '
        Me.Controls(strResultControl) = Me.Controls(strValueControl) / _
            Me.Controls(strCalcControl)
    Next i
End Sub
like image 189
HansUp Avatar answered Sep 24 '22 02:09

HansUp


I prefer to use a FOR EACH to iterate through the controls collection of whatever the textboxes are on (either the form itself or a panel control)

dim myBox as Textbox
For each myBox in myForm
    myBox.Text = "hello"
Next

Also means you can make custom groups (by putting them all on the same container). Note that if you have other controls, you might need a typecheck in there (IF TYPEOF(myBox) = "TextBox" THEN ...)

You could also do it like:

dim i as integer
For i = 1 to 10
     myForm.Controls("txt_00" & i & "_Title").Text = "hello"
Next i

I definitely prefer the For Each, though.

like image 32
BradC Avatar answered Sep 20 '22 02:09

BradC