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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through Controls by name

Status
Not open for further replies.

icodian

IS-IT--Management
Aug 28, 2001
74
US
I have an old program that I am working to upgrade. The old language was based on VB but was proprietary from what I understand. I am now using VBScript built into the application I am customizing.

In the old app, I had the following loop written to loop through 12 controls and assign values.

for i = 1 to 12
setpropertyof "txtAlloc" & cStr(i), "Text", ""
next i

The names of the 12 controls were 'txtAlloc1,' 'txtAlloc2,' 'txtAlloc3,' etc....

This would loop through them all and set the Text property to blank for all of them. Simple.

Moving to VBScript, I need to accomplish the same thing. I don't have the option of using a control array in the app I am using. Also, I have to do this sort of thing soooo many times, I can't have 12 IF statements every time I need to do it.

Does anyone have any suggestions on the syntax to perform the same functionality in VBScript?

Thanks for any help!
 
I'm not sure if this is what you need....
Code:
For i = 1 to XX
   [Forms]![frmName].Controls("txtAlloc" & i).Value = vbNullString
Next

 
As a minor modification, I'd suggest dropping the square brackets and the ! (as these have a very specific meaning that was essentially inherited from Access Basic), to give the more usual

Forms.frmName.<etc>

 
Dim Ctrl As Control

For Each Ctrl In Controls
If TypeOf Ctrl Is TextBox Then
Ctrl.Text = ""
End If
Next

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
Thanks for the posts guys. I am getting an invalid property error when attempting the following statement:

For i = 1 to XX
Forms.frmName.Controls("txtAlloc" & i).Value = ""
Next

I used vbNullString initially but it errored out there first... Perhaps the variation on VBScript I'm using does not support this functionality.
 
Are you using Visual Basic Scripting, or Visual Basic 6 - the programming language?

Try changing .value for .text
 
I am using VBScript.

The list that appears upon hitting the dot after Controls("txtAlloc" & i) gives me a list of controls on the form.

To clarify that, I type Form.frmName.Controls("txtAlloc" & i).

The list of available properties appears and all of the form controls are listed after hitting the dot. There is no Value or Text property available. It's as if it doesn't recognize the string in the () as being the control name and is expecting me to select it...hmmm..

I appreciate you guys responses. Thanks for trying to help out. :)

 
This sounds eerily like you are working with SalesLogix. If so I should warn you that SLX Scripting is not true VBScripting.

If I were to do this in SLX (which doesn't have control arrays or collections) I would have to change each control value line by line (unfortunately)
 
It should be noted, however, if you are using SalesLogix there are other ways around the problem, like calling the legacy code to do this from the newer vb code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top