Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem using IF statement for a textbox

Status
Not open for further replies.

Newatprogramming

Technical User
Mar 5, 2002
30
0
0
US
Hi I am trying to validate that the user entered a number greater than zero in a textbox. My problem is if the user does not enter any data the form will not unload unless something is entered in the text box. Any help would be great.
 
so you are wanting the form to unload if the value is NULL. Correct?
 
Reply. Yes that would be correct. What I have is three textboxes. Each requiring a numeric value, which combined will complete a calculation. If a user entered a zero or a negative number the calculation would be inaccurate. But if the user just wanted to exit the form without even entering any data, the user could do so. Your help would be great.
 
Ok. can simply do this by the following: I added 3 text boxes and set them to numeric only. I added a command button to calculate. If I enter nothing in them it will exit the program but as long as I enter any # the calculation is correct, including negative numbers!

Dim txtvalue As Integer

Private Sub Command1_Click()
If Text1.Text = "" Or Text2.Text = "" Or Text3.Text = "" Then
End
Else
txtvalue = Len(Text1.Text + Text2.Text + Text3.Text)
MsgBox (txtvalue)
End If

End Sub

I hope this helps you.
 
Newatprogramming ...
The form will not unload probably due to a logic error in your validate events.
There are several methods that are commonly used in something like this. A ForEach loop that examines the TypeOfControl and if it is a textbox, it tests the text property for a &quot;&quot;, a 0 or <0 values. You could also use the If statement that Chris posted, but I use change END with Unload ME.

Chris .. How did you do this &quot;I added 3 text boxes and set them to numeric only.&quot; By setting, I assume you mean in code of some sort?

HTH
Michael
 
in the validate event of the textboxes you can use

if text1.text=vbnullstring or text1.text < 0 then
'whatever
end if

hope this help :)
 
Actually I did it both with setting the value to be a number and code that will only allow numeric, decimal and the negative sign to be inputed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top