|
-
Aug 3rd, 2006, 04:49 AM
#1
Thread Starter
Junior Member
[RESOLVED] need help on text box
why does Label13.Caption does not show the value of num8 instantly . instead it shows the value after i enter in the 2nd number
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim num1 As Integer
Dim num2 As Integer
Dim num3 As Integer
Dim num4 As Integer
Dim num5 As Integer
Dim num6 As Integer
Dim num7 As Integer
Dim num8 As Integer
Dim lngNumber As Long
' convert string to Long variable
lngNumber = CLng(Val(Text1.Text))
' to multiple variables
num1 = lngNumber \ 10000000
num2 = (lngNumber \ 1000000) Mod 10
num3 = (lngNumber \ 100000) Mod 10
num4 = (lngNumber \ 10000) Mod 10
num5 = (lngNumber \ 1000) Mod 10
num6 = (lngNumber \ 100) Mod 10
num7 = (lngNumber \ 10) Mod 10
num8 = lngNumber Mod 10
Label13.Caption = num8
Select Case KeyAscii
Case vbKey0 To vbKey9, vbKeyBack
'do nothing, accept the keys
Case Else
Beep 'optional
KeyAscii = 0
End Select
End Sub
-
Aug 3rd, 2006, 05:06 AM
#2
Re: need help on text box
does i show 0?
a number less than 11 does not have a remainder when divided by 10
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 3rd, 2006, 05:18 AM
#3
Fanatic Member
Re: need help on text box
I understood the problem. the problem is that the exucution takes place first and then typing. ihope u understand suppose u press 1 then it shows 0 value in label beacuse when it exucute the textbox is empty.
Dont rely only on your luck. Work hard until You get success.
vb Code:
Private sub Time_ispassing
While Me.Notgetsuccess
trygain=tryagain+1
Me.workhard
wend
end sub
-
Aug 3rd, 2006, 05:19 AM
#4
Fanatic Member
Re: need help on text box
apply this code:
VB Code:
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
Dim num1 As Integer
Dim num2 As Integer
Dim num3 As Integer
Dim num4 As Integer
Dim num5 As Integer
Dim num6 As Integer
Dim num7 As Integer
Dim num8 As Integer
Dim lngNumber As Long
' convert string to Long variable
lngNumber = CLng(Val(Text1.Text))
' to multiple variables
num1 = lngNumber \ 10000000
num2 = (lngNumber \ 1000000) Mod 10
num3 = (lngNumber \ 100000) Mod 10
num4 = (lngNumber \ 10000) Mod 10
num5 = (lngNumber \ 1000) Mod 10
num6 = (lngNumber \ 100) Mod 10
num7 = (lngNumber \ 10) Mod 10
num8 = lngNumber Mod 10
Label13.Caption = num8
Select Case KeyAscii
Case vbKey0 To vbKey9, vbKeyBack
'do nothing, accept the keys
Case Else
Beep 'optional
KeyAscii = 0
End Select
use keyup instead of keypress.
Dont rely only on your luck. Work hard until You get success.
vb Code:
Private sub Time_ispassing
While Me.Notgetsuccess
trygain=tryagain+1
Me.workhard
wend
end sub
-
Aug 3rd, 2006, 08:14 PM
#5
Thread Starter
Junior Member
Re: need help on text box
btw what does this part means ?
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
because when i use that , my code to let textbox show only numbers cant work anymore
Select Case Keyascii
Case vbKey0 To vbKey9, vbKeyBack
'do nothing, accept the keys
Case Else
Beep 'optional
Keyascii = 0
End Select
-
Aug 3rd, 2006, 09:15 PM
#6
Re: need help on text box
change your sub around, put the selectcase at the top, before your calculations
and change to
VB Code:
lngNumber = CLng(Val(TextBox1.Text & (Chr(KeyAscii))))
edit: also
VB Code:
If Not KeyAscii = 0 Then Label1.Caption = num8
Last edited by westconn1; Aug 3rd, 2006 at 09:29 PM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 3rd, 2006, 09:40 PM
#7
Thread Starter
Junior Member
Re: need help on text box
then the code look like this ?
VB Code:
Private Sub Text1_Keypress(Keyascii As Integer)
Dim num1 As Integer
Dim num2 As Integer
Dim num3 As Integer
Dim num4 As Integer
Dim num5 As Integer
Dim num6 As Integer
Dim num7 As Integer
Dim num8 As Integer
Dim lngNumber As Long
' convert string to Long variable
lngNumber = CLng(Val(TextBox1.Text & (Chr(Keyascii))))
Select Case Keyascii
Case vbKey0 To vbKey9, vbKeyBack
'do nothing, accept the keys
Case Else
Beep 'optional
Keyascii = 0
End Select
' to multiple variables
num1 = lngNumber \ 10000000
num2 = (lngNumber \ 1000000) Mod 10
num3 = (lngNumber \ 100000) Mod 10
num4 = (lngNumber \ 10000) Mod 10
num5 = (lngNumber \ 1000) Mod 10
num6 = (lngNumber \ 100) Mod 10
num7 = (lngNumber \ 10) Mod 10
num8 = lngNumber Mod 10
If Not Keyascii = 0 Then Label1.Caption = num8
End Sub
-
Aug 3rd, 2006, 09:43 PM
#8
Re: need help on text box
lngNumber = CLng(Val(TextBox1.Text & (Chr(Keyascii))))
move below select case
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 3rd, 2006, 09:45 PM
#9
Thread Starter
Junior Member
Re: need help on text box
but the code itself got error
VB Code:
lngNumber = CLng(Val(TextBox1.Text & (Chr(Keyascii))))
-
Aug 3rd, 2006, 09:49 PM
#10
Re: need help on text box
sorry change textbox1 to text1 as per your control
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 3rd, 2006, 10:00 PM
#11
Thread Starter
Junior Member
Re: need help on text box
oh thx very much ! its working now . btw what does this sentence means ? just curious.
VB Code:
lngNumber = CLng(Val(TextBox1.Text & (Chr(Keyascii))))
-
Aug 3rd, 2006, 10:07 PM
#12
Re: need help on text box
as the keyascii is not put into the textbox until after this sub is processed, as noted by vivek, this line makes the longnumber to be the content of the textbox (if any) and the keypress that has not yet been put into the textbox. keyascii is a number the ascii value of the character, the chr converts it back to the character that was pressed
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 3rd, 2006, 10:18 PM
#13
Thread Starter
Junior Member
Re: need help on text box
then how can i make num1 to num8 stop accepting anything after the 8th digit because when i enter the 9th digit , it is saved into num8 so is my code correct ?
VB Code:
If lngNumber <= 8 Then
num1 = lngNumber \ 10000000
num2 = (lngNumber \ 1000000) Mod 10
num3 = (lngNumber \ 100000) Mod 10
num4 = (lngNumber \ 10000) Mod 10
num5 = (lngNumber \ 1000) Mod 10
num6 = (lngNumber \ 100) Mod 10
num7 = (lngNumber \ 10) Mod 10
num8 = lngNumber Mod 10
End If
-
Aug 3rd, 2006, 10:31 PM
#14
Re: need help on text box
[Highlight=VB]if Len(lngnumber) >8 then
keyascii = 0 'put nothing
exit sub ' skip the rest
end if[Highlight=VB]
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 3rd, 2006, 10:43 PM
#15
Thread Starter
Junior Member
Re: need help on text box
so now my code look like this but it still has the same problem . the 9th digit i entered still saves inside num8. so i tried checking the value of "Len(lngNumber)" by using "Label41.Caption" and found out that the value is always 4 .
VB Code:
Private Sub Text1_Keypress(Keyascii As Integer)
Dim num1 As Integer
Dim num2 As Integer
Dim num3 As Integer
Dim num4 As Integer
Dim num5 As Integer
Dim num6 As Integer
Dim num7 As Integer
Dim num8 As Integer
Dim lngNumber As Long
Select Case Keyascii
Case vbKey0 To vbKey9, vbKeyBack
'do nothing, accept the keys
Case Else
Beep 'optional
Keyascii = 0
End Select
' convert string to Long variable
lngNumber = CLng(Val(Text1.Text & (Chr(Keyascii))))
' to multiple variables
num1 = lngNumber \ 10000000
num2 = (lngNumber \ 1000000) Mod 10
num3 = (lngNumber \ 100000) Mod 10
num4 = (lngNumber \ 10000) Mod 10
num5 = (lngNumber \ 1000) Mod 10
num6 = (lngNumber \ 100) Mod 10
num7 = (lngNumber \ 10) Mod 10
num8 = lngNumber Mod 10
If Len(lngNumber) > 8 Then
Keyascii = 0 'put nothing
Exit Sub ' skip the rest
End If
'this is to check the num's value
Label41.Caption = Len(lngNumber)
Label42.Caption = num2
Label43.Caption = num3
Label44.Caption = num4
Label13.Caption = num5
Label12.Caption = num6
Label24.Caption = num7
Label25.Caption = num8
End Sub
-
Aug 3rd, 2006, 10:49 PM
#16
Re: need help on text box
VB Code:
Private Sub Text1_Keypress(Keyascii As Integer)
Dim num1 As Integer
Dim num2 As Integer
Dim num3 As Integer
Dim num4 As Integer
Dim num5 As Integer
Dim num6 As Integer
Dim num7 As Integer
Dim num8 As Integer
Dim lngNumber As Long
Select Case Keyascii
Case vbKey0 To vbKey9, vbKeyBack
'do nothing, accept the keys
Case Else
Beep 'optional
Keyascii = 0
End Select
' convert string to Long variable
lngNumber = CLng(Val(Text1.Text & (Chr(Keyascii))))
[B] If Len(lngNumber) > 8 Then
Keyascii = 0 'put nothing
Exit Sub ' skip the rest
end if[/B]
' to multiple variables
num1 = lngNumber \ 10000000
num2 = (lngNumber \ 1000000) Mod 10
num3 = (lngNumber \ 100000) Mod 10
num4 = (lngNumber \ 10000) Mod 10
num5 = (lngNumber \ 1000) Mod 10
num6 = (lngNumber \ 100) Mod 10
num7 = (lngNumber \ 10) Mod 10
num8 = lngNumber Mod 10
'this is to check the num's value
Label41.Caption = Len(lngNumber)
Label42.Caption = num2
Label43.Caption = num3
Label44.Caption = num4
Label13.Caption = num5
Label12.Caption = num6
Label24.Caption = num
7
Label25.Caption = num8
End Sub[Highlight=VB]
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 3rd, 2006, 11:00 PM
#17
Thread Starter
Junior Member
Re: need help on text box
the result is the same , Len(lngNumber) value always stay at 4 so it doesnt enters the loop at all
-
Aug 3rd, 2006, 11:02 PM
#18
Re: need help on text box
try something like this:
VB Code:
Option Explicit
Private bCalc As Boolean
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case vbKey0 To vbKey9
If Len(Text1.Text) = 8 Then KeyAscii = 0: Exit Sub
Case vbKeyBack
Case Else
KeyAscii = 0: Exit Sub
End Select
bCalc = True
End Sub
Private Sub Text1_Change()
Dim lNum(7) As Long, lTemp As Long, N As Long
If bCalc Then
lTemp = CLng(Val(Text1.Text))
For N = 0 To UBound(lNum)
lNum(N) = (lTemp \ (10 ^ (UBound(lNum) - N))) Mod 10
Debug.Print lNum(N)
Next N
Debug.Print
bCalc = False
End If
End Sub
-
Aug 3rd, 2006, 11:03 PM
#19
Re: need help on text box
 Originally Posted by Jetsok
the result is the same , Len(lngNumber) value always stay at 4 so it doesnt enters the loop at all 
using the Len function on anything other than a string always returns the size of the variable in bytes. A long is always four bytes - so that's why you get 4.
-
Aug 3rd, 2006, 11:09 PM
#20
Re: need help on text box
you could use
VB Code:
if len(text1.text) > 7 then
as that would check the length correctly
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 4th, 2006, 12:12 AM
#21
Thread Starter
Junior Member
Re: need help on text box
 Originally Posted by bushmobile
try something like this:
VB Code:
Option Explicit
Private bCalc As Boolean
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case vbKey0 To vbKey9
If Len(Text1.Text) = 8 Then KeyAscii = 0: Exit Sub
Case vbKeyBack
Case Else
KeyAscii = 0: Exit Sub
End Select
bCalc = True
End Sub
Private Sub Text1_Change()
Dim lNum(7) As Long, lTemp As Long, N As Long
If bCalc Then
lTemp = CLng(Val(Text1.Text))
For N = 0 To UBound(lNum)
lNum(N) = (lTemp \ (10 ^ (UBound(lNum) - N))) Mod 10
Debug.Print lNum(N)
Next N
Debug.Print
bCalc = False
End If
End Sub
although i dont understand your code completely but this part of your code
VB Code:
Select Case KeyAscii
Case vbKey0 To vbKey9
If Len(Text1.Text) = 8 Then KeyAscii = 0: Exit Sub
Case vbKeyBack
Case Else
KeyAscii = 0: Exit Sub
End Select
help me solved the problem that "if len(text1.text) > 7" has.
thx westconn1 and bushmobile
-
Aug 4th, 2006, 06:43 AM
#22
Re: [RESOLVED] need help on text box
well i put the calculating of the numbers in the _Change event, because that way the KeyPress has already been processed by the Text box.
You're assuming with this line:
VB Code:
lngNumber = CLng(Val(Text1.Text & (Chr(Keyascii))))
that the user will always be adding text to the end of the textbox - which may not be the case. Putting it in the _Change event allows you to calculate based on what is actually there rather than your assumption.
I also used a array rather than individual variables number num1 to num8
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
|