Solved

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

Posted on 2014-12-14
6
372 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 27

Assisted Solution

by:MacroShadow
MacroShadow earned 250 total points
ID: 40499713
    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
ID: 40499716
There is a difference between Null and an empty string (vbNullstring) values
0
 
LVL 45

Accepted Solution

by:
aikimark earned 250 total points
ID: 40499720
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:mainrotor
ID: 40499744
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
ID: 40499749
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 48

Expert Comment

by:Dale Fye (Access MVP)
ID: 40500144
@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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

695 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