Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

refering to an object with a string 1

Status
Not open for further replies.

sfenx

Programmer
Feb 23, 2001
81
BE
How can I refer to an object, such as a textbox, when all I've got to refer to is a string containing its name?

exemple: 5 textboxes (txt1... txt5) (no array)
I want to use the textboxes like this:
Code:
dim tmp as integer
for i = 1 to 5
  tmp = tmp + txti
next

Sfenx
 
Sfenx

You could do something like.

For x = 0 to Form1.ControlCount -1
if left(Form1.Controls(x),3) = "txt" then
Tmp = Tmp & Form1.Controls(x)
EndIf
Next

This is only pseudo code but should work.

Dazz
Live2Give (Like Harry Potter's schooling, I can't do anything without a spell checker)
 
Public Function SumOfText(ByRef oForm As Form) As Double
Dim oControl As Control
Dim dblSum As Double

For Each oControl In oForm.Controls
If TypeOf oControl Is TextBox Then
dblSum = dblSum + CDbl(oControl.Text)
End If
Next

SumOfText = dblSum

End Function

NB.... Assumes that validation on the textboxes has been done and the fields are numeric.
 
Nice one John. Saves having to run through every control on the form.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top