Link to home
Create AccountLog in
Avatar of SteveL13
SteveL13Flag for United States of America

asked on

If a field is left empty

I have a field on a form which I do not want the user to be able to leave blank.  I've tried this code in afterupdate, beforeupdate, onexit and it will not work.  What am I doing wrong?  The field is a combobox.  The field's rowsource is a query.  The bound column is numeric.

    If Me.cboSoldByName = "" Then
        MsgBox "You must select or enter a member name"
    End If
Avatar of IrogSinta
IrogSinta
Flag of United States of America image

If you are you getting a Null error, try:
If Me.cboSoldByName & "" = "" Then
SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of skullnobrains
skullnobrains

reg.Pattern = "^\s*$"
If reg.Test(Me.cboSoldByName) ...

this regexp should match any number of spaces in the field

if you are afraid there may be nulls or weird values, better use the negation

reg.Pattern = "^.*\S.*$"
If ! reg.Test(Me.cboSoldByName) ...

ie if the field does not contain at least one non blank character
Just some notes...

This is a typical question here...

Remember that doing the validation on the control itself will not catch situations where the user does not even go into the control. (suppose they simply just skip the SoldByName Combobox?)

In some cases, validation like this is done on the BeforeUpdate event of the "Form".
This way you can validate all of the controls at the same time (Just like in a web site order form)

Validating "on the fly" seems slick, but invariably, users will always find a way around this.
(By accident, or intentionally)

Also you may want to allow for situations where it might be perfectly valid to leave this box blank (Unassigned, test, Default, ...etc)
...or even set a default value for this control/field

Obviously, every situation is different, ...again, just some other things to consider...

;-)

JeffCoachman