[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
Re: need help on text box
does i show 0?
a number less than 11 does not have a remainder when divided by 10
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.
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.
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
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
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
Re: need help on text box
Quote:
lngNumber = CLng(Val(TextBox1.Text & (Chr(Keyascii))))
move below select case
Re: need help on text box
but the code itself got error
VB Code:
lngNumber = CLng(Val(TextBox1.Text & (Chr(Keyascii))))
Re: need help on text box
sorry change textbox1 to text1 as per your control
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))))
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
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
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]
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
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]
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 :(
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
Re: need help on text box
Quote:
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.
Re: need help on text box
you could use
VB Code:
if len(text1.text) > 7 then
as that would check the length correctly
Re: need help on text box
Quote:
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
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