Solved A class module does not work

zelarra821

Registered User.
Local time
Today, 19:27
Joined
Jan 14, 2019
Messages
835
Hello. I am trying to create a class module for when I press a key. I manage to start it, but not pass the event procedure to the form, and I don't know what I'm doing wrong. Could someone tell me what is failing? Thanks

Code:
Option Compare Database
Option Explicit

Private WithEvents Form As Access.Form
Private mPresionarTecla As Boolean

Public Property Get PresionarTecla() As Boolean
    PresionarTecla = mPresionarTecla
End Property

Public Property Let PresionarTecla(ByVal vNewValue As Boolean)
    mPresionarTecla = vNewValue
End Property

Public Sub InitalizeAutokeys(FName As Form)
    Set Form = FName
    
    If mPresionarTecla = True Then
        FName.OnKeyDown = "[Event Procedure]"
    End If
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        mPresionarTecla = True
        Select Case Shift
            Case 2
                Select Case KeyCode
                    Case vbKeyV
                        rbPegarSinFormato Screen.ActiveControl
                End Select
        End Select
End Sub
 
I manage to start it, but not pass the event procedure to the form, and I don't know what I'm doing wrong. Could someone tell me what is failing?
It is primarily *your* job to tell us what is failing. So, please provide a meaningful description of the problem.
I do not understand what you mean by "I manage to start it, but not pass the event procedure to the form[...]"
Maybe it would clarify things, if you would show the initialization code for the class.

One thing you should check: Is mPresionarTecla actually true, when the InitalizeAutokeys procedure is executed?
 
Code:
Public Sub InitalizeAutokeys(FName As Form, Ptecla as boolean)
    Set Form = FName
    me.PresionarTecla = Ptecla
    If mPresionarTecla = True Then
        FName.OnKeyDown = "[Event Procedure]"
    End If
End Sub

But that still makes no sense to me. Why would you ever not trap the event on initialize?
Code:
Public Sub InitalizeAutokeys(FName As Form)
    Set Form = FName
    me.PresionarTecla = True
     FName.OnKeyDown = "[Event Procedure]"
End Sub

I would not turn on and turn off the event handler.
If you want to turn it off . I think the code you want is
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
       if mPresionarTecla = True then
        Select Case Shift
            Case 2
                Select Case KeyCode
                    Case vbKeyV
                        rbPegarSinFormato Screen.ActiveControl
                End Select
        End Select
       end if   
End Sub
 
Last edited:
Hello good. It's the first time I've made a class module, so I've seen a tutorial on YouTube, but it doesn't clarify my specific case, and I've also followed @MajP's example with his FindAsYouTypeCombo class module.

In the form, I declare a variable at the beginning of the module:

Code:
Dim RichText As New Autokeys

And then in the Form_Load event, I call the procedure that starts the class module:

Code:
RichText.InitalizeAutokeys Me, True

I don't know if this is enough for it to work because, as I said, it's the first time I've done this and I have no idea. I want to learn how to do it.

What I'm looking for is to execute a procedure (rbPegarSinFormato) as long as the Control + V keys are pressed. And the only thing missing to be perfect is that it only runs in the text boxes that have richtext activated.

Thank you very much and sorry that my initial post is not accurate.
 

Attachments

I would assume when you initialize the class you want the keypress functionality so I made the parameter optional of PresonairTecla. The main issue is that you have to set the form's Keypreview property to true or this will never work in a class or otherwise.
I did this in the initialize.
To check if a control is Rich text must first check to see if it is a textbox. Then check to see if the TextFormat = 1. I added a user defined constant for this.


Code:
Private WithEvents Form As Access.Form
Private mPresionarTecla As Boolean
Private Const acRichtext = 1
Public Sub InitalizeAutokeys(FName As Form, Optional Ptecla As Boolean = True)
    Set Form = FName
    Me.PresionarTecla = Ptecla
    Form.OnKeyDown = "[Event Procedure]"
    Form.KeyPreview = True
End Sub


Public Property Get PresionarTecla() As Boolean
    PresionarTecla = mPresionarTecla
End Property


Public Property Let PresionarTecla(ByVal vNewValue As Boolean)
    mPresionarTecla = vNewValue
End Property


Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
   If Me.PresionarTecla Then
   If Shift = 2 And KeyCode = vbKeyV Then
      If Form.ActiveControl.controlType = acTextBox Then
        Dim txt As TextBox
        Set txt = Form.ActiveControl
        If txt.TextFormat = acRichtext Then
          rbPegarSinFormato txt
        End If
      End If
    End If
   End If
End Sub
 
FYI. Not all controls have the same properties. All controls do have a tag property. So I cannot do the following
Code:
dim ctrl as access.control
set ctrl = me.activecontrol
'check if the control is a text box and richtext
if ctrl.controltype = acTextbox and ctrl.Textformat = 1 then
The above will error if the active control is not a textbox because most controls do not have a TextFormat property. That is why you have to nest the if check and check first that it is a textbox.
 
Last edited:
I understand everything you say and where was the failure.

I've had to do two adjustments to get it to work.

First, make the rbPegarSinFormato argument optional, because it gave a type error. I have been analyzing the procedure, and I don't know to what extent it is necessary to pass an argument to it that is not going to be used in the procedure itself for the ribbon (Control As IRibbonControl). This happens to me with more than one procedure that I have created for other ribbons. Is it necessary to put "Optional Control As IRibbonControl"?

The other detail that must be corrected is that it was pasting twice, one without format and the other with format. Since I'm using the same keyboard shortcut as for paste, I have to override the parent, which I did with KeyCode = 0 before using the rbPasteNoFormat procedure.

I have a second one to solve. And it is: on this line:

If txt.TextFormat = acRichtext Then

Couldn't you replace acRichText directly with the value 1, instead of creating the constant at the beginning?

Thanks a lot.
 
If txt.TextFormat = acRichtext Then
Couldn't you replace acRichText directly with the value 1, instead of creating the constant at the beginning?
Yes I only did that for readability. If another person wants to use the code, they cannot easily read that and know 1 is for richtext. I had to go figure out what the values were. This can be frustrating since access is not consistent.
Example for the textformat property the values are
0 = plain text
1 = RichText

So you would think for a combobox rowsourcetype it would be numerics like
0 = table/query
1 = Value List
2 = Field List

But it is not it is a string property
"Table/Query"
"Value List"
"Field List"

Not sure I know what the IribbonControl has to do with it. I would think it is

Code:
'call it and pass the text box as I did
If Form.ActiveControl.controlType = acTextBox Then
        Dim txt As TextBox
        Set txt = Form.ActiveControl
        If txt.TextFormat = acRichtext Then
          rbPegarSinFormato txt
        End If
      End If

then
Code:
Sub rbPegarSinFormato(Control As Access.Textbox)

If (txt.text & "") = "" Then
  txt = Application.PlainText(Clipboard)
Else
  txt = Application.HtmlEncode(Application.PlainText(txt.text & "<br>" & Clipboard))
End If
txt.SelStart = Len(txt.Text)
End Sub
 

Users who are viewing this thread

Back
Top Bottom