if statement

  • Thread starter Thread starter jason
  • Start date Start date
J

jason

hi there, i have a really easy one. i have a form with
only a text box and a command button, i am trying to code
the command button so that when a user clicks it it does a
search. i have that part down. the problem i am having is
making an IF statement to say "if the user does not type
anything in the text box and hits the button it comes up
with a message box saying "enter search criteria"

i have tried everything i can think of, my VB programming
is rusty.
any suggestions would help
 
Hi Jason,

If IsNull(txtBoxName) Then
MsgBox "Enter Search Criteria",vbOkOnly+vbInformation,"Your Title"
txtBoxName.SetFocus
Else
'your search code here
End If

another alternative to the first line is

If Len(Nz(txtBoxName, "")) = 0 Then

HTH
Steve C
 
Hi Jason,

I'm a newbie but this works for me:

if isnull(txtbox.value) then
msgbox("You have to enter a value")
txtbox.setfocus
end if

You can also

if len(txtbox.value) = 0 then
msgbox("You have to enter a value")
txtbox.setfocus
end if

Karen
 
Hey.

Code:

if me.yourtextbox="" then
intRetVal=msgbox(Prompt,Button,Title)
exit sub
endif
 
Something like...

If Len(Me.txtBox & "") = 0 Then
MsgBox "Please enter search criteria."
Me.txtBox.SetFocus
Exit Sub
End If

- Jim
 
jason said:
hi there, i have a really easy one. i have a form with
only a text box and a command button, i am trying to code
the command button so that when a user clicks it it does a
search. i have that part down. the problem i am having is
making an IF statement to say "if the user does not type
anything in the text box and hits the button it comes up
with a message box saying "enter search criteria"

i have tried everything i can think of, my VB programming
is rusty.
any suggestions would help

I know I'm late to the party, but I always check for NULL and blank by:

Dim MSG as string, Answer

If Isnull(Me.YourTextBox) or Me.TextBox="" then
Msg = "Hey! You didn't enter anything!"
Answer = Msgbox(Msg, vbokonly, "Enter something...")
Else
<Insert your code here...>
End IF
 
Or another one is:

If me("controlName") & "" = ""
..........

This will catch null as well, so it is nice for lazy typers ;-)

A comment on Jason's code below:

If IsNull(txtBoxName) Then
MsgBox "Enter Search Criteria",vbOkOnly+vbInformation,"Your Title"
txtBoxName.SetFocus
Else
'your search code here
End If

I've found I have to set focus to another control (a label will do)
and then back to the control I really want to have focus.

Peter
 
Back
Top