The message class - RAISING events

Are you pasting code from Word/LibreOffice Writer?

If so, it may have extra hidden characters for end of line? Do you get the same if you paste in to a proper text editor?

Really, all code should be kept as far away as possible from word processors!

One good trick, however, is to use Ctrl+Shift+V to paste (instead of simple Ctrl+V), or select 'Paste as plain text' from the right-click shortcut menu.
I am pasting it from the VBA editor. Which is not "proper" editor or for that matter a "proper" language:eek::unsure:(n), at least according to the widget that opens.
 
@jwcolby54
Can we take a break from the book for a short while please?

I am trying to implement what I have read into the attached DB, which is from another member here.

I believe I fixed their issue with just a common routine, but can see the potential for a class for the certain command buttons.

I do not want you to write the code, but 'nudge' me in the correct direction.

The buttons are called AlphaTabxx and I have tried to create a class for them.
However when trying to compile, I am getting error below.
View attachment 120004
on Dim WithEvents mlClsAlphaCmd As ClsAlphaCmd

I am trying to get all the buttons down the lefthand of the form to be a class with just the common code for the click in the class, and then be able to remove all their events from the form.

In the class as well, I also wish to set the caption from the class and so believe I need to pass in the command button and an integer to indicate which caption from the Const cstralphabet.

So would be able to tell me what I am missing please.

Thank you.
"does not source automation events" means it does not raise its own events.... Hard to nudge without being too explicit.

A command button raises a ton of events, click etc. Your class can raise its own events...
 
"does not source automation events" means it does not raise its own events.... Hard to nudge without being too explicit.

A command button raises a ton of events, click etc. Your class can raise its own events...
Could you please take a peek and tell me where I am going wrong?
I am struggling on how to just make the buttons from the class. :(
 
Back on the book.
frmDemoSinkingCommandButtonEvent does not exist in your db.
 
This err
"does not source automation events" means it does not raise its own events.... Hard to nudge without being too explicit.

A command button raises a ton of events, click etc. Your class can raise its own events...
Dim WithEvents mlClsAlphaCmd As ClsAlphaCmd is TELLING the compiler that my class RAISES events!

the compiler is telling you that your ClsAlphaCmd doesn't RAISE any events.

by declaring your mclsAlphaCmd "Withevents", you are telling the compiler "my class can raise an event".

Look for something like

Public Event evMyEventName(MyParam)

If you have that in ClsAlphaCmd THEN you can dim it WithEvents.

In the demo database, look in clsMsgPD. This class does that Public Event thing twice. That tells the compiler that "I can raise two different events.

Then down a little ways it actually RAISES those two events.

This a very simple event demonstrating those two parts of being able to raise an event.

I suspect that you do not even want to raise an event, but if you DO, then you have to have a

Public Event evMyEventName(MyParam)
 
What do you want your class to do? What does you code currently look like? Remember in the book I had

Header
variable stuff
Initialization
events
etc
 
@Gasman

You're over-complicating.

Remove the declaration Dim WithEvents mlClsAlphaCmd As ClsAlphaCmd

Then adjust SetAlphaButtons()
Code:
Private Sub SetAlphaButtons()
' Use clsCommand for the buttons for alphabet
Dim i As Integer
Dim acmd As ClsAlphaCmd
For i = 1 To 27
    Debug.Print Me.Controls("AlphaTab" & i).Caption & " - " & Me.Controls("AlphaTab" & i).Name
'    Set Me.Controls("AlphaTab" & i) = mlClsAlphaCmd()
    Set acmd = New ClsAlphaCmd
    Call acmd.fInit(Me.Controls("AlphaTab" & i), i)
Next
End Sub
 
I am not seeing the entire class. Did I miss something?
 
Belay that! The variable goes out of scope at the end of the proc!

Some more slight adjustments:
Code:
Option Compare Database
Option Explicit

' ...
Private Const cstrAlphabet As String = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ"
'Dim WithEvents mlClsAlphaCmd As ClsAlphaCmd
Private m_colAlphaCmds As Collection

' ...

Private Sub Form_Load()
Set m_colAlphaCmds = New Collection
Call SetAlphaButtons ' Initialise the command buttons via a class
End Sub

Private Sub SetAlphaButtons()
' Use clsCommand for the buttons for alphabet
Dim i As Integer
Dim acmd As ClsAlphaCmd
For i = 1 To 27
    Debug.Print Me.Controls("AlphaTab" & i).Caption & " - " & Me.Controls("AlphaTab" & i).Name
'    Set Me.Controls("AlphaTab" & i) = mlClsAlphaCmd()
    Set acmd = New ClsAlphaCmd
    Call acmd.fInit(Me.Controls("AlphaTab" & i), i)
    m_colAlphaCmds.Add acmd, "alpha_" & i
Next
End Sub

Private Sub Form_Unload(Cancel As Integer)
Dim i As Integer
For i = 1 To 27
    m_colAlphaCmds.Remove "alpha_" & i
Next
End Sub

' ...

(BTW, adding Option Explicit gives compile errors in the version you uploaded!)
 
I am not seeing the entire class. Did I miss something?
I do not know.
Using what you have given me and walking the code, the button is initialsed and values are correct, but when it goes to create the next button it terminates the previous one?
I presume we need a collection to store them?
 
Almost always when I build a custom collection that wraps control events and I am working with more than one control I build a matching custom collection class
This is the way I would do it too.
 
Belay that! The variable goes out of scope at the end of the proc!

Some more slight adjustments:
Code:
Option Compare Database
Option Explicit

' ...
Private Const cstrAlphabet As String = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ"
'Dim WithEvents mlClsAlphaCmd As ClsAlphaCmd
Private m_colAlphaCmds As Collection

' ...

Private Sub Form_Load()
Set m_colAlphaCmds = New Collection
Call SetAlphaButtons ' Initialise the command buttons via a class
End Sub

Private Sub SetAlphaButtons()
' Use clsCommand for the buttons for alphabet
Dim i As Integer
Dim acmd As ClsAlphaCmd
For i = 1 To 27
    Debug.Print Me.Controls("AlphaTab" & i).Caption & " - " & Me.Controls("AlphaTab" & i).Name
'    Set Me.Controls("AlphaTab" & i) = mlClsAlphaCmd()
    Set acmd = New ClsAlphaCmd
    Call acmd.fInit(Me.Controls("AlphaTab" & i), i)
    m_colAlphaCmds.Add acmd, "alpha_" & i
Next
End Sub

Private Sub Form_Unload(Cancel As Integer)
Dim i As Integer
For i = 1 To 27
    m_colAlphaCmds.Remove "alpha_" & i
Next
End Sub

' ...

(BTW, adding Option Explicit gives compile errors in the version you uploaded!)
Not my DB. :)
I was helping another user and could see that all these buttons were ripe candidates for a class. :)

So I thought I would try, and failing miserably. :(

I have added it now.
 
Check MajP's suggestion.

I was just building on top of what you had done, but better to dial back a bit further.
 
I took my daughter fishing this morning. I don't fish nor care to fish. Allie though wants to go fishin with dad. So dad... decides to take my lapboard, laptop and mouse down to the lake and try and do stuff while the fish are stealing our worms.

DROPPED MY MOUSE IN THE LAKE!

That would be my God sending me a message!!!:cry: So I was not in the best of moods let us say.
 
If you read the thread, he specifically asked that I NOT "just write it for him" as he is trying to learn how to do it himself. 5 pages of code that has to be waded through with no explanation is not "how do I do this".
Well that is for sure not HOW I DO IT either.
That is hysterical, where do you see five pages of code without explanation?
The referenced Custom Collection Class is explained in great detail in the link provided. And as further explained this is a cut and paste for all Collection Classes. I walk step by step on how to implement a collection class and raise a common event. Then for good measure I summarize step by step in thread 41. I mean, how much more explanation do you need. As for the length of what I posted there is maybe a half page of the collection class, and then I reduced the original class by about half. In the Form's module I added maybe 5 lines of code.

At thread 21 this thread stop being about whatever it was about originally to @Gasman asking to solve a specific problem pretty unrelated to a common listener class. If Gasman was using some technique you were suggesting, I was missing that because I saw no reference to a listener class. To tell you the truth, I could not follow the original thread anyways. However, if @Gasman wants to start a new thread that is up to him. @Gasman, I apologize if I wrote it for you. My intent was to provide a real demo so you could follow the technique of the Collection Class and having the collection class work as a common listener.

New thread here
 
move those posts about my issue to a thread of my own. Waiting to see if it is possible.
You can just paste those into the thread I started and delete these.
 

Users who are viewing this thread

Back
Top Bottom