views:

70

answers:

3

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

I was wondering what kind of VBA would 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

probably pretty simple to do....although the more reason i should learn how!

EDIT: sooo I should have been more descriptive about this....apologies.

because of the above naming covention, I am looking to iterate through these text boxes so that I can preform something with each. So what each of these 10 text boxes actually represent is numeric values that each have a sql statement behind them on the forms load 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 bascially 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 wanting to use this for the 'resulting' text boxes. I want to loop through the first ten, preform the little easy calc of:

 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.

Thanks Justin

+1  A: 

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.

BradC
As a note, in Access VBA, you want to use .Value, not .text (.text is used in a different context then VB, and the control has to have the focus for .text to be useable). So, .Value = "some string" is the correct syntax
Albert D. Kallal
+2  A: 

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 Open event:

Private Sub Form_Open(Cancel As Integer)
    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.

Rather than computing a result for me.txt_001_result and then assigning that value to the text box, can't you just set 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?

Update: Can you build on this procedure?

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
HansUp
so i understand what you are saying, but even though I didn't mention it above, i think i need the vba to account for zeros right? also yes you're right, rather than going overboard with descriptions, I am looking to show the result of dividing one text box's value, by another's, in the 'result text' box. so there are acutally way more than 10, but its the exact same idea. and these text boxes are filled with numbers based on another choice that the user selected, based on a choice before that, etc. i use these 'dashboards' all the time because that is what people want ALL THE TIME...( i also
Justin
(cont)..realize that this isn't the intended idea of access but for what i need to get done, it works). so i was looking for a loop that would increment the number of the text box names, in order to perform the bassic calc for each, provided that they contain a value in the first place.
Justin
@Justin I still don't understand enough about the context, but you don't necessarily need VBA code to deal with zero's. Maybe a VBA function in a textbox's control source expression: IIf(something = 0, "deal with zero", "deal with non-zero")
HansUp
@Justin Despite my confusion, did the TextBoxNames procedure give you at least a piece of what you wanted?
HansUp
@Justin I am curious to see your approach for that dashboard form. If you would be willing to send me a copy of your db, I'll give you my email address. Or could you upload it somewhere we can get at it and give us a link?
HansUp
@Hans...yeah I apologize. I think this may boil down to my lack of understand how to explain it. And yes the first example you gave helped me tremendously. So i started out just thinking that there must be some loop to go through all the text boxes that are applicable to the equation, so that I did not have to type of the equation for every set of relative text boxes. i suppose the idea was looping through the 'numeric' value of the text box. so the loop would do this first: txt_001_result = txt_001_value / txt_001_calc if all both are not null/empty, an
Justin
@Hans (cont)....d then do this: txt_002_result = txt_002_value / txt_002_calc, and etc through all text boxes that hold a value of that specific name.
Justin
@Justin Can you use the MyTextBoxValues procedure and make the Me.Controls assignment conditional on: `If Nz(Me.Controls("strCalcControl")) <> 0`
HansUp
@HanUps.....yes that helped a lot....I actually figured out how to do is based on the examples....I was just trying to put two ideas into one loop, which may be possible, but once I used two it was quite easy actually. thanks Hans!
Justin
@Justin. Good! I would still appreciate an opportunity to study your approach to dashboard forms, if you're willing. :-)
HansUp
A: 

I can't entirely understand why you need to do what you're doing, but I've had forms like that where I had an unbound form that I wanted to display an arbitrary number of fields, so I can see it. If you're walking the collection of controls only in the form's OnOpen event, that's fine. But if you're doing it in the OnCurrent of a bound form, or multiple times in an unbound form, you might consider a long post of mine on using custom collections to manage groups of controls.

David-W-Fenton