Solved

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

Posted on 2014-12-14
6
365 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
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!

 

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 47

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
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.

730 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