Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop without Do 1

Status
Not open for further replies.

Ireland1978

Programmer
Sep 29, 2005
17
GB
Code:
Private Sub txtName_KeyPress(KeyAscii As Integer)
    Dim i As Integer
    i = 0
   
    Do
        If KeyAscii >= "a" And KeyAscii <= "z" Then
            If i = 0 Then
                UCase (Chr$(KeyAscii))
            End If
        i = i + 1
        Else
            If KeyAscii = " " Then
                i = 0
            End If
    Loop While txtName.Text
            
End Sub

I'm trying to capatilize the first letter of each word entered by the user into a text box. They must be capatilized as they are entered. I've written the above Do loop, but when I run it I get the following message: Compile error: Loop without Do. I'm trying to get it to keep looping while text is being entered.

I've also tried doing this with an array, but it got messy....

Thanks in advance for any help.
 
You forgot to close the if statement inside

Private Sub txtName_KeyPress(KeyAscii As Integer)
Dim i As Integer
i = 0

Do
If KeyAscii >= "a" And KeyAscii <= "z" Then
If i = 0 Then
UCase (Chr$(KeyAscii))
End If
i = i + 1
Else
If KeyAscii = " " Then
i = 0
End If
END IF
Loop While txtName.Text

End Sub
 
Thanks for that.
I've made some changes, and my code now looks like this:
Code:
Private Sub txtName_KeyPress(KeyAscii As Integer)
    Dim i As Integer
    i = 0
    
    Do
        If KeyAscii >= 97 And KeyAscii <= 122 Then
            If i = 0 Then
                UCase (Chr$(KeyAscii))
            End If
        i = i + 1
        Else
            If KeyAscii = 32 Then
                i = 0
            End If
        End If
    Loop While txtName.Text
            
End Sub

I'm now getting the following message: Invalid procedure call or argument in reference to the following line:
Code:
 Loop while txtName.Text
What I'm trying to do in this line is keep looping while text is being entered, text includes the space bar.
 
Wouldn't it be easier to do:

Code:
Private Sub txtName_LostFocus()

txtName.Text = StrConv(txtName.Text, vbProperCase)

End Sub
???

HTH

---- Andy
 
In that case, since he has to display things as the keys keys are pressed, one could try:

Private Sub txtName_KeyUp(KeyCode As Integer, Shift As Integer)
Dim pos As Integer
pos = txtName.SelStart
txtName.Text = StrConv(txtName.Text, vbProperCase)
txtName.SelStart = pos
End Sub

Unless you want to catch it even earlier.
-Max
 
Andrzejek's suggestion does what you want using a VB builtin function and I suggest that you go with that.

As to your code ...
Code:
UCase (Chr$(KeyAscii))
is a function that returns a value and you are not capturing that value so nothing is being changed.

Code:
Loop While txtName.Text
The "Loop" statement is expecting some condition that evaluates to False (terminate the DO) or True (keep looping).

"txtName.Txt" however is a text string that has no ready interpretation as True or False
 
You should be aware that this variant of StrConv also forces a conversion to lower case for all the characters that are not at the beginning of a word. In other word, "seattle, USA" is converted to "Seattle, Usa", "O'Connor" to "O'connor", McDonald" to "Mcdonald", "Ireland de Fries" to "Ireland De Fries", and so on, which might not be desirable. Therefore you still need a custom routine, but you can take advantage of StrConv capabilities to reduce the amount of code you need:
Code:
Function ProperCase(text As String) As String
    Dim i As Integer

    ' prepare the result
    ProperCase = StrConv(text, vbProperCase)

    ' restore all those characters that were capitalized
    For i = 1 To Len(text)
        Select Case Asc(Mid$(text, i, 1))
            Case 65 To 90   ' A-Z
                Mid$(ProperCase, i, 1) = Mid$(text, i, 1)
        End Select
    Next
End Function

--- Andy

PS. This code is not mine, but what about the star for me anyway? :)
 
Wow, most of these answers seem way too complicated. How about just converting each character as it's typed in:
Code:
Private Sub txtEnter_KeyPress(KeyAscii As Integer)
    If KeyAscii >= 97 And KeyAscii <= 122 Then
        KeyAscii = KeyAscii - 32
    End If
End Sub
 
Just realized my code doesn't handle cut & paste, here's another option:
Code:
Private Sub txtEnter_Change()
    txtEnter = UCase(txtEnter)
    'put cursor at the end
    txtEnter.SelStart = Len(txtEnter)
End Sub
 
Oh shoot, just realized he wasn't going for all capitals! My apologies!

Sometimes I really wish I could delete my own posts.
 
And of course if the backspace is used without subtracting from i then the first letter in the word will be lower case.

I think you need to deduct from i if backspace is used and/or if mid(txtname.text, i - 1, 1) = chr(32) then i = 0

[gray]Experience is something you don't get until just after you need it.[/gray]
 
Thanks for all your replies, I used Shakepeare's code....and I'm a she not a he!!!
 
For future reference, Ireland:

In the keypress event, you can set as well as read the keyascii value. You seemed to be missing that fact, which is why you were using ucase and all that without holding onto the results. You could say [tt]KeyAscii = UCase(Chr$(KeyAscii))[/tt], for example.

Also, the keypress event is called once each time a key is pressed. Keyascii is the ascii value of that key. If you change keyascii, you change the value that gets typed into the text box. For example, [tt]KeyAscii = 0[/tt] changes it to a null value, and therefore has the effect of canceling the input. So, your loop concept is based on not understanding this, and what's more doesn't work even if you did.

While Shakespeare's code is fine, the keypress event will probably work too and might be a bit more concise. For exmaple, the KeyUp event fires when you press the arrow or function keys, not just keys that put a value in a text box. What's more, the numbers on the keypad send a different value to KeyUp than do the numbers in the top row of the keyboard. While this doesn't seem to be a problem in this context, it could turn into one if you don't understand what's going on. I would have tried the keypress event first. Perhaps Shakespeare did, and found a reason to go to the KeyUp instead.

HTH

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top