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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VarType

Status
Not open for further replies.

GerardMcL

Technical User
Aug 5, 2004
212
IE
Dim selID
selID = Text1.Text

If selID <> vbNullString Then
If VarType(selID) <> vbInteger Then
Text1.SetFocus
Text1.Text = vbNullString
End If
End If

I want to make sure only an Integer can be entered.
So If the text box isnt Null then check the Type
If Not an Int then clear text box and set focus.
Unfortunately for some reason it wont even allow Ints
Tried entering 2,3,4, etc all clear the text box and set focus.....WHY?????
 
I don't know why but this should work:

If selID <> vbNullString Then
If Not IsNumeric(selID) Then
txttext1.SetFocus
txttext1.Text = vbNullString
End If
End If
 
The .Text property of the TextBox returns a string regardless of what value that string is.
The solution offered by TysonLPrice will work if you just want to make sure the TextBox holds a numeric value. If you really want to make sure it's an integer,
Code:
If selID < -32768 or selID > 32767 then
    Text1.SetFocus
    Text1.Text = vbNullString
End If
or
Code:
Dim intTest As Integer
On Error GoTo Bad_Input
intTest = Text1.Text
Exit Sub
Bad_Input:
Text1.SetFocus
Text1.vbNullString
 
Simplest and by far most productive way would be to use a masked edit box.

The other way would be to use (which checks the textbox text1 contains only numeric data, and there is no decimal point making it not an integer)

if isnumeric(text1.text) and (instr$(1,text1.text,".")=0) then bolIsInteger = true
 
Also workable methods if you just want to make sure it's a whole number - the dictionary definition of an integer.
GerardMcL seems to want to make sure the number is of the Visual Basic data type Integer which does not include all possible whole numbers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top