Check for empty values in unbound text and combo box

P

Paterson10987

I have several unbound controls that are filled out and then use a command
button to store those values in my table. I want to make sure that most of
them are not blank, but everything I'm trying is failing.

I tried:
If [cboArea] = "" then
MsgBox "Area is Empty"
Exit Sub
End If

And:
If Len(Nz([cboArea], "") > 0) Then
...

The Len function always returns true even when I change the value. Neither
work even when I change to [cboArea].Value or try similarly with a text box.

Any Ideas?
 
K

Ken Snell \(MVP\)

I usually check for empty values this way:

If Len(Me.NameOfControl.Value & vbNullString) = 0 Then
MsgBox "No value!"
End If

If you want to treat blank space(s) as an "empty value", then

If Len(Trim(Me.NameOfControl.Value) & vbNullString) = 0 Then
MsgBox "No value!"
End If
 
D

David W. Fenton

If Len(Me.NameOfControl.Value & vbNullString) = 0 Then
MsgBox "No value!"
End If

Is there any reason you explicitly specify what is already the
default property of the control, i.e., .Value?
 
D

David W. Fenton

if len(me.cboArea & "") =0 then
MsgBox "Area is Empty"

It's more efficient to use vbNullString instead of "". It doesn't
matter for a single line here or there, but if you're doing it in a
loop, it means allocating memory for the zero-length string with
each iteration of the loop, whereas if you use vbNullString, the
memory for that constant has already been allocated.
 
K

Ken Snell \(MVP\)

Yes. I find it easier to quickly see that I'm using a property of an object
and not an object itself. I know it's not necessary because Value is the
default property for a control and for a field, but I prefer to state it
explicitly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top