Solved

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

Posted on 2014-12-14
6
359 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 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

832 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