Unkown Cls/Collection Holding An Event (3 Viewers)

Thanks guys, learning a lot. I'm new to classes & this is probably a bit much for me at this early stage but I think if I can get a good understanding of them that's 90% of VBA (OOP) & less likely to come across issues later. It's interesting no matter how much you read you come across issues (MSN seriously lacking).

A problem here @MarkK pointed out that I was using a Form_FrmName reference; which can cause problems. I thought it would be nice to reference the form directly as it's object; allowing auto-update on it's name should i change it. But was unaware that if the form was not found it would then create a reference of it. He also kindly pointed out that an external reference outside the class to a procedure was the main issue. Obviously only Mark got handed the db so no one else could have been aware of this.

My original goal here was to assign a right-click event raiser globally to controls in the Detail section of a form & have a single event/ function/ whatever handle them. A way of assigning an ObjVar_ObjVarEvent of the user's type. The benefit being I may add/ delete controls... later to this frm & this coding pattern would save me in the future (more importantly I'm trying to learn). Was tyring to use classes to learn & get the most of out the exercise as I have little experience with classes.

Disregarding classes for a moment, I was thinking of using a function to handle multiple events from @MajP 's fine https://www.access-programmers.co.u...-you-need-to-know-about-access-events.308963/ in the form directly. But I cannot see how one could pass an argument (Button) of the MouseUp event if one was to assign a custom function? Seems that the function name must be a literal; "=fnName()" with a set of parentheses. Cannot handle concatenation, even from VBA. Be great if it is possible to use parameters with the function when assigning the function to handle multiple events.
I just discovered that there is a:
Code:
Private Sub Detail_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
but suprisingly does not seem to work.

The perfect solution would be if VBA supported below in pseudo code:

SubFrm:
mctrl as Control
Code:
Private Sub Form_Load:
  Dim mctrlAs Control
  For Each ctrl In Me.Detail.Controls
    If mctrl.ControlType = acTextBox Then
      mctrl.OnMouseUp = "[Event Procedure]"
    End If
  Next ctrl
End Sub

Private Sub mctrl_MouseUp (Button As Integer, Shift As Integer, X As Single, Y As Single)
  If Button = vbKeyRButton Then
    MsgBox "Honour the coders"
End Sub

It's quite difficult to see when to use a class over the data in tables... My main take at this point is they are of benefit in forms in grouping things... Done a few tutorials relating to data... but I think it's hard for a user to comprehend when a class prevails/ complements over the schema design... double-work as you've now got data in classes & data in tables which require updating...

It seems the options are:
  1. You separate the object itself as a class (object) & the object handler as another class (strategy). You merge them together & refer to the clsStrategy.FunctionName.
  2. You use a Collection Class.
 
Last edited:
I previously said you can also CRUD tag properties in vba code. Me thinks it's actually faster to use them than having to write code to create and maintain classes. But I'm not knocking that. If you prefer creating classes, so be it. I have a checklist I use when creating new apps.
I am sorry but this is 10 times funnier, because it is two completely unrelated topics. This is such a complete "apples and oranges" comparison that it makes no sense at all. Have you not been following the conversation? Explain again how the tag property will replace the capability of a class to
1. Trap events of multiple controls
2. Extend the properties of those controls
3. Encapsulate multiple functionality to avoid writing new code
4. Raise custom events
5. Allow to define the functionality when an event occurs

Using the tag property to group controls is a pretty well known and well used, but not really related to the conversation. Some times you need to read the room. Just saying.
 
I am sorry but this is 10 times funnier, because it is two completely unrelated topics. This is such a complete "apples and oranges" comparison that it makes no sense at all. Have you not been following the conversation? Explain again how the tag property will replace the capability of a class to
1. Trap events of multiple controls
2. Extend the properties of those controls
3. Encapsulate multiple functionality to avoid writing new code
4. Raise custom events
5. Allow to define the functionality when an event occurs

Using the tag property to group controls is a pretty well known and well used, but not really related to the conversation. Some times you need to read the room. Just saying.
I mentioned tag properties because it'similar to what the OP wants to accomplish with his global right click controls.
 
I mentioned tag properties because it'similar to what the OP wants to accomplish with his global right click controls.
You will have to pull the thread on that one for me because I am not seeing it. The OP wants to trap the right click on a mouse down/up for multiple controls. This one is complicated because the event procedure requires parameters to be passed from the application
Code:
Private Sub lbl1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

End Sub

If this was just a click event where you do not have parameters (or do not care) then the solution is easy and requires neither tag properties or some class. Simply on the onClick property for each control put the name of a function. The function traps the event for all of the controls
See discussion on events
and handling multiple events with one function

But in this case where I need to have the application provide values for those parameters, I know of no other way then to build a class as demonstrated. If someone knows another way to trap multiple mouse up/down and determine a right click without a class I would be interested because I cannot think how to do it. And I highly doubt a simple tag is going to get me there.
 
I know of no other way then to build a class as demonstrated. If someone knows another way to trap multiple mouse up/down and determine a right click without a class I would be interested because I cannot think how to do it. And I highly doubt a simple tag is going to get me there.
Does code similar to what I used in the On Click and KeyDown events for selecting records on the datasheet subform issue I had in my other thread applicable to what OP wants to accomplish?

Code:
Private Function CheckForEnterKey(KeyCode As Integer)
   If KeyCode = vbKeyReturn Then
      DisplayContract
   End If
End Function


Private Sub Form_Current()

    DoCmd.RunCommand acCmdSelectRecord
    Me!RowHighlight = Me!RefContractNo
 
End Sub

Private Sub Form_Load()

'      Call CreateRightClickMenu
Application.CommandBars("Form Datasheet Subcolumn").Enabled = True
DoCmd.RunCommand acCmdSelectRecord

End Sub


Private Sub ContractNo_KeyDown(KeyCode As Integer, Shift As Integer)
   If KeyCode = vbKeyReturn Then
      DisplayContract
   End If

End Sub

Private Sub ContractStatus_KeyDown(KeyCode As Integer, Shift As Integer)
   CheckForEnterKey KeyCode

End Sub

Private Sub DaysInterestDue_KeyDown(KeyCode As Integer, Shift As Integer)
   CheckForEnterKey KeyCode

End Sub

Private Sub DisplayComment_Click()
    'SendKeys "{F2}"
   ' DoCmd.RunCommand acCmdZoomBox
    DoCmd.OpenForm "frmPopUpDisplayContractComments", , , "Contractno = " & Me.ContractNo & ""
 
End Sub


Private Sub txtContractNo_KeyDown(KeyCode As Integer, Shift As Integer)
   CheckForEnterKey KeyCode

End Sub

Private Sub txtReedemedAmount_KeyDown(KeyCode As Integer, Shift As Integer)
   CheckForEnterKey KeyCode

End Sub

ForfeitContract4.png
 
I had in my other thread applicable to what OP wants to accomplish?
Ahh... No.

fml-sylvester.gif


All you are showing is a bunch of hard wired events procedures. So what? Not even related. The point was to handle of multiple events without writing multiple Event Procedure. More apples and oranges. Now I am confused why Dalski would give you a thumbs up, because I cannot see any value to the conversation from that code. Maybe he sees something I do not or is just a nicer person.
 
Hey guys don't spend anymore time on this; you've already been too generous. I'm playing around more, getting a better understanding & probably come up with something not puristly correct, but may work. I'll update the thread on how I get on.

Things are finally starting to get a bit better after nearly 2 weeks of studying it full time.
I've been trying to run before I can walk. I'm playing around, using a cls for the MenuBar... storing more properties in the right-click event into a collection & I think I might be able to refer to these later. Getting the value back from there/ passing through other classes...
I'm understanding basic classes a bit better now. It hasn't been an ideal first class experience as I've been overwhelmed by doing a CmdBar for the first time, with Advanced Decoupling on my first class. I've been running before I can walk. Thanks for all your patience & input.

Maybe he sees something I do not or is just a nicer person.

I wanted to be encouraging & grateful but I must admit I removed it prior to your post. Not to be ungrateful @BlueSpruce :ROFLMAO: but I'd have to agree with @MajP here :ROFLMAO:.
 
Ahh... No.

View attachment 121092

All you are showing is a bunch of hard wired events procedures. So what? Not even related. The point was to handle of multiple events without writing multiple Event Procedure. More apples and oranges. Now I am confused why Dalski would give you a thumbs up, because I cannot see any value to the conversation from that code. Maybe he sees something I do not or is just a nicer person.
okay, well, i'm going to bail out of this thread because i'ts over my head and don't want to continue sticking my foot in my mouth. i'm not as experienced with Access as several here.
 
Hey guys don't spend anymore time on this; you've already been too generous. I'm playing around more, getting a better understanding & probably come up with something not puristly correct, but may work.
I do not think Mark and I were necessarily focused on solving your specific problem, but more on demonstrating coding concepts and techniques that you and others can use anywhere.
Mark identifies some general design concepts that you should strive for
With this approach, the functionality you call at handleRightClickOnBillAndPageF() is encapsulated within the class. This promotes tidy code, because it reduces the pollution of your global namespace with a proliferation of standard module based Public Sub HandleRightClickOnBillAndPageF-like methods.

For even more flexibility, create a cTextBoxRightClickHandler class, and pass in a cBillAndPageFStrategy class ( which exposes an Execute() method ). This way you can decouple generic right-click textbox handling from what actually happens when the right click occurs, which is provided by the Strategy class at runtime
I may have approached this differently, but still trying to incorporate these concepts of encapsulation, clean and efficient code, flexibility, and decoupling / loosely coupled design.
 
Idemonstrating coding concepts and techniques that you and others can use anywhere.
This is more valuable than anything. Understanding best practices & the reasons behind them.
Mark identifies some general design concepts that you should strive for
Yes I'm most grateful. You've been a massive help yourself also. I received several solutions to my problem here but more importantly you've both helped me to grasp the concept of OOP, which I simply couldn't have done on my own. Achieved on inadequate info to boot.
Thanks again.
 

Users who are viewing this thread

Back
Top Bottom