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

Cycle through objects in Form - how? 1

Status
Not open for further replies.

MikeBronner

Programmer
May 9, 2001
756
US
I am working on a form that contains many text- and comboboxes. Instead of hardcoding each of these individually, i would like to use a loop to cycle through all of them.

I think I have seen this done before, but I forgot where and how. Any ideas? Thanks! Best Regards and many Thanks!
Michael G. Bronner X-)

"The intermediate stage between socialism and capitalism is alcoholism." Norman Brenner

 
If you want to cycle through then
Code:
Dim ctlW as Control
For each ctlW in Form.Controls
    if ctlw is TextBox then
    ....
    elseif ctlw is combobox
    ....
    elseif ctlw is listbox
    end if
Next
 
Thanks!

I was attempting something similar, but didn't quite get it to work. Best Regards and many Thanks!
Michael G. Bronner X-)

"The intermediate stage between socialism and capitalism is alcoholism." Norman Brenner

 
This is strange, for some reason i can't get this darn thing to work. here's what I'm trying to do:

I want to bind textBoxes and ComboBoxes to recordfields. I was trying do this via a loop as described above, but when I'm done and do adoSet.Update, I get the error that it can't add an empty record. So I'm figuring that the fields are not correctly bound. Here is how I was doing it:

Dim ctlObj As Object
Dim ctltext As TextBox
Dim ctlCmb As ComboBox
Dim i As Integer

i = 0
For each ctlObj in Me.Controls
If (ctlObj is ctlTxt) or (ctlObj is ctlCmb) Then
If (ctlObj.TabIndex = i) And (adoSet.Fields(i) <> &quot;&quot;) Then
ctlObj.Text = adoSet.Fields(i)
Set ctlObj.DataSource = adoSet
End If
End If
Next ctlObj

Any ideas what is wrong here? Thanks! Best Regards and many Thanks!
Michael G. Bronner X-)

&quot;The intermediate stage between socialism and capitalism is alcoholism.&quot; Norman Brenner

 
Use Typeof and use object types e.g. textbox, combobox etc. The object types are listed at the top of the property sheet for the object.
Code:
For each ctlObj in Me.Controls
    If (typeof ctlObj is textbox) or (typeof ctlObj is combobox) Then
        If (ctlObj.TabIndex = i) And (adoSet.Fields(i) <> &quot;&quot;) Then
            ctlObj.Text = adoSet.Fields(i)
            Set ctlObj.DataSource = adoSet
        End If
    End If
Next ctlObj
 
Thanks! Although I didn't get the loop to do what I want yet, I am finally getting it to write to the database! Best Regards and many Thanks!
Michael G. Bronner X-)

&quot;Beer makes you feel the way you ought to feel with out beer.&quot; Henry Lawson

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top