Double-Click Event of Datasheet Line and WithEvents (1 Viewer)

GK in the UK

Registered User.
Local time
Today, 09:39
Joined
Dec 20, 2017
Messages
274
On my invoice/order form I have a sub form with a function ViewTransaction to open a pop-up form for that datasheet line.

On the double-click event of every field in the datasheet I have: =ViewTransaction
So double-clicking anywhere on the line opens the pop-up.

On the host form I also have a button View Line Detail which opens the same pop-up, so I have duplicated code which I want to consolidate to the main form.

I was about to set up a WithEvents on the double-click event and deal with it all in once place, but it occurred to me I have 8 fields on the sub form datasheet.

Must I set up a WithEvents for each field on the datasheet or is there an easier way ?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:39
Joined
Oct 29, 2018
Messages
21,358
Hi. I'm no expert on this subject; but if you're using a Datasheet (not a Continuous Form), then I don't know any way around having to "wire" each Control with a DblClick event. If you can have the user double-click on the record selector, rather than on any field, then you can get away with just setting it up in one place. Otherwise, when using a Continuous Form, you can overlay a transparent button on top of the record (unless, of course, you still need to edit each field). Just my 2 cents...
 

Micron

AWF VIP
Local time
Today, 05:39
Joined
Oct 20, 2018
Messages
3,476
You can simplify calling the same event for many controls by using a Public Function in a standard module. If there are no parameters for the function it's fairly straightforward - you select all controls in design view and make the event call that function. If there are parameters, then something about each control might need to be passed to the function which then means editing each control's function call. At least with that you still don't have to write what is basically the same sub for every control. If you can make use of the Tag property for this it might simplify the parameter issue. I suppose you could also use the Screen.ActiveControl property to get its value or name if either were useful, but I try to not depend on that property.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:39
Joined
May 21, 2018
Messages
8,463
Must I set up a WithEvents for each field on the datasheet or is there an easier way ?
Not sure if easier, but it is easy, and good practice in custom events. So a single function handles all events and raises a single event. Interesting, not sure if gaining anything.
Mainform
Code:
Private WithEvents subFrm As Form_subFrmDetails
Private Sub Form_Load()
  Set subFrm = Me.Order_Details_subform.Form
End Sub

Private Sub subFrm_Info(TheInfo As String)
  MsgBox TheInfo
End Sub

Subform
Code:
Public Event Info(TheInfo As String)
Public Function GiveInfo() As String
  RaiseEvent Info(ActiveControl.Name & " " & ActiveControl.Value)
End Function
 

GK in the UK

Registered User.
Local time
Today, 09:39
Joined
Dec 20, 2017
Messages
274
Thanks all, I think on balance I will leave the duplicated code in rather than tinker with it. I just put a note in the code to say, don't forget to update the sister code in the other module (you guessed I kept forgetting)

MajP's code is bookmarked for future reference.
 

Users who are viewing this thread

Top Bottom