Label Not Defined Error

  • Thread starter Thread starter Jerry
  • Start date Start date
J

Jerry

I'm a novice in VB, so please bear with me.

The following code is set asan "after update" for
my "Grade" field. When it's run, I get a "label not
defined" error, and it highlights "GoTo IfNull"
indicating that's where the error is. I'm sure it's
obvious to the experienced, but I can't see the problem.

What the routine does is this: If the grade you enter
is "S", then it will increment the Superiors number by
one. If you enter something other than "S" it takes the
number in the Superiors field, if there is one, and
deletes it (sets it to null).

Private Sub Grade_AfterUpdate()
Dim TempGrade As String
Dim TempSuperior As Integer

TempGrade = Me.Grade
If Me.Superiors = Null Then GoTo IfNull Else GoTo
IsNotNull

IsNotNull:
TempSuperior = Me.Superiors
If [TempGrade] <> "S" Then Me.Superiors = Null
If [TempGrade] = "S" Then Me.Superiors = TempSuperior
+ 1
End Sub

IfNull:
TempSuperior = 0
If [TempGrade] <> "S" Then Me.[Superiors] = Null
If [TempGrade] = "S" Then Me.[Superiors] =
TempSuperior + 1
End Sub
 
The construct
If Me.Superiors = Null Then
will never be true. Instead use:
If IsNull(Me.Superiors) Then

For an explanation, see:
Common errors with Null
at:
http://allenbrowne.com/casu-12.html

In general, we try to avoid GoTo in code, because trying to trace how a
program executes is like trying to trace how the spagehtii sits on a plate.
An If block would do the same job more easily:
If IsNull(Me.Superiors) Then
'put as many lines here as you need
ElseIf
'do some other lines
Else
'do whatever
End If

More broadly, though, trying to store a count like that really worries me.
When I enter a value in the Grade field, your code runs and performs the
updates. Then if I tab to another field without saving the record, realise I
made a mistake and go back and change the Grade, the code runs again? And
the results are wrong? Storing dependent values is almost always more
trouble that it is worth.
 
Not sure why you're getting that error, but the code won't
work regardless.

You can never compare anything to Null. Think of Null as
meaning Unkown and comparing something to an unknown value
always has an unknown result. Us the IsNull function
instead of =.

Also, you should try harder to avoid the use of GoTo, which
would obviate the problem:

Private Sub Grade_AfterUpdate()
Dim TempGrade As String
Dim TempSuperior As Integer

TempGrade = Me.Grade
If IsNull(Me.Superiors) Then
If [TempGrade] <> "S" Then
Me.[Superiors] = Null
Else
Me.[Superiors] = 1
End If
Else
TempSuperior = Me.Superiors
If [TempGrade] <> "S" Then
Me.Superiors = Null
Else
Me.Superiors = TempSuperior + 1
End If
End If
End Sub
 
Back
Top