Null error msg

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a table which is used to track mileages. What I am trying to do is if
there are records present, get the hightest mileage reading and insert it
into the form field. If the user is inputting the first record, I want 0
(zero) to be inserted into the field. Below is the code I have created for
this but I get an 'Invalid use of Null' error message. Can someone help me
correct the code please? Thank you.

Private Sub StatDate_Exit(Cancel As Integer)
Dim StrtMiles As Variant

StrtMiles = DMax("[OdometerEnding]", "[Statistics]")
If IsNull(StrtMiles) Then
StrtMiles = 0
Me.Start = StrtMiles
Else
Me.Start = StrtMiles
End If

End Sub
 
Wylie said:
I have a table which is used to track mileages. What I am trying to do is if
there are records present, get the hightest mileage reading and insert it
into the form field. If the user is inputting the first record, I want 0
(zero) to be inserted into the field. Below is the code I have created for
this but I get an 'Invalid use of Null' error message. Can someone help me
correct the code please? Thank you.

Private Sub StatDate_Exit(Cancel As Integer)
Dim StrtMiles As Variant

StrtMiles = DMax("[OdometerEnding]", "[Statistics]")
If IsNull(StrtMiles) Then
StrtMiles = 0
Me.Start = StrtMiles
Else
Me.Start = StrtMiles
End If

End Sub

I am using A2K. I tried to replicate your error but I couldn't.

When you step thru the code, which line causes the error?

Note that you can reduce the above sub to one line:

Private Sub StatDate_Exit(Cancel As Integer)
Me.Start = Nz(DMax("[OdometerEnding]", "[Statistics]"), 0)
End Sub
 
I was working on this last night and found I was working with a backup copy.
BUT I STILL have a question. The only difference was the copy I was working
with set the StrtMiles as a Single variable (as was the field property in the
table.) When I changed the codes variable to Variant, it worked without
error. Could you maybe explain why it works as a Variant and not a Single
variable?
Thanks

Ken Snell said:
On which line of code do you get the error?

--

Ken Snell
<MS ACCESS MVP>

Wylie C said:
I have a table which is used to track mileages. What I am trying to do is
if
there are records present, get the hightest mileage reading and insert it
into the form field. If the user is inputting the first record, I want 0
(zero) to be inserted into the field. Below is the code I have created for
this but I get an 'Invalid use of Null' error message. Can someone help me
correct the code please? Thank you.

Private Sub StatDate_Exit(Cancel As Integer)
Dim StrtMiles As Variant

StrtMiles = DMax("[OdometerEnding]", "[Statistics]")
If IsNull(StrtMiles) Then
StrtMiles = 0
Me.Start = StrtMiles
Else
Me.Start = StrtMiles
End If

End Sub
 
Variant is the only data type that can contain a Null value.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)



Wylie C said:
I was working on this last night and found I was working with a backup
copy.
BUT I STILL have a question. The only difference was the copy I was
working
with set the StrtMiles as a Single variable (as was the field property in
the
table.) When I changed the codes variable to Variant, it worked without
error. Could you maybe explain why it works as a Variant and not a Single
variable?
Thanks

Ken Snell said:
On which line of code do you get the error?

--

Ken Snell
<MS ACCESS MVP>

Wylie C said:
I have a table which is used to track mileages. What I am trying to do
is
if
there are records present, get the hightest mileage reading and insert
it
into the form field. If the user is inputting the first record, I want
0
(zero) to be inserted into the field. Below is the code I have created
for
this but I get an 'Invalid use of Null' error message. Can someone help
me
correct the code please? Thank you.

Private Sub StatDate_Exit(Cancel As Integer)
Dim StrtMiles As Variant

StrtMiles = DMax("[OdometerEnding]", "[Statistics]")
If IsNull(StrtMiles) Then
StrtMiles = 0
Me.Start = StrtMiles
Else
Me.Start = StrtMiles
End If

End Sub
 
Back
Top