|
-
Sep 14th, 2005, 12:26 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Checking Ascii Values
I have a textbox on my form with code in the "keypress" event. In this code it says something like
Well, how do I get the actual value that is stored in KeyAscii 13. I want to check each keypress to make sure the user typed in a numeric value.
Something like
VB Code:
If IsNumeric(KeyAscii.Value) Then
-
Sep 14th, 2005, 12:39 PM
#2
Re: Checking Ascii Values
You can return the character of an ascii using CHR$(13) but it won't return a character, as it is the ENDLINE CR sequence.
I usually place a breakpoint in my code and check what was pressed.
0 is chr$(48) and 9 is chr$(57)
-
Sep 14th, 2005, 12:39 PM
#3
Re: Checking Ascii Values
This should help.
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 13 Then
MsgBox "Only numbers are allowed for this entry"
KeyAscii = 0
Exit Sub
End If
End Sub
'also, in the change event put this code
'to prevent pasting in non numeric text
Private Sub Text1_Change()
If Not IsNumeric(Text1.Text) Then
MsgBox "Only Numbers Are Allowed"
Text1.Text = vbNullString
Exit Sub
End If
End Sub
-
Sep 14th, 2005, 12:42 PM
#4
Re: Checking Ascii Values
I see Hack posted, but I was talking about this:
VB Code:
Option Explicit
Private Sub Text1_KeyPress(KeyAscii As Integer)
MsgBox KeyAscii & " " & Chr$(KeyAscii)
End Sub
-
Sep 14th, 2005, 01:03 PM
#5
Re: Checking Ascii Values
 Originally Posted by The_Grudge
I have a textbox on my form with code in the "keypress" event. In this code it says something like
Well, how do I get the actual value that is stored in KeyAscii 13. I want to check each keypress to make sure the user typed in a numeric value.
Something like
VB Code:
If IsNumeric(KeyAscii.Value) Then
VB Code:
If IsNumeric(Chr(KeyAscii)) Then
-
Sep 14th, 2005, 01:19 PM
#6
Re: [RESOLVED] Checking Ascii Values
I think martinliss or Joacim wrote this originally. It only allows numbers to be entered into the text box.
VB Code:
Option Explicit
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_STYLE = (-16)
Private Const ES_NUMBER As Long = &H2000& 'or 8192 in decimal form
Private Sub Form_Load()
Dim style As Long
Text1.Text = ""
style = GetWindowLong(Text1.hwnd, GWL_STYLE)
SetWindowLong Text1.hwnd, GWL_STYLE, style Or ES_NUMBER
End Sub
-
Sep 14th, 2005, 01:19 PM
#7
Thread Starter
Fanatic Member
Re: [RESOLVED] Checking Ascii Values
Thanks Pradeep...that's exactly what I was after.
Usually Hack helps me out of my jams, good to see a new face. LOL
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|