Norm
8/22/2010 11:02:00 PM
It happens that ralph formulated :
> On Sun, 22 Aug 2010 13:29:00 -0700, Norm <NormF4@Spoof.com> wrote:
>
>> Hi,
>>
>> I am using the following code to set all controls on a form to visible,
>> so that I can set only the ones I want to see later in a routine, as
>> this seems the quickest and easiest way to do it when I only want to
>> see a few controls.
>>
>> Code:
>>
>> Dim ctl as Control
>>
>> For Each ctl In Me.Controls
>> If Not TypeOf ctl Is Menu Then
>> ctl.Visible = False
>> End If
>> Next
>>
>> The problem I don't understand is that the line ctl.Visible = False
>> generates and error that the object does not support this property or
>> method. If I put On Error Resume Next in the code it will contine and
>> will make all the controls not visible.
>>
>> Is there some other way of coding this to either avoid the error or
>> some way of just trapping the error as it happens, without using On
>> Error Resume Next?
>>
>
> You could place a "Select Case" or a series of "If...End If"s for each
> type of control and only call a Visible method for a control type that
> supports it.
>
> However, there is nothing wrong with using an Error Statement in this
> case - and Error Resume Next is probably the most elegant way to
> handle it. Think of VB's 'structured' Error Handling as an early form
> of a "Try ... Catch" block. The block is defined by each On Error
> statement, and you can nest them.
>
> Remember to reset the error handler to something else or 'back-again'
> coming out of the loop, if you have additional code in the same
> routine, and Resume/Next would not be an appropriate response.
>
>> I have tried googling, but have not found anything explaining the
>> problem that I am having. Probably just not searching for the right
>> phrase. ;o)
>
> Not that it makes that much difference, but what you are using is not
> a "Control Array", it is the "Forms.Control Collection".
>
> -ralph
Thanks for all the suggestions, I will separate the types of controls
in the loop and see if I can find which one is causing the error. If
nothing else I can put On Error Resume Next right before the loop,
Err.clear after the loop and an On Error GoTo statement after the loop.
It is probably not the best way to work, but I try to get my code to
work with no error trapping first, it helps point out all my mistakes.
:D
Norm