compile error

G

Guest

I have code in the AfterUpdate event of a control with an error handler.
when I update the control, I get a "compile error: label not defined". The
Private Sub MileageDescription_AfterUpdate() line is highlighted in yellow
and the
Resume Exit_MileageDescription_AfterUpdate line is highlighted in blue.
I have compared this to other error handlers and haven't been able to spot
anything wrong. If I comment the Resume Exit_MileageDescription_AfterUpdate
line, the rest of the code works as it should so I don't know why the error
line is even coming into play. Here is the code I have. Could someone tell
me what my problem is?
Private Sub MileageDescription_AfterUpdate()
On Error GoTo Err_MileageDescription_AfterUpdate
If Me.MileageDescription = "State Line" Then
Me.State_Cover.Visible = False
Me.State.Enabled = True
Me.State.SetFocus
Me.Gallons.Enabled = False
Me.Gallons.Visible = False
Me.Gallons_Label.Visible = False
Me.Amount.Enabled = False
Me.Amount.Visible = False
Me.Amount_Label.Visible = False
Me.StopPurpose.Enabled = False
Me.StopPurpose.Visible = False
Me.StopPurpose_Label.Visible = False
Me.Location.Enabled = False
Me.Location.Visible = False
Me.Location_Label.Visible = False

ElseIf Me.MileageDescription = "Fuel" Then
Me.State_Cover.Visible = True
Me.State.Enabled = False
Me.Gallons.Enabled = True
Me.Gallons.Visible = True
Me.Gallons_Label.Visible = True
Me.Gallons.SetFocus
Me.Amount.Enabled = True
Me.Amount.Visible = True
Me.Amount_Label.Visible = True
Me.StopPurpose.Enabled = False
Me.StopPurpose.Visible = False
Me.StopPurpose_Label.Visible = False
Me.Location.Enabled = False
Me.Location.Visible = False
Me.Location_Label.Visible = False

ElseIf Me.MileageDescription = "Stop" Then
Me.State_Cover.Visible = True
Me.State.Enabled = False
Me.Gallons.Enabled = False
Me.Gallons.Visible = False
Me.Gallons_Label.Visible = False
Me.Amount.Enabled = False
Me.Amount.Visible = False
Me.Amount_Label.Visible = False
Me.StopPurpose.Enabled = True
Me.StopPurpose.Visible = True
Me.StopPurpose_Label.Visible = True
Me.StopPurpose.SetFocus
Me.Location.Enabled = True
Me.Location.Visible = True
Me.Location_Label.Visible = True

ElseIf Me.MileageDescription = "Trip End" Then
Me.State_Cover.Visible = True
Me.State.Enabled = False
Me.Gallons.Enabled = False
Me.Gallons.Visible = False
Me.Gallons_Label.Visible = False
Me.Amount.Enabled = False
Me.Amount.Visible = False
Me.Amount_Label.Visible = False
Me.StopPurpose.Enabled = False
Me.StopPurpose.Visible = False
Me.StopPurpose_Label.Visible = False
Me.Location.Enabled = True
Me.Location.Visible = True
Me.Location_Label.Visible = True

End If
Exit Sub

Err_MileageDescription_AfterUpdate:
MsgBox Err.Description
'Resume Exit_MileageDescription_AfterUpdate
End Sub
I realize now this could be done more efficiently probably with SELECT CASE
but it's working, except for the error part, and I've been hesitant to mess
with it
 
T

tina

first, your error handler needs to be inside the sub, not below it. second,
you don't have the label "Exit_Mileage..." in your sub, so the Resume
command has no place to "go to". look at the last 6 lines of your sub
Me.Location.Enabled = True
Me.Location.Visible = True
Me.Location_Label.Visible = True

End If
Exit Sub

and try changing them as follows:

Me.Location.Enabled = True
Me.Location.Visible = True
Me.Location_Label.Visible = True
End If

Exit_MileageDescription_AfterUpdate:
Exit Sub

Err_MileageDescription_AfterUpdate:
MsgBox Err.Description
'Resume Exit_MileageDescription_AfterUpdate

Exit Sub

btw, when you set various controls to Visible = False, i noticed you're also
setting their labels' Visible property to False, as well. just as an fyi:
when a label is attached to a control (i.e., when you drag the control from
one place to another, the label moves with it automatically), setting the
control's Visible property to False will automatically hide the label as
well - so you shouldn't have to use extra code to hide/show the attached
control labels.

hth
 
T

tina

here's another btw: when you hide a control, there's no need to also set
its' Enabled property to False. an "invisible" control can't be entered
directly, only manipulated programmatically, so disabling it is redundant.
just as with hiding the labels attached to hidden controls, it doesn't "hurt
anything" - you're just making extra work for yourself in writing and
maintaining all those extra lines of code.

here's a "condensed" version of your code that i thought you might be
interested in looking at. you could make a copy of your database and try it
out in the copy, if you want (that way if it doesn't work, you can discard
the copy without having to "fix" anything).

With Me
Select Case .MileageDescription
Case "State Line"
.State.Enabled = True
.State.SetFocus
.Gallons.Visible = False
.Amount.Visible = False
.StopPurpose.Visible = False
.Location.Visible = False
Case "Fuel"
.State.Enabled = False
.Gallons.Visible = True
.Gallons.SetFocus
.Amount.Visible = True
.StopPurpose.Visible = False
.Location.Visible = False
Case "Stop"
.State.Enabled = False
.Gallons.Visible = False
.Amount.Visible = False
.StopPurpose.Visible = True
.StopPurpose.SetFocus
.Location.Visible = True
Case "Trip End"
.State.Enabled = False
.Gallons.Visible = False
.Amount.Visible = False
.StopPurpose.Visible = False
.Location.Visible = True
End Select

.State_Cover.Visible = Not .State.Enabled
End With

hth


tina said:
first, your error handler needs to be inside the sub, not below it. second,
you don't have the label "Exit_Mileage..." in your sub, so the Resume
command has no place to "go to". look at the last 6 lines of your sub
Me.Location.Enabled = True
Me.Location.Visible = True
Me.Location_Label.Visible = True

End If
Exit Sub

and try changing them as follows:

Me.Location.Enabled = True
Me.Location.Visible = True
Me.Location_Label.Visible = True
End If

Exit_MileageDescription_AfterUpdate:
Exit Sub

Err_MileageDescription_AfterUpdate:
MsgBox Err.Description
'Resume Exit_MileageDescription_AfterUpdate

Exit Sub

btw, when you set various controls to Visible = False, i noticed you're also
setting their labels' Visible property to False, as well. just as an fyi:
when a label is attached to a control (i.e., when you drag the control from
one place to another, the label moves with it automatically), setting the
control's Visible property to False will automatically hide the label as
well - so you shouldn't have to use extra code to hide/show the attached
control labels.

hth
 
G

Guest

Thanks for your help Tina. I didn't realize there needed to be an Exit
label. I thought the Exit Sub was sufficient. When I was working on this,
for some reason the labels did not hide with the controls so that is why they
were included in the code. Could this be that the Tag is different for the
label ie: Control Tag = option, Control Label Tag = Option Label? I just
noticed this. But I'm thinking the Tag has no effect on this code. Thanks
for the condensed code. I'll try it and I'm sure it'll work. I have been
looking at trying to do something similar but hadn't gotten around to trying
it. I'm tyring to learn what I can from Access Programming for Dummies and
from help from Discussions. I really appreciate the help I get from people
like you.
Thanks again,
Walter

tina said:
here's another btw: when you hide a control, there's no need to also set
its' Enabled property to False. an "invisible" control can't be entered
directly, only manipulated programmatically, so disabling it is redundant.
just as with hiding the labels attached to hidden controls, it doesn't "hurt
anything" - you're just making extra work for yourself in writing and
maintaining all those extra lines of code.

here's a "condensed" version of your code that i thought you might be
interested in looking at. you could make a copy of your database and try it
out in the copy, if you want (that way if it doesn't work, you can discard
the copy without having to "fix" anything).

With Me
Select Case .MileageDescription
Case "State Line"
.State.Enabled = True
.State.SetFocus
.Gallons.Visible = False
.Amount.Visible = False
.StopPurpose.Visible = False
.Location.Visible = False
Case "Fuel"
.State.Enabled = False
.Gallons.Visible = True
.Gallons.SetFocus
.Amount.Visible = True
.StopPurpose.Visible = False
.Location.Visible = False
Case "Stop"
.State.Enabled = False
.Gallons.Visible = False
.Amount.Visible = False
.StopPurpose.Visible = True
.StopPurpose.SetFocus
.Location.Visible = True
Case "Trip End"
.State.Enabled = False
.Gallons.Visible = False
.Amount.Visible = False
.StopPurpose.Visible = False
.Location.Visible = True
End Select

.State_Cover.Visible = Not .State.Enabled
End With

hth
 
T

tina

comments inline.

Walter said:
Thanks for your help Tina. I didn't realize there needed to be an Exit
label. I thought the Exit Sub was sufficient.

well, the code you posted included a Resume line that directed the system to
that "Exit..." label, so it had to be included in the sub or the code would
err out. the code i posted is an example of a standard error handler setup,
anyway, so it gives you a template that will work equally well when you
begin writing more complex error handling code.
When I was working on this,
for some reason the labels did not hide with the controls so that is why they
were included in the code.

hmmm, that's strange. is each label "attached to its' respective control?
you can check that in form design view two different ways: 1) select the
control only, *not* the label, and move the control to a different place in
the form. if the label moves with it, it's "attached". 2) select the control
only, and look at the label; it should have a little "handle" in its' upper
left corner, which shows that it's attached to the selected control. this
works in reverse too, if you select the label only, its' attached control
should have that "handle" in its' upper left corner.

the above should apply if you're using A2000 or newer. it's probably true in
A97 as well, but it's been some years since i used that version, so i
couldn't swear to it.
Could this be that the Tag is different for the
label ie: Control Tag = option, Control Label Tag = Option Label?

i don't quite follow you there. but if you mean the Tag property of the
control, and the Tag property of the label - no, that property shouldn't
have anything to do with whether or not a given label is "attached" to a
given control.
I just
noticed this. But I'm thinking the Tag has no effect on this code. Thanks
for the condensed code. I'll try it and I'm sure it'll work.

well, yes, it should work, as far as it goes. and the comments i made about
not needing to set the Enabled property still hold true. but if your control
labels are not automatically hidden when you hide the corresponding
controls, then i'm afraid you'll have to add that hide/unhide code back in
for the labels. shouldn't be difficult, though - just a pain. ;)
I have been
looking at trying to do something similar but hadn't gotten around to trying
it. I'm tyring to learn what I can from Access Programming for Dummies and
from help from Discussions. I really appreciate the help I get from people
like you.
Thanks again,
Walter

you're very welcome, Walter! i write reasonably clean code, most of the
time, and i've picked up a lot of tricks here in the newsgroups (i wish i'd
had this resource when i first began learning VBA!), so i'm glad to pass a
little on when i can. :)
 

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