Results 1 to 22 of 22

Thread: why variable return 0

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    251

    why variable return 0

    Hi everyone I have a problem when i press space and tow character to follow i get 0 value in variable charcode1 marked in red instead of the ascii code



    any help and suggestions are welcome


    Code:
    Dim right As Boolean
    Private Function converttext(ByVal text As String) As String
    
    Dim pos As Integer
    Dim count As Integer
    Dim charcode As Integer
    Dim charcode1 As Integer
    
    
    count = Len(Text1.text)
    
    For pos = 1 To count
    
    charcode = AscW(Mid(text, pos, 1))
    
    If charcode = 32 Then
    
    right = True
    
    GoTo here
    
     ElseIf right = False Then
    MsgBox charcode1
    
    Else
    
    End If
    
    If charcode <> 32 Then
    
    right = False
    End If
    here:
    charcode1 = charcode
    Text2.text = Text2.text & Chr(charcode1)
    
    Next pos
    
    End Function
    
    Private Sub Text1_Change()
     converttext (Text1.text)
    Text1.text = ""
     
    End Sub
    Last edited by bordino; May 24th, 2012 at 07:21 PM.

  2. #2
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: why variable return 0

    Right is VB function. Use something else

    Example:

    If Right("ABCDEF", 1) = "F" Then
    '
    ' Do something
    '
    End If
    Last edited by jmsrickland; May 24th, 2012 at 08:12 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    251

    Re: why variable return 0

    still i get 0 out of charcode1 i changed right to goright still the same
    Code:
    Dim goright As Boolean
    Private Function converttext(ByVal text As String) As String
    
    Dim pos As Integer
    Dim count As Integer
    Dim charcode As Integer
    Dim charcode1 As Integer
    
    
    count = Len(Text1.text)
    
    For pos = 1 To count
    
    charcode = AscW(Mid(text, pos, 1))
    
    If charcode = 32 Then
    
    goright = True
    
    GoTo here
    
     ElseIf goright = False Then
    
    MsgBox charcode1
    
    Else
    
    End If
    
    If charcode <> 32 Then
    
    goright = False
    End If
    here:
    charcode1 = charcode
    Text2.text = Text2.text & Chr(charcode1)
    
    Next pos
    
    End Function
    
    Private Sub Text1_Change()
     converttext (Text1.text)
    Text1.text = ""
     
    End Sub
    Last edited by bordino; May 24th, 2012 at 08:22 PM.

  4. #4
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: why variable return 0

    Everytime you enter a single character in text1 it enters your routine and charcode1 never gets initialized unless the character code is 32 then because only one character was entered it exits the sub.

    So, if character in Text1 is anything but SPACE then charcode1 = 0. When Text1 is a SPACE then charcode1 = 32 but the sub is exited and it never returns to MsgBox.
    Last edited by jmsrickland; May 24th, 2012 at 08:35 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    251

    Re: why variable return 0

    charcode1 holds the previous ascii character charcode holds the second ascii character
    the charcode1 holds the ascii just before the next pos and then charcode1 return 0 i dont know why
    Last edited by bordino; May 24th, 2012 at 08:34 PM.

  6. #6
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: why variable return 0

    @ bordino,

    When I try your code in the above post (post #3) it displays "0" at first but it loops though the value of charcode displaying the other values to.

    Example:

    If I have "Text1" in the text box in the message box the numbers shown are: "0", "84", "101", "120" and "116"
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  7. #7
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: why variable return 0

    If I have "Text1" in the text box in the message box the numbers shown are: "0", "84", "101", "120" and "116"

    That's true because "Text1" was already in Text1 but you cannot type in more than 1 character at a time and therefore his sub only test for 1 character,


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  8. #8
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: why variable return 0

    No, it works for me when I tried it with other text in the text box mind you I put the code from the text1_changed event into a command button instead.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  9. #9
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: why variable return 0

    Quote Originally Posted by Nightwalker83 View Post
    No, it works for me when I tried it with other text in the text box mind you I put the code from the text1_changed event into a command button instead.
    Well, that may be one way to solve the problem but not the way he coded it and I just tested it verbatum to tell him why he gets 0.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    251

    Re: why variable return 0

    i put the text into the command button and it works .
    thanks
    not works with Text1_Change event when i enter into text1 how do i modify the code to work with
    Text Change Event same as the text in command button




    Code:
    Dim goright As Boolean
    Private Function converttext(ByVal text As String) As String
    
    Dim pos As Integer
    Dim count As Integer
    Dim charcode As Integer
    Dim charcode1 As Integer
    
    
    count = Len(Text1.text)
    
    For pos = 1 To count
    
    charcode = AscW(Mid(text, pos, 1))
    
    If charcode = 32 Then
    
    goright = True
    
    GoTo here
    
     ElseIf goright = False Then
    
    MsgBox charcode1
    
    Else
    If charcode <> 32 Then
    
    goright = False
    End If
    End If
    
    
    here:
    charcode1 = charcode
    
    Text2.text = Text2.text & Chr(charcode1)
    
    Next pos
    
    End Function
    
    Private Sub Command1_Click()
    Text1.text = Chr(32) & Chr(98) & Chr(97)
    End Sub
    
    Private Sub Text1_Change()
     converttext (Text1.text)
    Text1.text = ""
     
    End Sub
    Last edited by bordino; May 24th, 2012 at 09:06 PM.

  11. #11
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: why variable return 0

    I don't think you want to use the Change event as that will be triggered everytime you type in a single character.
    Would be better to use the KeyPress event since the Change event does not tell you what key was pressed but the KeyPress does. You don't want to test for 13 anyway.

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
     If KeyAscii = 13 Then                    '<------CHECK FOR THE ENTER PRESSED
       converttext (Text1.text)
       Text1.text = ""
     End If
    End Sub
    Last edited by jmsrickland; May 24th, 2012 at 09:30 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    251

    Re: why variable return 0

    i think the problem is not with text Change event its with my code

  13. #13
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: why variable return 0

    Quote Originally Posted by bordino View Post
    i put the text into the command button and it works .
    thanks
    not works with Text1_Change event when i enter into text1 how do i modify the code to work with
    Text Change Event same as the text in command button
    You say it works in the command button but you want to use it in the Text1_Change why are you doing like that for?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  14. #14
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: why variable return 0

    Quote Originally Posted by bordino View Post
    i think the problem is not with text Change event its with my code
    Sure it is. The Change event will call your routine when you enter the first character. Then you clear it out. That is your problem. When your routine is called with more than one character in Text1 your routine works. You said it works, Nightwalker said it works, and I know it works as long as more than one character is in Text1. I'm not saying your routine code is great because it isn't and it could be improved but this has nothing to do with your problem. Well, it does in a way. It means because of the way you wrote your code you cannot process just one character; you should correct that part


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    251

    Re: why variable return 0

    manage to solve the problem but very messy code

    could you guys help me to do it in a better way

    Code:
    Dim goright As Boolean
    
    Private Function converttext(ByVal text As String) As String
    
    Dim pos As Integer
    Dim count As Integer
    Dim CharCode As Integer
    Dim charcode1 As Integer
    
    
    count = Len(Text1.text)
    
    For pos = 1 To count
    
    CharCode = AscW(Mid(text, pos, 1))
    
    If CharCode = 32 Then
    
    goright = True
    
    
    GoTo here
    
     ElseIf goright = False Then
     
    MsgBox charcode1
    
    GoTo here2
    Else
    
    End If
    
    If CharCode <> 32 Then
    
    goright = False
    charcode1 = CharCode
    GoTo here3
    End If
    here:
    charcode1 = CharCode
    here2:
    Text2.text = Text2.text & Chr(charcode1)
    here3:
    Next pos
    
    End Function
    Private Sub Text1_Change()
    converttext (Text1.text)
       
    End Sub

  16. #16
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: why variable return 0

    I don't see where that corrected anything. I'm not even sure exactly what you want to achive. I know what you are doing but what is it you want in the final results?

    Also, you need to get out of the habit of using GoTo's. That makes for very poor programming practice.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    251

    Re: why variable return 0

    press space and tow character to follow what i want is charcode1 return the previous ascii character
    it works is there a better way to do it

  18. #18
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: why variable return 0

    there is an easier way than this but here:

    vb Code:
    1. Private Function converttext(ByVal TextBox_Name As TextBox) As String
    2. Dim Pos As Integer
    3. Dim Count As Integer
    4. Dim CharCode As Integer
    5.  
    6. Count = Len(TextBox_Name.text)
    7.  
    8. For Pos = 1 To Count
    9.  
    10. CharCode = AscW(Mid(TextBox_Name.text, Pos, 1))
    11.  
    12.    Select Case CharCode
    13.      
    14.       Case Is = 32
    15.  
    16.       Case Is <> 32
    17.          'MsgBox CharCode
    18.          Text2.text = Right(Text2.text, Pos - 1) & Chr(CharCode)
    19.          'Exit For
    20.    End Select
    21. Next Pos
    22.  
    23. End Function
    24.  
    25.  
    26. Private Sub Text1_Change()
    27. converttext Text1
    28. End Sub

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    251

    Re: why variable return 0

    is this not what i'm looking for if you see my code i created a Boolean to = true if ascii 32 else
    false so, when i press space ascii 32 goright = true else goright = flase then on
    next loop
    ElseIf goright = False Then

    MsgBox charcode1

    GoTo here2

    my code works great just want to know if i could do it easier way.

  20. #20
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: why variable return 0

    You want people to suggest a "better" way... but it' isn't CLEAR what it is that you want the code to do...

    What is it you're trying to do - from a USER POINT of view, not from a code view... Are you trying to limit what the user can enter? Or what? Until we know what the end result SHOULD be, I don't think anyone can suggest a better way.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  21. #21
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: why variable return 0

    Quote Originally Posted by bordino View Post
    is this not what i'm looking for if you see my code i created a Boolean to = true if ascii 32 else
    false so, when i press space ascii 32 goright = true else goright = flase then on
    next loop
    ElseIf goright = False Then

    MsgBox charcode1

    GoTo here2

    my code works great just want to know if i could do it easier way.
    I ran your code from what you posted at post #15 and it is no better than your original code; it still returns 0 at every character - it does not put characters in Text2.

    Here's what it is doing.

    If Text1 already contains text, let's say it already has in it "Text1" which is what is in it at startup. Then when you add a SPACE at the end then you get the MsgBox 6 times with a 0 each for each of the characters plus the SPACE you added.

    0 0 0 0 0 0

    If you add a SPACE at the beginning you get the MsgBox 4 times with the number 84 for each time.

    84 84 84 84

    If you insert a SPACE in the middle you get this

    0 0 120 120

    If Text1 is nothing at startup and you type in characters , for example you type in a, b, c, d one letter at a time you get this

    a you get 0
    b you get 0 0
    c you get 0 0 0
    d you get 0 0 0 0

    So, what do you mean it works?

    Like I said in post #16 what are you trying to achive? Not what your code is doing but what is your end results supposed to do?

    Since you insist on using the Change event I assume you want to process each character entered in Text1 one character at a time, right.

    So, if I enter the letter "A" then what do you want to happen? What do you want MsgBox to show and what do you want in Text2?

    Now I type in a "B". what do you want MsgBox to show and what do you want in Text2?

    Now I enter a SPACE. Now what is supposed to happen?.
    Last edited by jmsrickland; May 25th, 2012 at 10:37 AM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  22. #22
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: why variable return 0

    Here's what takes place when you type into Text1 the following: "ab "

    For the letter 'a' it does this:
    Code:
    Private Function converttext(ByVal text As String) As String
      '
      '
    count = Len(Text1.text)                   ' count = 1
      '
      '
    CharCode = AscW(Mid(text, pos, 1))        ' CharCode = 97
      '
      '
    ElseIf goright = False Then               'goright = False
    MsgBox charcode1                          'charcode = 0
    GoTo here2
    here2:
    Text2.text = Text2.text & Chr(charcode1)  ' charcode1 = 0 nothing addded to Text2
    here3:
    Next pos                                  ' Exits the sub
       '
    End Function
    Now when I add the letter 'b' it goes through the same steps as above then it loops back for the letter 'b' and goes through the same steps again and then it exits.

    When I type in a SPACE character it repeats exactly the same steps as it did above and then for the SPACE it does this:

    Code:
    Private Function converttext(ByVal text As String) As String
      '
      '
    CharCode = AscW(Mid(text, pos, 1))          '       CharCode = 32
      '
    If CharCode = 32 Then
    goright = True
    GoTo here
      '
      '
    here:
    charcode1 = CharCode                        'charcode1 = 32
    here2:
    Text2.text = Text2.text & Chr(charcode1)    'SPACE added to end of "ab" = "ab "
    here3:
    Next pos                                    'Exits the function
    
    End Function
    Now, you say this works, right.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

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