Solved Access gives Null error

R-Berg

New member
Local time
Today, 09:58
Joined
Mar 19, 2024
Messages
3
I have the following code that does not seem to work.
I am generating a Word dokument without any problems, but any empty textbox generates a Null error.

To prevent this i thought of using this code, but it is not solving the issue.
It just seems to jump over thevbNullString part.

Any tips?

Code:
       If Me.MaxBar <> vbNullString Then
                 MsgBox "Field shall contain a value"
                 Exit Sub
                 Else
        FindText = "[PdX]"
        ReplaceWith = Me.MaxBar
        .Execute FindText:=FindText, ReplaceWith:=ReplaceWith, Replace:=2
           End If
 
can you try:

If IsNull(Me.MaxBar) = False Then
'''
...
 
OR test both Null and empty string in one go:
Code:
      If Len(Me.MaxBar & vbNullString) = 0 Then
        MsgBox "Field shall contain a value"
        Exit Sub
      Else
        FindText = "[PdX]"
        ReplaceWith = Me.MaxBar
        .Execute FindText:=FindText, ReplaceWith:=ReplaceWith, Replace:=2
      End If
 
Any tips?

The others have given you sample code. I'll explain the problem.

If Me.MaxBar <> vbNullString Then

All of <, =, and > and combinations thereof are comparisons that normally return TRUE or FALSE. The problem is that if ANYTHING is compared to a null, the result is not TRUE or FALSE - it is also null. Null overtakes and propagates its way through expressions unless you use functions or specific relationship keywords specifically targeted to work with nulls. The fact that your comparison DIDN'T use a null-catcher is why the IF barfed.

One more suggestion:

Since that is being used as text, this also would work in your context, comparing against "", the "empty" string:

Code:
IF NZ( Me.MaxBar, "" ) <> "" THEN

By the way - that was your first post. Hello and welcome to the forum.
 
The check in the code from #1 works - only the logic is wrong.
Code:
If Me.MaxBar <> vbNullString Then ' => Me.MaxBar contain a value   (BTW: a string is never < vbNullString)
     MsgBox "Field shall contain a value"  ' <-- This should happen if the control does not contain a value, right?
     Exit Sub
Else
     FindText = "[PdX]"
     ReplaceWith = Me.MaxBar
     .Execute FindText:=FindText, ReplaceWith:=ReplaceWith, Replace:=2
End If
=> See #3 for correct code.
Or switch if blocks:
Code:
If Me.MaxBar > vbNullString Then ' => Me.MaxBar contain a value
     FindText = "[PdX]"
     ReplaceWith = Me.MaxBar
     .Execute FindText:=FindText, ReplaceWith:=ReplaceWith, Replace:=2
Else
     MsgBox "Field shall contain a value"  ' <-- This should happen if the control does not contain a value, right?
     Exit Sub
End If

Note:
If exit sub is used, you can write the Else part directly after the If.
Code:
    If Len(Me.MaxBar & vbNullString) = 0 Then
        MsgBox "Field shall contain a value"
        Exit Sub
    End If

    FindText = "[PdX]"
    ReplaceWith = Me.MaxBar
    .Execute FindText:=FindText, ReplaceWith:=ReplaceWith, Replace:=2
 
Last edited:
Thanks for all the explanations. I got it to work.
 

Users who are viewing this thread

Back
Top Bottom