Hide Blank Fields

G

Guest

Some time ago, someone in this forum suggested the following code to hide
controls which are blank:

Dim ctl As Control

For Each ctl In Me.GroupHeader0.Controls
Select Case Left(ctl.Name, 3)
Case "txt", "cbo"
If IsNull(ctl.Value) Then
ctl.Visible = False
Else
ctl.Visible = True
End If
Case Else
'Ignore: wrong type of control
End Select
Next ctl

However, I'm getting an error which says:

"Run-time error: '438': Object doesn't support this property or method.

Am I missing something here? Thanks.
ck
 
D

Duane Hookom

I expect that you have a label with a name that begins with "txt". A label
control will not have a Value property so this would generate the error you
see.
 
G

Guest

Thanks Duane. I didn't realize labels could begin with txt until now. I've
thought they are always named Label45 or something like this.
ck
 
D

Duane Hookom

If you are going to be writing code that references control names, then find
and use a good naming convention.
 
D

Douglas J. Steele

Much as I agree with that, Duane, I think in this case it would probably
make more sense to look at the type of the control rather than relying on
its name.

Dim ctl As Control

For Each ctl In Me.GroupHeader0.Controls
If TypeOf ctl Is TextBox or TypeOf ctl Is ComboBox Then
If IsNull(ctl.Value) Then
ctl.Visible = False
Else
ctl.Visible = True
End If
End If
Next ctl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top