Always Accept in Capital in any of the text box or Combo.

I have fully developed system with more than 50 forms in it ,& n number of Text boxes & combo for entry purpose.
Now there is a requirement to accept all the values into Capital, I do not want to create control & replaces every where. Is there a way to make caps lock on as soon as user presses any key on key board.
tanvirsAsked:
Who is Participating?
 
bobbit31Connect With a Mentor Commented:
how bout this:

basically when form is activate presses capslock (if it's off) and when form is unloaded presses it again (if it's on)

Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Const KEYEVENTF_KEYUP = &H2

Private Const VK_CAPITAL = &H14


Private Sub Form_Activate()

    '' check if caps is pressed
    If GetKeyState(VK_CAPITAL) <> 1 Then
        '' turn caps on
        keybd_event VK_CAPITAL, 0, 0, 0
        keybd_event VK_CAPITAL, 0, KEYEVENTF_KEYUP, 0
    End If
   
End Sub

Private Sub Form_Unload(Cancel As Integer)
    '' be nice turn caps off
    If GetKeyState(VK_CAPITAL) = 1 Then
        keybd_event VK_CAPITAL, 0, 0, 0
        keybd_event VK_CAPITAL, 0, KEYEVENTF_KEYUP, 0
    End If

End Sub

however, i'd probably turn caps off either in form_Deactivate or lostfocus (i just can't get those events to fire in my test)

0
 
COAtheGREATCommented:
Private Sub Text1_Change()

    Text1.Text = UCase(Text1.Text)

End Sub
0
 
slaviknCommented:
Private Sub Text1_KeyPress(KeyAscii As Integer)
    If (KeyAscii >= Asc("a")) And (KeyAscii <= Asc("z")) Then
        KeyAscii = KeyAscii - 32
    End If
End Sub
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
slaviknCommented:
My example works much faster.
0
 
bobbit31Commented:
slavikn: but they'll still be able to cut and paste?
0
 
tanvirsAuthor Commented:
Please note i do not want to write code on each control.
As suggest below

Private Sub Text1_Change()

   Text1.Text = UCase(Text1.Text)

End Sub

0
 
tanvirsAuthor Commented:
All the suggession are based on writing lot amount of code, For eg. If i have 50 Text boxes i need to write key press on each text boxes which i do not want to do.
0
 
COAtheGREATCommented:
Best solution is to create ActiveX control but you dont want to.
0
 
alaplumeCommented:
How about converting all everything to CAPS only immediately before saving to the database. Assuming that your design in reasonable, this should be very easy to implement.
0
 
alaplumeCommented:
How about converting all everything to CAPS only immediately before saving to the database. Assuming that your design in reasonable, this should be very easy to implement.
0
 
alaplumeCommented:
How about converting all everything to CAPS only immediately before saving to the database. Assuming that your design in reasonable, this should be very easy to implement.
0
 
alaplumeCommented:
How about converting all everything to CAPS only immediately before saving to the database. Assuming that your design in reasonable, this should be very easy to implement.
0
 
alaplumeCommented:
How about converting all everything to CAPS only immediately before saving to the database. Assuming that your design in reasonable, this should be very easy to implement.
0
 
alaplumeCommented:
How about converting all everything to CAPS only immediately before saving to the database. Assuming that your design in reasonable, this should be very easy to implement.
0
 
bobbit31Commented:
alternatively you can do something like:

Private Sub Form_KeyPress(KeyAscii As Integer)
   If (KeyAscii >= Asc("a")) And (KeyAscii <= Asc("z")) Then
       KeyAscii = KeyAscii - 32
   End If
End Sub

just make sure that the form's keypreview property is set to true.


0
 
bobbit31Commented:
i do agree w/ alaplume to convert everything before writing to db... b/c in all of the above examples (except for the change event on each textbox) the user will still be able to cut/paste text in
0
 
CodeWizrdCommented:

Yes, without a change event on every listbox, and without creating (or using an existing) control which masks them for you, you will not be able to catch all conditions.

You will either have to bite the bullet and make massive changes to your app (the easiest way to achieve EXACTLY what you are asking for), or do as alaplume suggests and convert when you write the record.

Good luck, either way.
0
 
tanvirsAuthor Commented:
This code will work Fully Now....
===================================



Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Const KEYEVENTF_KEYUP = &H2

Private Const VK_CAPITAL = &H14

Private Sub Form_Activate()

   '' check if caps is pressed
   If GetKeyState(VK_CAPITAL) <> 1 Then
       '' turn caps on
       keybd_event VK_CAPITAL, 0, 0, 0
       keybd_event VK_CAPITAL, 0, KEYEVENTF_KEYUP, 0
   End If
   
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)
Dim iVal As Integer

  '' check if caps is pressed
   If GetKeyState(VK_CAPITAL) <> 1 Then
       '' turn caps on
       iVal = KeyAscii
       KeyAscii = 0
       keybd_event VK_CAPITAL, 0, 0, 0
       keybd_event VK_CAPITAL, 0, KEYEVENTF_KEYUP, 0
       KeyAscii = iVal - 32
   End If


End Sub

0
 
tanvirsAuthor Commented:
set Form property keypreview = True for my example.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.