IsNull Problem (1 Viewer)

PatAccess

Registered User.
Local time
Today, 16:15
Joined
May 24, 2017
Messages
284
Hello Guys,
I have trouble understanding the IsNull process.
I have a CmdButton from a form that opens another form. I would like a message to pop up if there are no cr available to see. My code is on the "load" properties for the 2nd form and here is what I have. Can you please help me? :banghead:

Private Sub Form_Load()
Dim crname As String
crname = Me.EmpID.Value
If IsNull(crname) Then
crname = MsgBox("No Credit available", vbOKOnly)
Else
crname = ""
End If
End Sub

Thanks so much
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 13:15
Joined
Aug 30, 2003
Messages
36,124
Try simply

If IsNull(Me.EmpID) Then MsgBox("No Credit available", vbOKOnly)
 

PatAccess

Registered User.
Local time
Today, 16:15
Joined
May 24, 2017
Messages
284
Not it's giving me an error message
Compile error:
Expected: =
So should I declare a variable? or what am I missing?
Thank you
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 13:15
Joined
Aug 30, 2003
Messages
36,124
Did you get rid of everything else? That shouldn't throw an error.
 

PatAccess

Registered User.
Local time
Today, 16:15
Joined
May 24, 2017
Messages
284
Yes I took everything off
This what I have now:

Private Sub Form_Load()
If IsNull(Me.EmpID) Then MsgBox("No Credit available",vbOKOnly)
End Sub
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 13:15
Joined
Aug 30, 2003
Messages
36,124
Try

If IsNull(Me.EmpID) Then MsgBox "No Credit available"
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 15:15
Joined
Feb 28, 2001
Messages
27,146
Just to explain why Paul's suggestion is a good one, VBA function and subroutine syntax can be ambiguous with respect to parentheses. There are times when you need them and times when you don't.

Consider this sample:

Code:
    MsgBox "No credit available", vbOKOnly

You are using an IMPLIED "Call" and you are discarding the result of the function.

On the other hand,

Code:
iAnswer = MsgBox( "Press OK to continue or Cancel to abort", VBOKCancel )

Here you are expecting to use the value returned by the MsgBox function, which will tell you whether the user clicked the OK or the Cancel button.

But this case is neither fish nor fowl:

Code:
If IsNull( Me.EmpID ) Then MsgBox("No Credit Available", vbOKOnly)

After the "Then" keyword, the next item is a statement, which in this case begins with an identifier ("MsgBox") followed by parentheses that make it look like you are worried about the return value - but there is no place to PUT that return value. Which is why it wants the equals sign. Essentially, VBA is confused by a statement that does, but does not, want the value of the function.

I have a different take on this, though. Using this syntax,

Code:
crname = Me.EmpID.Value

I'm not sure WHAT you'll get back.

First, you don't need the .Value since for any statement that appears to be an expression, .Value is the default property. But we can let that slide.

Second, you are executing this in the Form_Load routine, which fires BEFORE the Form_Current routine. That means that you would never see any value there. But I don't know that you would ever see a null in that case, either. You MIGHT see an empty string. If I were coding that (in some other event than _Open or _Load), I might use

Code:
If Nz(Me.EmpID, 0) = 0 Then ...
' OR, if EmpID is mixed alphanumeric...
If Nz(Me.EmpID, "") = "" Then ...
 

PatAccess

Registered User.
Local time
Today, 16:15
Joined
May 24, 2017
Messages
284
Thank you Guys for the help and the explanation
This forum is pretty awesome!
 

Users who are viewing this thread

Top Bottom