Check for number

bodylojohn

Registered User.
Local time
Today, 14:21
Joined
Dec 28, 2005
Messages
205
Hello everyone,

The thing I want to do is check if the value entered in a textbox is an integer. If it is...then great.
If it isn't then display an errormessage.

I tried this:

If CInt(Me.txtMedewerker_ID) = Me.txtMedewerker_ID Then
MsgBox ("It's an integer.")
intID = Me.txtMedewerker_ID.Value
Else
MsgBox ("Not an integer.")
Me.txtMedewerker_ID.SetFocus
End If

But if I type in an 'a' I get an errormessage that the types do not match.
What am I doing wrong?

the field me.txtMedewerker_ID is Unbound.
 
use an input mask to save you the hassle of validation.
 
Try this instead (this is "air code" so it hasn't been tested):
Code:
If IsNumeric(Me.txtMedewerker_ID) And Instr(1, ".", Me.txtMedewerker_ID, vbTextCompare) = 0 Then
   MsgBox ("It's an integer.")
   intID = CInt(Me.txtMedewerker_ID.Value)
Else
   MsgBox ("Not an integer.")
   Me.txtMedewerker_ID.SetFocus
End If
 
I would say Dennisk's suggestion would be better though.
 
Thanks guys.

Bob, I tried your code and it works just fine.
I just dont get the instr statement and why you use it.

I also would like to check that only whole numbers are allowed like 1 or 10 etc. NOT 1.2 or 12,4.

Anyway I just want the user to input full numbers.
 
Last edited:
Thanks guys.

Bob, I tried your code and it works just fine.
I just dont get the instr statement and why you use it.

I also would like to check that only whole numbers are allowed like 1 or 10 etc. NOT 1.2 or 12,4.

Anyway I just want the user to input full numbers.

I used the Instr to check for a period (you may want to include the comma if your numbers are used like that. I went off of my U.S. based numbers). If there is a period then there is something following and that means unless it is a zero (and Access won't store a number with a .0 unless it is text) there is a decimal and therefore not a whole number. So, that's why I tested for it so that it would not allow it.
 
I used the Instr to check for a period (you may want to include the comma if your numbers are used like that. I went off of my U.S. based numbers). If there is a period then there is something following and that means unless it is a zero (and Access won't store a number with a .0 unless it is text) there is a decimal and therefore not a whole number. So, that's why I tested for it so that it would not allow it.

Dear Bob,

Thanks again for your quick reply.

I adjusted your code so now it looks like this:

Code:
Private Sub txtMedewerker_ID_LostFocus()
If Me.txtMedewerker_ID = "" Or IsNull(Me.txtMedewerker_ID) Then
  Exit Sub
Else
  If IsNumeric(Me.txtMedewerker_ID) And InStr(1, ",", Me.txtMedewerker_ID, vbTextCompare) = 0 Then
     'MsgBox ("It's an integer.")
    intID = CInt(Me.txtMedewerker_ID.Value)
  Else
    MsgBox ("Het ID van de medewerker moet een heel getal zijn!")
    Me.txtGebruikersnaam.SetFocus
    Me.txtMedewerker_ID.SetFocus
  End If
End If
End Sub

Still when I type in 1,1 or 34,5 access accepts this.
It supposed to give the error message:

Code:
MsgBox ("Het ID van de medewerker moet een heel getal zijn!")

What am I doing wrong?
 
Set a breakpoint at the IF point and F8 through it to watch what is happening and put your mouse over the parts like Me.txtMedewerker_ID after each time you press F8. It should show you the values so you can see what it is actually seeing.
 
And, sorry but I have to go to bed. It's almost 1am here in Portland. I will check this again in the morning.
 
And, sorry but I have to go to bed. It's almost 1am here in Portland. I will check this again in the morning.

I did it bob!!!!

I changed the code and now it works.

This is my code now:

Code:
Private Sub txtMedewerker_ID_LostFocus()
If Me.txtMedewerker_ID = "" Or IsNull(Me.txtMedewerker_ID) Then
  Exit Sub
Else
  If IsNumeric(Me.txtMedewerker_ID) And InStr(1, Me.txtMedewerker_ID, ",") = 0 Then
     'MsgBox ("It's an integer.")
    intID = CInt(Me.txtMedewerker_ID.Value)
  Else
    MsgBox ("Het ID van de medewerker moet een heel getal zijn!")
    Me.txtGebruikersnaam.SetFocus
    Me.txtMedewerker_ID.SetFocus
  End If
End If
End Sub

Thanks all for helping!!!
 

Users who are viewing this thread

Back
Top Bottom