|
-
Mar 15th, 2000, 12:29 PM
#1
Thread Starter
New Member
Where in my code can i insert a msgbox to tell the user that only numbers are allowed? I put the code in keypress but its not working. Where do you think should i put this code?
Option Explicit
Private Declare Function GetWindowLong& Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long)
Private Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
Private Const ES_NUMBER = &H2000&
Private Const GWL_STYLE = (-16)
Private Sub AccsNo_Keypress()
Dim tmpValue&
Dim fAlignment&
Dim ret&
Dim flag
fAlignment& = ES_NUMBER
tmpValue& = GetWindowLong&(AccsNo, GWL_STYLE)
If flag Then
tmpValue& = tmpValue& Or ES_NUMBER
Else
tmpValue& = tmpValue& And (Not ES_NUMBER)
End If
ret& = SetWindowLong&(AccsNo, GWL_STYLE, tmpValue& Or fAlignment&)
End Sub
I badly need a reply soon...
shei
-
Mar 15th, 2000, 12:49 PM
#2
Weird...try putting it into the LostFocus sub, with the text box.setfocus.
or simple
Private Sub Text.LostFocus()
if not(num(text)) then
MsgBox "Field must be numeric"
text.setfocus
end if
End Sub
or Use a masked edit box, and set type to integer or whatever.
Hope it helps
-
Mar 15th, 2000, 01:32 PM
#3
New Member
you can try an isnumeric statement
I hope this is right its been a while since I've used this statement, and I don't have vb installed on this computer so I don't know if this works, but it should
Private Sub Text1_Change()
If IsNumeric(Text1.Text) = False Then
MsgBox "only numbers are supported"
End If
End Sub
-
Mar 15th, 2000, 08:46 PM
#4
Lively Member
Hi,
I've got a little example here which I used recently.
Hope it helps you ...
Nina
-----------------------------------------------------------
Private Sub Text1_KeyPress(KeyAscii As Integer)
'Ignores all Keys that are not numbers or the backspace key
If (KeyAscii < 48 Or KeyAscii > 57) Then
If (Not (KeyAscii = vbKeyBack)) Then
KeyAscii = 0
' You might want to add a messagebox here which tells the user that he pressed an invalid key
End If
End If
End Sub
Private Sub Text1_Validate(KeepFocus As Boolean)
'Checks if the value of the textbox is
If Val(Text1) <= 0 Then
KeepFocus = True
MsgBox "Number has to be greater than 0", vbOKOnly, "Entry not valid"
Exit Sub
End If
'Checks if no entry was made
If Text1.Text = "" Then
KeepFocus = True
MsgBox "Enter Positve Integer", , "Entry not valid"
End If
End Sub
-
Mar 16th, 2000, 11:23 AM
#5
Member
Hai,
If you want to restrict only for numeric values even sometimes the IsNumeric function is also not so useful.Since if you use this function you can enter + and - signs also.
If you want restrict for only numeric values you can use asc(chr) function.
thanks
karun
-
Mar 16th, 2000, 11:59 AM
#6
You can simply use KeyPress event of the textbox to restrict it to numbers:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) Then
KeyAscii = 0
End If
End Sub
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
|