Results 1 to 22 of 22

Thread: [RESOLVED] need help on text box

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2006
    Posts
    28

    Resolved [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

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  3. #3
    Fanatic Member vivek_master146's Avatar
    Join Date
    Apr 2006
    Location
    Delhi,India
    Posts
    787

    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:
    1. Private sub Time_ispassing
    2. While Me.Notgetsuccess
    3. trygain=tryagain+1
    4. Me.workhard
    5. wend
    6. end sub

  4. #4
    Fanatic Member vivek_master146's Avatar
    Join Date
    Apr 2006
    Location
    Delhi,India
    Posts
    787

    Re: need help on text box

    apply this code:
    VB Code:
    1. Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
    2. Dim num1 As Integer
    3. Dim num2 As Integer
    4. Dim num3 As Integer
    5. Dim num4 As Integer
    6. Dim num5 As Integer
    7. Dim num6 As Integer
    8. Dim num7 As Integer
    9. Dim num8 As Integer
    10. Dim lngNumber As Long
    11.  
    12.  
    13. ' convert string to Long variable
    14. lngNumber = CLng(Val(Text1.Text))
    15. ' to multiple variables
    16. num1 = lngNumber \ 10000000
    17. num2 = (lngNumber \ 1000000) Mod 10
    18. num3 = (lngNumber \ 100000) Mod 10
    19. num4 = (lngNumber \ 10000) Mod 10
    20. num5 = (lngNumber \ 1000) Mod 10
    21. num6 = (lngNumber \ 100) Mod 10
    22. num7 = (lngNumber \ 10) Mod 10
    23. num8 = lngNumber Mod 10
    24.  
    25. Label13.Caption = num8
    26.  
    27. Select Case KeyAscii
    28. Case vbKey0 To vbKey9, vbKeyBack
    29. 'do nothing, accept the keys
    30. Case Else
    31. Beep 'optional
    32. KeyAscii = 0
    33. End Select
    use keyup instead of keypress.
    Dont rely only on your luck. Work hard until You get success.
    vb Code:
    1. Private sub Time_ispassing
    2. While Me.Notgetsuccess
    3. trygain=tryagain+1
    4. Me.workhard
    5. wend
    6. end sub

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2006
    Posts
    28

    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

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: need help on text box

    change your sub around, put the selectcase at the top, before your calculations
    and change to
    VB Code:
    1. lngNumber = CLng(Val(TextBox1.Text & (Chr(KeyAscii))))

    edit: also
    VB Code:
    1. 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

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Aug 2006
    Posts
    28

    Re: need help on text box

    then the code look like this ?

    VB Code:
    1. Private Sub Text1_Keypress(Keyascii As Integer)
    2.  
    3. Dim num1 As Integer
    4. Dim num2 As Integer
    5. Dim num3 As Integer
    6. Dim num4 As Integer
    7. Dim num5 As Integer
    8. Dim num6 As Integer
    9. Dim num7 As Integer
    10. Dim num8 As Integer
    11. Dim lngNumber As Long
    12.  
    13.  
    14.  
    15.     ' convert string to Long variable
    16.     lngNumber = CLng(Val(TextBox1.Text & (Chr(Keyascii))))
    17.  
    18.    
    19.     Select Case Keyascii
    20.         Case vbKey0 To vbKey9, vbKeyBack
    21.             'do nothing, accept the keys
    22.         Case Else
    23.             Beep 'optional
    24.             Keyascii = 0
    25.     End Select
    26.      
    27.      ' to multiple variables
    28.     num1 = lngNumber \ 10000000
    29.     num2 = (lngNumber \ 1000000) Mod 10
    30.     num3 = (lngNumber \ 100000) Mod 10
    31.     num4 = (lngNumber \ 10000) Mod 10
    32.     num5 = (lngNumber \ 1000) Mod 10
    33.     num6 = (lngNumber \ 100) Mod 10
    34.     num7 = (lngNumber \ 10) Mod 10
    35.     num8 = lngNumber Mod 10
    36.  
    37. If Not Keyascii = 0 Then Label1.Caption = num8
    38. End Sub

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Aug 2006
    Posts
    28

    Re: need help on text box

    but the code itself got error

    VB Code:
    1. lngNumber = CLng(Val(TextBox1.Text & (Chr(Keyascii))))

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Aug 2006
    Posts
    28

    Re: need help on text box

    oh thx very much ! its working now . btw what does this sentence means ? just curious.

    VB Code:
    1. lngNumber = CLng(Val(TextBox1.Text & (Chr(Keyascii))))

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Aug 2006
    Posts
    28

    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:
    1. If lngNumber <= 8 Then
    2.     num1 = lngNumber \ 10000000
    3.     num2 = (lngNumber \ 1000000) Mod 10
    4.     num3 = (lngNumber \ 100000) Mod 10
    5.     num4 = (lngNumber \ 10000) Mod 10
    6.     num5 = (lngNumber \ 1000) Mod 10
    7.     num6 = (lngNumber \ 100) Mod 10
    8.     num7 = (lngNumber \ 10) Mod 10
    9.     num8 = lngNumber Mod 10
    10.    
    11.     End If

  14. #14
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Aug 2006
    Posts
    28

    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:
    1. Private Sub Text1_Keypress(Keyascii As Integer)
    2.  
    3. Dim num1 As Integer
    4. Dim num2 As Integer
    5. Dim num3 As Integer
    6. Dim num4 As Integer
    7. Dim num5 As Integer
    8. Dim num6 As Integer
    9. Dim num7 As Integer
    10. Dim num8 As Integer
    11. Dim lngNumber As Long
    12.    
    13.     Select Case Keyascii
    14.         Case vbKey0 To vbKey9, vbKeyBack
    15.             'do nothing, accept the keys
    16.         Case Else
    17.             Beep 'optional
    18.             Keyascii = 0
    19.     End Select
    20.      
    21.  
    22.  
    23.     ' convert string to Long variable
    24.     lngNumber = CLng(Val(Text1.Text & (Chr(Keyascii))))
    25.    
    26.  
    27.      ' to multiple variables
    28.     num1 = lngNumber \ 10000000
    29.     num2 = (lngNumber \ 1000000) Mod 10
    30.     num3 = (lngNumber \ 100000) Mod 10
    31.     num4 = (lngNumber \ 10000) Mod 10
    32.     num5 = (lngNumber \ 1000) Mod 10
    33.     num6 = (lngNumber \ 100) Mod 10
    34.     num7 = (lngNumber \ 10) Mod 10
    35.     num8 = lngNumber Mod 10
    36.    
    37.     If Len(lngNumber) > 8 Then
    38.     Keyascii = 0 'put nothing
    39.     Exit Sub ' skip the rest
    40.     End If
    41.        
    42. 'this is to check the num's value
    43.  
    44. Label41.Caption = Len(lngNumber)
    45. Label42.Caption = num2
    46. Label43.Caption = num3
    47. Label44.Caption = num4
    48. Label13.Caption = num5
    49. Label12.Caption = num6
    50. Label24.Caption = num7
    51. Label25.Caption = num8
    52.  
    53.  
    54.  
    55. End Sub

  16. #16
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: need help on text box

    VB Code:
    1. Private Sub Text1_Keypress(Keyascii As Integer)
    2.  
    3. Dim num1 As Integer
    4. Dim num2 As Integer
    5. Dim num3 As Integer
    6. Dim num4 As Integer
    7. Dim num5 As Integer
    8. Dim num6 As Integer
    9. Dim num7 As Integer
    10. Dim num8 As Integer
    11. Dim lngNumber As Long
    12.    
    13.     Select Case Keyascii
    14.         Case vbKey0 To vbKey9, vbKeyBack
    15.             'do nothing, accept the keys
    16.         Case Else
    17.             Beep 'optional
    18.             Keyascii = 0
    19.     End Select
    20.     ' convert string to Long variable
    21.     lngNumber = CLng(Val(Text1.Text & (Chr(Keyascii))))
    22.    
    23.  [B]    If Len(lngNumber) > 8 Then
    24.     Keyascii = 0 'put nothing
    25.     Exit Sub ' skip the rest
    26.     end if[/B]
    27.  
    28.        ' to multiple variables
    29.     num1 = lngNumber \ 10000000
    30.     num2 = (lngNumber \ 1000000) Mod 10
    31.     num3 = (lngNumber \ 100000) Mod 10
    32.     num4 = (lngNumber \ 10000) Mod 10
    33.     num5 = (lngNumber \ 1000) Mod 10
    34.     num6 = (lngNumber \ 100) Mod 10
    35.     num7 = (lngNumber \ 10) Mod 10
    36.     num8 = lngNumber Mod 10
    37.    
    38.  
    39.  
    40.        
    41. 'this is to check the num's value
    42.  
    43. Label41.Caption = Len(lngNumber)
    44. Label42.Caption = num2
    45. Label43.Caption = num3
    46. Label44.Caption = num4
    47. Label13.Caption = num5
    48. Label12.Caption = num6
    49. 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

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Aug 2006
    Posts
    28

    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

  18. #18
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: need help on text box

    try something like this:
    VB Code:
    1. Option Explicit
    2.  
    3. Private bCalc As Boolean
    4.  
    5. Private Sub Text1_KeyPress(KeyAscii As Integer)
    6.  
    7.     Select Case KeyAscii
    8.         Case vbKey0 To vbKey9
    9.             If Len(Text1.Text) = 8 Then KeyAscii = 0: Exit Sub
    10.         Case vbKeyBack
    11.         Case Else
    12.             KeyAscii = 0: Exit Sub
    13.     End Select
    14.     bCalc = True
    15. End Sub
    16.  
    17. Private Sub Text1_Change()
    18.     Dim lNum(7) As Long, lTemp As Long, N As Long
    19.    
    20.     If bCalc Then
    21.         lTemp = CLng(Val(Text1.Text))
    22.         For N = 0 To UBound(lNum)
    23.             lNum(N) = (lTemp \ (10 ^ (UBound(lNum) - N))) Mod 10
    24.             Debug.Print lNum(N)
    25.         Next N
    26.         Debug.Print
    27.         bCalc = False
    28.     End If
    29. End Sub

  19. #19
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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.

  20. #20
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: need help on text box

    you could use
    VB Code:
    1. 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

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Aug 2006
    Posts
    28

    Re: need help on text box

    Quote Originally Posted by bushmobile
    try something like this:
    VB Code:
    1. Option Explicit
    2.  
    3. Private bCalc As Boolean
    4.  
    5. Private Sub Text1_KeyPress(KeyAscii As Integer)
    6.  
    7.     Select Case KeyAscii
    8.         Case vbKey0 To vbKey9
    9.             If Len(Text1.Text) = 8 Then KeyAscii = 0: Exit Sub
    10.         Case vbKeyBack
    11.         Case Else
    12.             KeyAscii = 0: Exit Sub
    13.     End Select
    14.     bCalc = True
    15. End Sub
    16.  
    17. Private Sub Text1_Change()
    18.     Dim lNum(7) As Long, lTemp As Long, N As Long
    19.    
    20.     If bCalc Then
    21.         lTemp = CLng(Val(Text1.Text))
    22.         For N = 0 To UBound(lNum)
    23.             lNum(N) = (lTemp \ (10 ^ (UBound(lNum) - N))) Mod 10
    24.             Debug.Print lNum(N)
    25.         Next N
    26.         Debug.Print
    27.         bCalc = False
    28.     End If
    29. End Sub
    although i dont understand your code completely but this part of your code
    VB Code:
    1. Select Case KeyAscii
    2.         Case vbKey0 To vbKey9
    3.             If Len(Text1.Text) = 8 Then KeyAscii = 0: Exit Sub
    4.         Case vbKeyBack
    5.         Case Else
    6.             KeyAscii = 0: Exit Sub
    7.     End Select
    help me solved the problem that "if len(text1.text) > 7" has.

    thx westconn1 and bushmobile

  22. #22
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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:
    1. 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
  •  



Click Here to Expand Forum to Full Width