VBA code in MS Access - testing bound text box for Null

Hi Experts,

I'm using VBA code in MS Access.

I've got a bound text box, Organization, that is on a subform.

I'm referencing it by Forms!Clients!Organization.

It works fine, as long as it is not Null.

How can I test if it is Null in an If statement?

I've got this code now:

If Forms!Clients!Organization = Null Then
   MsgBox "You have not specified Company/Organization."
   Exit Sub
End If

Open in new window

But when Forms!Clients!Organization is Null, the If evaluates to False.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

John TsioumprisSoftware & Systems EngineerCommented:
If its really Null then
If IsNull(Forms!Clients!Organization ) then

Open in new window

but sometimes Null is not exacty Null so i use
If Len(Nz(Forms!Clients!Organization,""))>0  then

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LeighWardleAuthor Commented:
Thanks, John.

You are worth your weight in Gold!

John TsioumprisSoftware & Systems EngineerCommented:
This is rather dangerous statement since we are talking about a lot of weight....lets say the scale has a hard time when i visit her..:)
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

LeighWardleAuthor Commented:
Hi John,

Just for the record:

I needed to change > 0 to = 0 in the If statement:

If Len(Nz(Forms!Clients!Organization,"")) =  0  then

Open in new window

That works for the text box when it is Null, also when it consists of spaces...
John TsioumprisSoftware & Systems EngineerCommented:
If you have spaces then you need one more command...to clear the spaces
If Len(Nz(Trim(Forms!Clients!Organization),"")) =  0  then

Open in new window

Dale FyeOwner, Developing Solutions LLCCommented:
except if it is NULL, Trim will raise an error:

I use a function which makes it very clear what you are trying to test for, and has the added benefit of having less key strokes than the other options.

If IsNullOrBlank(Forms!Clients!Organization) then

Public Function IsNullOrBlank(SomeValue as Variant) as boolean

    IsNullOrBlank = Trim(SomeValue & " ") = ""

End Function

Open in new window

John TsioumprisSoftware & Systems EngineerCommented:
Good point Dale..so my code should be
If Len(Trim(Nz(Forms!Clients!Organization,""))) =  0  then

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.