Solved

I need help checking to see if a text box is NULL in ACCESS

Posted on 2014-12-14
6
352 Views
Last Modified: 2014-12-15
Hi Experts,
I need to check if a combo box and text box have an empty string in ACCESS.  If they do, then I don't want to save a record in my table.

I have added the code below in my application, but the record still saves.  
    Dim sql As String

    If cboClientName.Value = Null Or cboClientName.Value = "" Then
       MsgBox ("Please make sure Client Name is filled in.")
    ElseIf txtJobName.Value = Null Or txtJobName.Value = "" Then
        MsgBox ("Please make sure Job Name is filled in.")
    Else
         sql = "Insert Into tblJOb ....)"
         DoCmd.RunSQL sql
    End If

Open in new window



What am i doing wrong?  How do I fix this?  Thanks in advance.

mrotor
0
Comment
Question by:mainrotor
6 Comments
 
LVL 26

Assisted Solution

by:MacroShadow
MacroShadow earned 250 total points
Comment Utility
    Dim sql As String

    If Len(cboClientName.Value) = 0 Or Len(cboClientName.Value) = 0 Then
       MsgBox ("Please make sure Client Name is filled in.")
    ElseIf Len(txtJobName.Value) = 0 Or Len(txtJobName.Value) = 0 Then
        MsgBox ("Please make sure Job Name is filled in.")
    Else
         sql = "Insert Into tblJOb ....)"
         DoCmd.RunSQL sql
    End If
                                  

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
There is a difference between Null and an empty string (vbNullstring) values
0
 
LVL 45

Accepted Solution

by:
aikimark earned 250 total points
Comment Utility
If this happens in a subroutine/function, you might do something like the following:
    Dim sql As String
    'Validate non-empty controls
    If Len(vbNullString & cboClientName.Value) = 0 Then
        cboClientName.SetFocus
        'cboClientName.BackColor =  vbYellow
        MsgBox "Please make sure Client Name is filled in."
        Exit Sub
    End If
    If Len(vbNullString & txtJobName.Value) = 0 Then
        txtJobName.SetFocus
        'txtJobName.BackColor = vbYellow
        MsgBox "Please make sure Job Name is filled in."
        Exit Sub
    End If

    sql = "Insert Into tblJOb ....)"
    DoCmd.RunSQL sql

Open in new window


Although I've commented the statements, I usually set the background color of non valid controls to yellow.  This also requires the controls to set their background color to white whenever they change.  If you want to do that, the code I posted would need to change to do both validations an only exit if there were no validation errors.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:mainrotor
Comment Utility
Thank you both very much for your help.  Really appreciate it.  You both are awesome!

I have also posted another ACCESS question that has to do with populating a subform using a SQL SELECT statement.  Do you think you can take a look at it and see you can assist?  Thank you.

mrotor
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
what is the URL or question number for that question?

I am headed to bed, but I'll take a look at it when I wake up
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
@mainrotor

I actually have a function I use to do this.  It does the basically the same thing as Macro and aikimark recommend, but is easier to read and actually takes less key strokes.

Public Function IsNullOrBlank(SomeValue) as Boolean

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

End Function

Open in new window

Then, in your code you simply use:

If IsNullOrBlank(me.cboClientName) = 0 Then

**Note that in VBA, the default property of a control is the Value property, so you don't really need to add that to your code.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Access Date Query 28 28
How to Add / Edit Windows Menu 4 23
Access MDB/PDF 21 28
putting an icon in a form 13 19
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now