Assign a code to Key Press of all Textboxes

amir0914

Registered User.
Local time
Today, 08:17
Joined
May 21, 2018
Messages
151
Hello everyone,
I've added a small code to OnKeyPress event of a Textbox and it works correct, but I need to assign it on all forms and all Textboxes of my program :

Code:
Private Sub txtCustomer_KeyPress(KeyAscii As Integer)
If KeyAscii = 1740 Then
KeyAscii = 1610
End If
End Sub

I can put it on each TextBox manually, but it takes many time to accomplish.
Do you think there is a better way to do it?
 
you create a Class and on the Load event of your form.
you can have a code to Open each form in design view and insert
some code to the Load event of the form to instantiate the class.
 
Without looking up the values, I haven't a clue what the code is doing. Do yourself and your successors a favor and document the values in the code.
 
you create a Class and on the Load event of your form.
you can have a code to Open each form in design view and insert
some code to the Load event of the form to instantiate the class.
Could you please attach an example from Class ?
 
here is a demo, you copy both Module1 and tboxClass class
to your db. make sure to name your class same as "tboxClass".
on VBA, immediate window run:

InsertToAllForms

this will insert code to All your form.
you only run this once, otherwise the code
will be duplicate and you'll get error when
you open your form.

if you created new form and want to use
the same functionality to your textboxes,
you run the sub with the name of the form:

InsertToAllForms "formName"
 

Attachments

arnel,
Even when you pass in a name of a form in, it still processess all forms?
 
thanks gasman (the price of gas is high!).
forgot to modify:
Code:
Public Sub InsertToAllForms(Optional ByVal frmName As String)
Dim o As AccessObject
For Each o In CurrentProject.AllForms
    If Len(frmName) <> 0 Then
        If UCase(o.Name) = UCase(frmName) Then
            Call tboxCode(o.Name)
            Exit For
        End If
    Else
        Call tboxCode(o.Name)
    End If
Next
End Sub
 

thanks gasman (the price of gas is high!).
forgot to modify:
Code:
Public Sub InsertToAllForms(Optional ByVal frmName As String)
Dim o As AccessObject
For Each o In CurrentProject.AllForms
    If Len(frmName) <> 0 Then
        If UCase(o.Name) = UCase(frmName) Then
            Call tboxCode(o.Name)
            Exit For
        End If
    Else
        Call tboxCode(o.Name)
    End If
Next
End Sub
It was useful, Thank you so much for spending time on my project.
 
Expanding on arnel's sample, if you write another class you can reduce the code on any form to...
Code:
Private m_kpcs As New cKeyPressConverters

Private Sub Form_Load()
    m_kpcs.Load Me
End Sub
Here's a sample...
 

Attachments

Expanding on arnel's sample, if you write another class you can reduce the code on any form to...
Code:
Private m_kpcs As New cKeyPressConverters

Private Sub Form_Load()
    m_kpcs.Load Me
End Sub
Here's a sample...
Perfect, is it possible to set function to other objects such as Combobox, Subform's fields ?
 
Yes. In the loop that enumerates the controls on the form and builds the collection there's an If block that tests the type of the control. Code will be something like...
Code:
If TypeOf ctrl is Access.Textbox Then
To also handle ComboBoxes, change that line to...
Code:
If TypeOf ctrl is Access.Textbox Or TypeOf ctrl is Access.Combobox Then
 

Users who are viewing this thread

Back
Top Bottom