Auto capitalize letters in bounded forms
i have a bounded, continuous form with some textbox that users can modify. I want to use some code to auto capitalize what ever the users typed, like
开发者_开发问答Private Sub textbox_BeforeUpdate(Cancel As Integer)
Me.textbox = UCase(Me.textbox)
End Sub
but it causes error... Any suggestions?
And I noticed that in a bounded, continuous form, I can't move the pointer in its recordset.
Me.Recordset.MoveNext
This causes error (number 3462). Why?
Put the UCase expression in the text box control's After Update event.
Private Sub textbox_AfterUpdate()
Me.textbox = UCase(Me.textbox)
End Sub
As for the second question, this works without error for a command button on my form.
Private Sub cmdMoveNext_Click()
Me.Recordset.MoveNext
End Sub
This is what I get in the Immediate Window for your error number.
? AccessError(3462)
Failure to load a DLL.
Is that the error text you see? If so, I don't know the cause, but I would wonder if your database is corrupt. Can you re-create a simplified version of this form in a new database to see whether it works there? If it also fails then, perhaps your Access installation is broken.
Frankly, I'm just grasping at straws. This seems very unusual to me.
For the first question: Use the Keypress event to check for lower case letter and then return it's upper case counter-part:
Private Sub textbox_KeyPress(KeyAscii As Integer)
Const ASCII_LOWER_A = 97
Const ASCII_LOWER_Z = 122
Const UPPER_MODIFIER = -32
If KeyAscii >= ASCII_LOWER_A And KeyAscii <= ASCII_LOWER_Z Then
KeyAscii = KeyAscii + UPPER_MODIFIER
End If
End Sub
UPDATE: Since I received criticism and a downvote for my answer, I will elaborate on when and why you might use the code above.
First, let me say that the *After_Update* event fires after the control tied to that event loses focus (either via tab or actually moving to the next record). *After_Update* also recognizes a paste if the user cuts and pastes text; admittedly, my code does not. If your only concern is storing the text in the database in upper case, by all means use *After_Update* to UCase the value.
However, there is one advantage that *KEY_PRESS* press will give you: the ability to correct the letter pressed in real time.
To the purist developer, this event may seem wasteful. However, anyone who’s received a paycheck from a company should recognize that keeping their paying client happy is imperative. If that paid client says “I want to see capital letters when the user starts typing”, then you would use the code above. It will have little (if any) impact on how the application behaves and will meet your boss's requirement.
In fact, maybe the client says “Hey, this field is for putting in serial numbers for our inventory. All of products’ serial numbers use zeros, not the letter O. So please make sure they do not enter O for this field.”
Now *KEY_PRESS* has an additional use. You could either negate the letter O being pressed or you can replace the letter O getting pressed by the value you need:
Private Sub TextBox_KeyPress(KeyAscii As Integer)
Const ASCII_LOWER_A = 97
Const ASCII_LOWER_Z = 122
Const UPPER_MODIFIER = -32
Const UPPER_O = 79
Const LOWER_O = 111
Const ZERO_ASCII = 48
Select Case KeyAscii
Case UPPER_O, LOWER_O
KeyAscii = 0 'Cancel it out
'KeyAscii = ZERO_ASCII 'OR Replace it with the number zero
Case ASCII_LOWER_A To ASCII_LOWER_Z
KeyAscii = KeyAscii + UPPER_MODIFIER 'The old familiar code that will capitalize all other letters
Case Else
'Whatever other validations you want to do on the textbox here.
End Select
End Sub
So what about cutting and pasting invalid values? That is what Form_BeforeUpdate event is for. You can check all values before you save and set Cancel = True if something doesn’t jive.
It’s a very useful event and completly applicable if you want to UCase letters in real time.
Have you tried putting it in the TextBox1_Change
Sub ???
Try
Private Sub textbox_BeforeUpdate(Cancel As Integer)
Dim strBuffer As String
strBuffer = Me.textbox
Me.textbox.Focus
Me.textbox = UCase(strBuffer)
End Sub
精彩评论