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

How do you late bind a 'For each (control) in Form?

Status
Not open for further replies.

jofarrell

Programmer
Mar 21, 2001
178
US
I have a loop that I use in my standard module that clears a forms text boxes. It works fine in other programs I have deployed. But at the company I work with now I am getting error 458 Automation not supported in VB.

Is there a way to late bind the controls to do what I want it to do?

Public Sub ClearText(frm As Form)
'Used to clear all text and pertinent labels by passing in the form
'name and tagging labels that are to be cleared

Dim ctl As Control

For Each ctl In frm.Controls
If ctl.Tag = False Then
GoTo Skip
ElseIf TypeOf ctl Is TextBox Then
ctl.Text = ""
If ctl.Tag = "tcl" Then
ctl.Visible = False
End If
End If
If TypeOf ctl Is Label And ctl.Tag = "clf" Then
ctl.Caption = ""
End If
If TypeOf ctl Is CheckBox And ctl.Tag = "ccb" Then
ctl.Value = vbUnchecked
ctl.Visible = False
ElseIf TypeOf ctl Is CheckBox And ctl.Tag = "cckb" Then
blnClearForm = True
blnNoEvent = True
ctl.Value = vbUnchecked
blnClearForm = False
End If
Skip:
Next ctl

End Sub

Any help would be greatly appreciated.

Joanne
 
Dim ctl As object.

You can replace GOTO with a Use DO / EXIT DO: LOOP code container or in this case just leave out the GOTO.
Code:
Do
    If ctl.Tag = False Then
        exit do
    ElseIf TypeOf ctl Is TextBox Then
        ctl.Text = ""
        If ctl.Tag = "tcl" Then
            ctl.Visible = False
        End If
    End If
    If TypeOf ctl Is Label And ctl.Tag = "clf" Then
        ctl.Caption = ""
    End If
    If TypeOf ctl Is CheckBox And ctl.Tag = "ccb" Then
        ctl.Value = vbUnchecked
        ctl.Visible = False
    ElseIf TypeOf ctl Is CheckBox And ctl.Tag = "cckb" Then
        blnClearForm = True
        blnNoEvent = True
        ctl.Value = vbUnchecked
        blnClearForm = False
    End If
Exit Do: Loop     ' LOOP never executed.

 
Thank you very much JohnYingling, I will let you know if this solved my problem .. if so ..you made me very happy :):)

Joanne
 
So do you have a way around

For Each ctl In frm.Controls

because if I dont tell it what ctl is even though I dimmed it as a control it says variable or with block not set.

And why does the above that I did work on some installation packages and not others? Just frustrating to develop have it run and then you are unable to distribute it because it says VB does not support automation..

Sorry to complain there :)

Joanne



 
When I do For Each control, I make sure to isolate properties to varaiable before any if statements. Then I can use On Error to avouid controls that do not have the property e.g. Timer does not have a Width. Maybe, not all controls have a Tag property. Remember, all expressions in an IF statement are evaluated even though the first is false. Also, I do not like the Ctl.Tag = false because it may cause every Tag to be converted to boolean and that will fail on any Tag that does not contain True or False.
I think your trouble is not For Each but what properties you are rying to access on every control.
Code:
'* Example Isolation
Dim ctl as object
Dim varTag as variant

Dim lngErr as long

For Each ctl in Form.Controls
    on error resume next 
        vartag = ctl.tag
        lngErr = Err.Number
    on error goto 0
    If lngErr <> 0 then
    elseIf varTag = false then ' I SUSPECT THIS STATEMENT
    ElseIf TypeOf ctl Is TextBox Then
        ctl.Text = &quot;&quot;
        If vartag = &quot;tcl&quot; Then
            ctl.Visible = False
        End If
    End If
    If TypeOf ctl Is Label And varTag = &quot;clf&quot; Then
        ctl.Caption = &quot;&quot;
    End If
    If TypeOf ctl Is CheckBox And varTag = &quot;ccb&quot; Then
        ctl.Value = vbUnchecked
        ctl.Visible = False
    ElseIf TypeOf ctl Is CheckBox And varTag = &quot;cckb&quot; Then
        blnClearForm = True
        blnNoEvent = True
        ctl.Value = vbUnchecked
        blnClearForm = False
    End If
Next
 
I hope I haven't misled you, my problem doesnt occur from the code I have it works fine. I use a For EACH ctl in frms control in both my bas ClearText and FormResize prodecdures. The problem occurs when I deploy the application and get an Automation not supported error. I am trying to combat this this (After running dcom98, vb6cli.exe, mdac_typ.exe) and anything else I can find to allow myself to distribute the application. On a development computer the exe runs fine ... it is only on a client computer I am facing either dll or autmation errors. These procedures have worked fine in other programs I have distributed but for some reason ( and I am grasping at straws) I am thinking it may be the For EACH LOOP with controls that is causing this.

I have another post as Error 458 - if you want to see more clearly what I am speaking of. Basically now I am just trying to replace my nice neat reusable code with no &quot;For EACH&quot; loops for controls.

Thank you for your efforts ... I am looking at your form resize code and its a fair bit longer than my own, if you would to se what I have just let me know. (heh then again mine seems to only like certain platforms such as NT a lot better than Win98)

Joanne
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top