How do you make a text box automatically capitalize letters that are typed in?
How do you make a text box automatically capitalize letters that are typed in?
Use text box change event -
Code:Private Sub Text55_Change() On Error Resume Next DoCmd.RunCommand acCmdSaveRecord Me.Text55.Value = VBA.UCase(Me.Text55.Value) Me.Text55.SelStart = Len(Me.Text55) End Sub
Thanks. But just out of curiosity, why is there a save command in there? Also is the Text55 the actual name of the text box? And what does SelStart and Len do?
Sorry but kinda new to VBA and want to learn more.
I also found an easier way to make it caps. Go to the Format property of the text box and enter > symbol in it. The only thing I don't like about it is that it does not show the capital letters until the text box loses focus. Would you know an easy way like this to make the text box show capitals as you type them?
Text55 is name of text box whose value you wish to change to upper case.
Mark the thread as solved if your problem is solvedCode:Private Sub Text55_Change() On Error Resume Next 'if value is null access will throw error DoCmd.RunCommand acCmdSaveRecord 'if not saved, the current text cannot be retrieved in next line Me.Text55.Value = VBA.UCase(Me.Text55.Value) ' Sets the case to upper Me.Text55.SelStart = Len(Me.Text55) 'Sets the cursor to the end of text other wise it will always be at the beginning End Sub
Sure, the six lines of code amrut gave you! Writing 6 lines of code is not exactly onerous!
BTW, using > not only doesn't effect the display until you exit the Control, if focus returns to the Control the data returns to the non-capitalized version until you once again exit it. Also, with some versions of Access, under some conditions, formatting with the > (actually, anything done in the Format Property) can cause data to 'disappear.'
Linq ;0)>
The code does not work because I have some VBA already prompting me in the before update event to ask if I am sure that I want to save the changes. Can anyone give me some code that does not have to include a save command?
Yes I realized that when leaving focus is when the caps show and setting focus back to the text box it will then go back to lowercase. I don't like this and I also don't like the part you mentioned that sometimes the data can disappear. I don't want that to happen.
Code:Private Sub TextBoxName_KeyPress(KeyAscii As Integer) KeyAscii = Asc(UCase(Chr(KeyAscii))) End Sub
Just replace TextBoxName with the actual name of your Control.
Linq ;0)>
That works nice mr. Linq
But if someone copy and pastes his data in the textbox ?
I have implemented your code and im keeping my afterupdate event as well for above reasons
Code:Private Sub YourTextBox_AfterUpdate() Me.YourTextBox = UCase(Me.YourTextBox)
This is wonderful. Thank you so much. This works great on the text boxes that I type data into but I do have one text box that is locked because it updates with the user's initials when they edit the record so I don't type anything into this text box. It automatically fills. So this code doesn't work for that. Would you know how to make a text box like this locked one, be all caps too?
Thanks again for the help. This was really helpful.
Ok this is also helpful. I didn't even think about the copy and paste. But this doesn't cover an auto-fill text box. I have a few boxes that automatically fill in data when records are edited. How do you make a text box such as this, all caps?
Still need help with the auto fill text boxes. Do you know how to make those all caps? I have it locked so the user will not enter data into it. It will auto fill when the user enters certain information in other text boxes.
What is auto fill?
I have text boxes that will pull data from another text box when certain text boxes get typed into. For example, when a record is edited, I have the username from the login form transfer over to the current form that the user is entering data so I know who updated the record. Since these text boxes that the username is transferred to is not physically typed into but automatically filled in by the VBA, all the methods listed above to make text boxes capital do not work. Would you have a solution to this? In the beginning of the post it was already suggested that I use > in the format property which I heard was a bad idea because it can cause data to be lost and I also do not like how it only stays capital when it does not have focus. If you put focus on it it will then reveal lowercase till you lose focus again. Please keep that in mind if you have any suggestions. Thanks for whatever help you can provide.
You can use the fStrConv() function in VBA to format as upper case. Apply your string variable to the first argument abd the constant, vbUpperCase, to the second argument.
where MyString is the variable your existing code uses to populate the unbound textbox.
Me.TextboxName.Value = StrConv(MyString, vbUpperCase)
As you've said, most Control-related events don't fire when a Control is populated using VBA code. The trick, then, is to Call whichever event you need. So, if you're using this to change the data to upper case:
Code:Private Sub YourTextBox_AfterUpdate() Me.YourTextBox = UCase(Me.YourTextBox) End Sub
after the code that populates the Textbox, simply Call the Sub:
Code:'Code to transfer data to Textbox goes here Call YourTextBox_AfterUpdate
Linq ;0)>