Invalid Use of Null Error when checking for Nulls with IsNull (1 Viewer)

Zedster

Registered User.
Local time
Today, 09:11
Joined
Jul 2, 2019
Messages
168
I am using the IsNull function to prevent passing Null values to another function, but the line with the IsNull check is itself throwing an Invalid Use of Null Error. I am a little confused and would appreciate any insight into what the problem may be. The Error is returning at line 20.

Code:
Private Sub txtReceivedFrom_BeforeUpdate(Cancel As Integer)
10    On Error GoTo err_txtReceivedFrom_BeforeUpdate

20        If Not IsNull(Me.txtReceivedFrom) Then
30            If Not LengthOK(Me.txtReceivedFrom, 20) Then
40                Cancel = True
50                Me.txtReceivedFrom.Undo
60            End If
70        End If

exit_txtReceivedFrom_BeforeUpdate:
80        Exit Sub
          
err_txtReceivedFrom_BeforeUpdate:

90        MsgBox "Error No: " & Err.Number & vbCrLf & "Description: " & Err.Description & vbCrLf & "Error Line: " & Erl
100       Call Logger("Error", "stores_v" & cstLatestVersion & ".Form_frmControlScreen.txtReceivedFrom_BeforeUpdate", Erl, Err.Number, Err.Description)
110       Resume exit_txtReceivedFrom_BeforeUpdate
End Sub
 

cheekybuddha

AWF VIP
Local time
Today, 09:11
Joined
Jul 21, 2014
Messages
2,237
Not sure why your code with suffer that error at line 20.

Do you have Option Explicit declared at the top of every code module, and does your code compile? It can often be a completely different code error that is throwing things.

If you do have the above, then you can try a different test for line 20:
Code:
' ...
20        If Len(Me.txtReceivedFrom & vbNullString) > 0 Then
30            If Not LengthOK(Me.txtReceivedFrom, 20) Then
40                Cancel = True
' ...
The code checks for both Null and zero-length string in one.

hth,

d
 

Zedster

Registered User.
Local time
Today, 09:11
Joined
Jul 2, 2019
Messages
168
Not sure why your code with suffer that error at line 20.

Do you have Option Explicit declared at the top of every code module, and does your code compile? It can often be a completely different code error that is throwing things.

If you do have the above, then you can try a different test for line 20:
Code:
' ...
20        If Len(Me.txtReceivedFrom & vbNullString) > 0 Then
30            If Not LengthOK(Me.txtReceivedFrom, 20) Then
40                Cancel = True
' ...
The code checks for both Null and zero-length string in one.

hth,

d

Thanks for the tip to check for both empty strings and nulls, will give that a try.

Generally I add option explicit at the top of each module, however I took the opportunity to check out all modules and on 3 out of 19 I had forgotten to. When I compiled it there was indeed one compile error (misspelled a variable) but it was in a totally unrelated form. I am struggling to get my head around a Null Error on a line which uses IsNull to check for Nulls!
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:11
Joined
May 7, 2009
Messages
19,169
you can use the Change event to inspect
how many Chars, so far, your textbox (text property) has.
so you can Cancel further entry.
Code:
Private Sub txtReceivedFrom_Change()
Dim t As String
t = [txtReceivedFrom].Text
If Len(t) > 20 Then SendKeys "{BS}"
End Sub
 
Last edited:

cheekybuddha

AWF VIP
Local time
Today, 09:11
Joined
Jul 21, 2014
Messages
2,237
>> but it was in a totally unrelated form. <<

Did you still get the error after you fixed the unrelated problem and compiled? Sometimes these unrelated errors cause weird illogical errors elsewhere. Hence why I asked.
 

Zedster

Registered User.
Local time
Today, 09:11
Joined
Jul 2, 2019
Messages
168
>> but it was in a totally unrelated form. <<

Did you still get the error after you fixed the unrelated problem and compiled? Sometimes these unrelated errors cause weird illogical errors elsewhere. Hence why I asked.

In truth, I don't know as I personally have never been able to recreate the error (which is why I am finding it hard to get to the bottom of), but it does happen for users because my error trapper routine sends me an email when a user gets an error.

Your suggestion that Access can throw illogical error due to errors elsewhere is consistent with my experience, so it could plausibly be a factor. I have corrected it and I am about to re-issue the database so I will be able to monitor if it has an impact.
 

cheekybuddha

AWF VIP
Local time
Today, 09:11
Joined
Jul 21, 2014
Messages
2,237
See Arnel's suggestion - you can avoid the Null issue entirely if you use the Change event.

(I might avoid the use of SendKeys, however)
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 09:11
Joined
Sep 12, 2006
Messages
15,613
try just this as the first line
then you have handled the null.

if nz(txtreceivedfrom,"")="" then exit sub
 

Zedster

Registered User.
Local time
Today, 09:11
Joined
Jul 2, 2019
Messages
168
thanks for all the suggestions, will do some experimenting
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:11
Joined
May 7, 2009
Messages
19,169
this is Experiment no.1
 

Attachments

  • MaxChar.accdb
    452 KB · Views: 514

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 09:11
Joined
Sep 12, 2006
Messages
15,613
I should add, if the various suggestions for handling the null are not working, I would think there is some other issue. There may be a mild corruption. Rebuild the form from scratch, and see if that fixes it.
 

Users who are viewing this thread

Top Bottom