Determining which control triggered forms before update enevt

mcdhappy80

Registered User.
Local time
Today, 20:03
Joined
Jun 22, 2009
Messages
347
Determining which control triggered forms before update event

I have this code in the forms before update event:
Code:
If Not (Me.NewRecord) Then
    If MsgBox("Do You wanna save changes on this record?", vbQuestion + vbYesNo + vbDefaultButton1, "Saving changes?") = vbNo Then
           Me.Undo
    End If
Else
    If MsgBox("Do You wanna save this new record?", vbQuestion + vbYesNo + vbDefaultButton1, "Record saving?") = vbNo Then
        Me.Undo
    End If
End If
I have this command in several controls on form:
Code:
DoCmd.RunCommand acCmdSaveRecord
Can I, and how find out which of these controls triggered before update event in the before update event, and then act accordingly?

Thank You
 
Last edited:
Technically the easiest bet would be to add a module level variable to hold the control in question. You'd expand your called code to set that variable to the control which called it.
e.g.
Code:
Sub SaveMe(strControl As String)
    mstrControl = strControl
    If Me.Dirty Then
        Me.Dirty = False
    End If
End Sub
You'd call that sub instead of your existing code (where mstrControl is the variable you've created at the module level).

SaveMe "cmdSave"

Equally, you could pass the control object itself (I tend to prefer that - but it's not mandatory)
Code:
Sub SaveMe(ctlPassed As Access.Control)
    Set mctlControl = ctlPassed 
    ...

SaveMe Me.cmdSave

You'll then be able to quiz that form variable to know which was the last control used.
it suffers from being fairly obvious and open to mistakes if you don't code this thoroughly throughout and so forth.

You can "soft code" your event handlers if you wish (as Chris describes the technique) - or even use a class and collection to handle events.

As a more basic hack, you can consider the currently selected control as your form is updating. Very much open to mis-interpretation, but that would be
Me.ActiveControl
Just as the previous control would be vulnerable if that was your requirement
Me.PreviousControl

My main question is - why do you have a commit of data initiated from so many places on your form?
If edits have been made then caching the update isn't generally a bad thing. Writing to the database is the most tentative time for a file server database (which you're likely using). Doing it when absolutely required is the best bet.

Cheers.
 

Users who are viewing this thread

Back
Top Bottom