Checking for an empty string

  • Thread starter vbnetman via AccessMonster.com
  • Start date

V

vbnetman via AccessMonster.com

On one of my forms, I want to verify that a combo box actually has data
populated by the user. In psudo code I need to do something like;


if combo box.text = ""l then
msgbox "you have to enter something"
combobox.gotfocus
run the first line of this code again and continue to
do so until there is something in the combox
end if

Could someone assist with VBA?

Thank you
 
Ad

Advertisements

D

Douglas J. Steele

A combo box that hasn't had anything selected doesn't contain "" (a
zero-length string): it contains Null.

If IsNull(Me.ComboBox) Then
MsgBox "You have to enter something"
Me.ComboBox.SetFocus
End If

It's doubtful that you'll need a loop: presumably you're putting this code
in a BeforeUpdate event, in which case you set Cancel = True, and the event
doesn't fire again (until they make a change, or hit the Save button, or
whatever you've got on your form to trigger the event)
 
V

vbnetman via AccessMonster.com

Hello Douglas,
Thank you for the response. The form itself has 5 input boxes. The trigger
event is an OK button accepting the data. It is at this point that I would
like to test the data for the combo box. For example, what if the user does
not populate the combo box at all, skips, forgets, error, whatever. So, I'm
looking for something that brings the users attention back to the combo box
if it's empty and preventing him / her from from leaving it until it's filled
in. Seems like a loop is appropriate. Also, when you suggest a BeforeUpdate
event do you mean the combo box or the form?

Thank you
A combo box that hasn't had anything selected doesn't contain "" (a
zero-length string): it contains Null.

If IsNull(Me.ComboBox) Then
MsgBox "You have to enter something"
Me.ComboBox.SetFocus
End If

It's doubtful that you'll need a loop: presumably you're putting this code
in a BeforeUpdate event, in which case you set Cancel = True, and the event
doesn't fire again (until they make a change, or hit the Save button, or
whatever you've got on your form to trigger the event)
On one of my forms, I want to verify that a combo box actually has data
populated by the user. In psudo code I need to do something like;
[quoted text clipped - 9 lines]
Thank you
 
V

vbnetman via AccessMonster.com

So I end with something like this only I cannot enter data to break the loop..
 
V

vbnetman via AccessMonster.com

Douglas,
One additional thing...in the click event of the OK button there is code that
follows this that I can't allow to execute. Thus, I really feel that a loop
is necessary prior to proceeding. For some reason the line "Me.ComboBox.
SetFocus" is not returning to the form to allow a user to add data.
A combo box that hasn't had anything selected doesn't contain "" (a
zero-length string): it contains Null.

If IsNull(Me.ComboBox) Then
MsgBox "You have to enter something"
Me.ComboBox.SetFocus
End If

It's doubtful that you'll need a loop: presumably you're putting this code
in a BeforeUpdate event, in which case you set Cancel = True, and the event
doesn't fire again (until they make a change, or hit the Save button, or
whatever you've got on your form to trigger the event)
On one of my forms, I want to verify that a combo box actually has data
populated by the user. In psudo code I need to do something like;
[quoted text clipped - 9 lines]
Thank you
 
D

Douglas J. Steele

Best place to put the code would be in the form's BeforeUpdate event. That
would mean that they would not be able to move to another record until they
provided all of the information on the current one (or undid whatever other
changes they'd made)

Regarding your other question about code in the Click event of a command
button that shouldn't be executed if they haven't picked a value from the
combo box:

If IsNull(Me.ComboBox) Then
MsgBox "You have to enter something"
Me.ComboBox.SetFocus
Else
' Code that should only execute if the combo box is okay
End If


--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


vbnetman via AccessMonster.com said:
Hello Douglas,
Thank you for the response. The form itself has 5 input boxes. The trigger
event is an OK button accepting the data. It is at this point that I would
like to test the data for the combo box. For example, what if the user
does
not populate the combo box at all, skips, forgets, error, whatever. So,
I'm
looking for something that brings the users attention back to the combo
box
if it's empty and preventing him / her from from leaving it until it's
filled
in. Seems like a loop is appropriate. Also, when you suggest a
BeforeUpdate
event do you mean the combo box or the form?

Thank you
A combo box that hasn't had anything selected doesn't contain "" (a
zero-length string): it contains Null.

If IsNull(Me.ComboBox) Then
MsgBox "You have to enter something"
Me.ComboBox.SetFocus
End If

It's doubtful that you'll need a loop: presumably you're putting this code
in a BeforeUpdate event, in which case you set Cancel = True, and the
event
doesn't fire again (until they make a change, or hit the Save button, or
whatever you've got on your form to trigger the event)
On one of my forms, I want to verify that a combo box actually has data
populated by the user. In psudo code I need to do something like;
[quoted text clipped - 9 lines]
Thank you
 
Ad

Advertisements

V

vbnetman via AccessMonster.com

Thank you Douglas for your time. I ended up using your first suggestion after
all.
A combo box that hasn't had anything selected doesn't contain "" (a
zero-length string): it contains Null.

If IsNull(Me.ComboBox) Then
MsgBox "You have to enter something"
Me.ComboBox.SetFocus
End If

It's doubtful that you'll need a loop: presumably you're putting this code
in a BeforeUpdate event, in which case you set Cancel = True, and the event
doesn't fire again (until they make a change, or hit the Save button, or
whatever you've got on your form to trigger the event)
On one of my forms, I want to verify that a combo box actually has data
populated by the user. In psudo code I need to do something like;
[quoted text clipped - 9 lines]
Thank you
 

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