?
Solved

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

Posted on 2014-12-14
6
Medium Priority
?
376 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 1000 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 46

Expert Comment

by:aikimark
ID: 40499716
There is a difference between Null and an empty string (vbNullstring) values
0
 
LVL 46

Accepted Solution

by:
aikimark earned 1000 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 46

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
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

764 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