-
May 24th, 2012, 07:18 PM
#1
Thread Starter
Addicted Member
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.
-
May 24th, 2012, 08:07 PM
#2
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.
-
May 24th, 2012, 08:17 PM
#3
Thread Starter
Addicted Member
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.
-
May 24th, 2012, 08:24 PM
#4
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.
-
May 24th, 2012, 08:29 PM
#5
Thread Starter
Addicted Member
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.
-
May 24th, 2012, 08:31 PM
#6
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
-
May 24th, 2012, 08:39 PM
#7
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.
-
May 24th, 2012, 08:43 PM
#8
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
-
May 24th, 2012, 08:45 PM
#9
Re: why variable return 0
Originally Posted by Nightwalker83
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.
-
May 24th, 2012, 08:55 PM
#10
Thread Starter
Addicted Member
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.
-
May 24th, 2012, 09:05 PM
#11
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.
-
May 24th, 2012, 09:55 PM
#12
Thread Starter
Addicted Member
Re: why variable return 0
i think the problem is not with text Change event its with my code
-
May 24th, 2012, 10:26 PM
#13
Re: why variable return 0
Originally Posted by bordino
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
-
May 24th, 2012, 11:02 PM
#14
Re: why variable return 0
Originally Posted by bordino
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.
-
May 24th, 2012, 11:13 PM
#15
Thread Starter
Addicted Member
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
-
May 24th, 2012, 11:24 PM
#16
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.
-
May 24th, 2012, 11:32 PM
#17
Thread Starter
Addicted Member
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
-
May 25th, 2012, 12:08 AM
#18
Re: why variable return 0
there is an easier way than this but here:
vb Code:
Private Function converttext(ByVal TextBox_Name As TextBox) As String
Dim Pos As Integer
Dim Count As Integer
Dim CharCode As Integer
Count = Len(TextBox_Name.text)
For Pos = 1 To Count
CharCode = AscW(Mid(TextBox_Name.text, Pos, 1))
Select Case CharCode
Case Is = 32
Case Is <> 32
'MsgBox CharCode
Text2.text = Right(Text2.text, Pos - 1) & Chr(CharCode)
'Exit For
End Select
Next Pos
End Function
Private Sub Text1_Change()
converttext Text1
End Sub
-
May 25th, 2012, 06:30 AM
#19
Thread Starter
Addicted Member
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.
-
May 25th, 2012, 07:12 AM
#20
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
-
May 25th, 2012, 09:29 AM
#21
Re: why variable return 0
Originally Posted by bordino
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.
-
May 25th, 2012, 11:26 AM
#22
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|