|
-
Apr 19th, 2005, 07:48 PM
#1
Thread Starter
Lively Member
validation(resolved)
hi,
I want to do a validation on a textbox. it only accept numeric from -20 to 40.
below is part of my coding but i cant do the validation on the length and only accept -20 to 40. Please help.
Public Sub Val_Temp(TextObj As TextBox)
Dim i As Integer, n As Integer
Dim s As String, Char As Byte
i = TextObj.SelStart
For n = 1 To Len(TextObj)
Char = Asc(Mid(TextObj, n, 1))
If ((Char >= 48 And Char <= 57) Or (Char = 45 And n = 1) Then
s = s & Mid(TextObj, n, 1)
Else
i = i - 1
End If
Next n
If i > Len(s) Or i < 0 Then i = Len(s)
TextObj.Text = s
TextObj.SelStart = i
End Sub
Last edited by vivian2u; Apr 21st, 2005 at 11:33 AM.
-
Apr 19th, 2005, 08:00 PM
#2
Re: validation
How about this?
VB Code:
If TextObj < -20 Or TextObj > 40 Then
MsgBox "Invalid"
End If
-
Apr 19th, 2005, 08:40 PM
#3
Thread Starter
Lively Member
Re: validation
it cant work here because i m using
Private Sub text1_Change()
Val_Temp text1
end sub
-
Apr 19th, 2005, 08:45 PM
#4
-
Apr 19th, 2005, 08:48 PM
#5
Re: validation
VB Code:
If Len(Abs(TextObject)) = 2 Then
If TextObj < -20 Or TextObj > 40 Then
MsgBox "Invalid"
End If
Else
MsgBox "Invalid"
End If
-
Apr 19th, 2005, 09:02 PM
#6
Thread Starter
Lively Member
Re: validation
ABs cant accept "-"
if Len(Abs(TextObject)) = 2 Then
-
Apr 19th, 2005, 09:17 PM
#7
Lively Member
Re: validation
ABS accept all numerics, positive and negative, and return the absoloute value (disregards the negative sign). You may have to use Val(text) to convert from string to integer.
-
Apr 19th, 2005, 09:25 PM
#8
Thread Starter
Lively Member
Re: validation
i got compile error ..variable required - can't assign to this expression when i use Abs
-
Apr 19th, 2005, 09:33 PM
#9
Re: validation
Not sure if I'd do it this way, but to make it work, you need this:
VB Code:
If Len(Abs(val(TextObject.text))) = 2 Then
If val(TextObj.text) < -20 Or val(TextObj.text) > 40 Then
MsgBox "Invalid"
End If
Else
MsgBox "Invalid"
End If
Last edited by dglienna; Apr 19th, 2005 at 10:13 PM.
-
Apr 19th, 2005, 10:06 PM
#10
Thread Starter
Lively Member
Re: validation
it cant work.
i want a textbox validation buy using Private Sub text1_Change()
(a) it only accept numeric from -20 to 40.
(b) the "-" only allow in front
(c) if negative numeric , allow the length to 3 and not allow the user to press the 4th digit
(d) if posivive numeric, allow the length to 2 and not allow the user press the 3th digit.
my coding below is only work on (b)
Public Sub Val_Temp(TextObj As TextBox)
Dim i As Integer, n As Integer
Dim s As String, Char As Byte
i = TextObj.SelStart
For n = 1 To Len(TextObj)
Char = Asc(Mid(TextObj, n, 1))
If ((Char >= 48 And Char <= 57) Or (Char = 45 And n = 1) Then
s = s & Mid(TextObj, n, 1)
Else
i = i - 1
End If
Next n
If i > Len(s) Or i < 0 Then i = Len(s)
TextObj.Text = s
TextObj.SelStart = i
End Sub
-
Apr 19th, 2005, 10:14 PM
#11
Re: validation
My routine won't work in Text_Change(), but it could be in Text_Validate(), or even Text_LostFocus()
-
Apr 19th, 2005, 10:20 PM
#12
Thread Starter
Lively Member
Re: validation
but i want to have the validation in Text_Change(), any idea??
-
Apr 19th, 2005, 10:49 PM
#13
Re: validation
You could check
1) for numerics and no decimal points
2) the '-' sign only at position 1
3) no more than 2 characters, unless #2 is true
it will fire each time something is put into or taken out of the textbox
-
Apr 19th, 2005, 10:56 PM
#14
Thread Starter
Lively Member
Re: validation
can someone give me the coding in :
3) no more than 2 characters, unless #2 is true
-
Apr 19th, 2005, 11:51 PM
#15
Re: validation
There are too many combinations for the change event. I've tried 3 different approaches, and each one crashed.
Why don't you want to use the keypress event? or the lost_focus event?
It would be much cleaner to code.
The change event fires when you hit backspace, or delete, and the length can be 0, but you can't use 0 in the mid statement. you can't compare the length to the previous instance either.
Last edited by dglienna; Apr 20th, 2005 at 12:02 AM.
-
Apr 20th, 2005, 12:09 AM
#16
Thread Starter
Lively Member
Re: validation
i thinking of the keypress even now... you are right...
hows the coding in keypress even ?
-
Apr 20th, 2005, 12:24 AM
#17
Re: validation
Here is a start. I wasted too much time on the other mess trying to make it work. I'll look at it again tomorrow if you can't figure it out.
VB Code:
Option Explicit
Private Sub Form_Load()
Text1.Text = ""
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 49 Or KeyAscii > 57 Then
KeyAscii = 0
Else
If KeyAscii = 45 And Text1.Text <> "" Then
KeyAscii = 0
End If
End If
End Sub
-
Apr 20th, 2005, 12:45 AM
#18
Thread Starter
Lively Member
Re: validation
cant work...its not accept '-', because of
If KeyAscii < 49 Or KeyAscii > 57 Then
KeyAscii = 0
-
Apr 20th, 2005, 01:05 AM
#19
Re: validation
keyascii = 45 is the '-'
i took that one out.
-
Apr 20th, 2005, 01:27 AM
#20
Re: validation
Give this a go.
VB Code:
Option Explicit
Dim IsNegative As Boolean
Private Sub Text1_Change()
If Left$(Text1.Text, 1) = "-" Then
Text1.MaxLength = 3
IsNegative = True
Else
Text1.MaxLength = 2
IsNegative = False
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case vbKey0 To vbKey9 ' These keys are OK
If Len(Text1.Text) = 1 Then
If Not IsNegative Then
If Val(Text1.Text) >= 4 Then
If KeyAscii > vbKey0 Then
KeyAscii = 0
End If
End If
End If
Else
If Abs(Val(Text1.Text)) >= 2 Then
If KeyAscii > vbKey0 Then
KeyAscii = 0
End If
End If
End If
Case 45 ' 45 is '-'
If Len(Text1.Text) > 0 Then
KeyAscii = 0
End If
Case vbKeyBack, vbKeyDelete ' Allow backspace & delete
Case Else
KeyAscii = 0
End Select
End Sub
Last edited by pnish; Apr 20th, 2005 at 01:31 AM.
Pete
No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.
-
Apr 20th, 2005, 03:15 AM
#21
Thread Starter
Lively Member
Re: validation
this is exactly what i want but when it is negative it accept '0' for
-30 ,-40, -50, -60, -70, -80 and -90.
how can i correct this errors.
thanks
-
Apr 20th, 2005, 05:49 PM
#22
Re: validation
Did you try and figure out why it wasn't working properly? I've amended the code, this should do what you want.
VB Code:
Option Explicit
Dim IsNegative As Boolean
Private Sub Text1_Change()
If Left$(Text1.Text, 1) = "-" Then
Text1.MaxLength = 3
IsNegative = True
Else
Text1.MaxLength = 2
IsNegative = False
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case vbKey0 To vbKey9 ' These keys are OK
If Len(Text1.Text) = 1 Then
If Not IsNegative Then
If Val(Text1.Text) > 4 Then
KeyAscii = 0
ElseIf Val(Text1.Text) = 4 Then
If KeyAscii > vbKey0 Then
KeyAscii = 0
End If
End If
End If
Else
If Abs(Val(Text1.Text)) > 2 Then
KeyAscii = 0
ElseIf Abs(Val(Text1.Text)) = 2 Then
If KeyAscii > vbKey0 Then
KeyAscii = 0
End If
End If
End If
Case 45 ' 45 is '-'
If Len(Text1.Text) > 0 Then
KeyAscii = 0
End If
Case vbKeyBack, vbKeyDelete ' Allow backspace & delete
Case Else
KeyAscii = 0
End Select
End Sub
I know the code is sorely lacking in comments, so why don't you spend some time stepping through the code to try and understand how it works. You may surprise yourself and come up with a better way to do it. 
BTW There is still a subtle logic error in the code. See if you can figure out what it is and then fix it.
Last edited by pnish; Apr 20th, 2005 at 05:53 PM.
Pete
No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.
-
Apr 21st, 2005, 11:31 AM
#23
Thread Starter
Lively Member
Re: validation
yes, this is exactly what i want and it works perfectly. Thanks again
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
|