Solved

How stop routine if text field is left blank

Posted on 2012-04-13
3
302 Views
Last Modified: 2012-04-13
I have a form which contains a command button.  Also on this form is a text field.  When the command button is clicked and if the text field is left blank I want the routine to stop and make the user enter a value in the text field.  Here is my attempt...

    If Me.txtPOn = "" Then
        MsgBox "You must enter a PO number first!"
        Me.txtPOn.SetFocus
    End If

But what is happening is the record is getting written to the table because of code after the above code.  This is not what I want to have happen.  Instead, the whole routine should halt and the focus should be on the text box.. "txtPOn".

I can't figure out how to make this happen.  Can someone help?

--Steve
0
Comment
Question by:SteveL13
3 Comments
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 37842256
Steve,

It is likely that if no data has ever been entered into the txtPOn control, that the "value" of that control is NULL, and whenever you try to compare a NULL to anything, it will fail, so you should either use:

IF me.txtPOn & "" = "" Then

or you could use

If NZ(me.txtPOn, "") = "" Then
0
 
LVL 17

Accepted Solution

by:
Natchiket earned 500 total points
ID: 37842281
You need to exit the procedure rather than carrying on with the code, thus

If Nz(Me.txtPOn,vbNullstring) = vbNullstring Then
        MsgBox "You must enter a PO number first!"
        Me.txtPOn.SetFocus
        Exit Sub
End If
0
 

Author Closing Comment

by:SteveL13
ID: 37842351
This worked perfectly.  Thank you for your help.
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

910 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

20 Experts available now in Live!

Get 1:1 Help Now