Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capture Name of Sub From Within the Sub 1

Status
Not open for further replies.

Du2good

Technical User
May 9, 2005
41
US
I want to track some(not all) of the buttons the users are using.

For instance if a user clicks Private Sub cmdAffidavit_Click(), I'd like to be able to call a function that will return the name of the event.

Currently I've added a line of code to each event, that is the name of the event. When the button is clicked the name of the event is saved to a table.

Is it possible to do it with one line, so I dont have to add a line like SubName = "cmdAffidavit_Click" for each sub that I want to track the useage of? Or is there a better method of tracking which subs are being called?

Thanks everyone!!
 
You mean one line, period, instead of one line per sub-procedure call?

Or would putting the code within the sub-procedures themselves work? I'd imagine that would work. The latter wouldn't work, I believe, as there is no way to track "current sub-procedure". Though, I could be wrong, I've just never heard/read of such.

That may reduce the total code required if some of the sub-procedures are called multiple times, at least.

--

"If to err is human, then I must be some kind of human!" -Me
 
How are ya Du2good . . .

The following code will pack a string with the full form/subform path to the button clicked ... the format of which looks like:
[blue]MainForm\sub1\sub2\ ... subN\ButtonName_Click[/blue]

So in a module in the modules window, copy/paste the following:
Code:
[blue]Public Function IsSubForm(frmName As String) As Boolean
   IsSubForm = (SysCmd(acSysCmdGetObjectState, acForm, frmName) = 0)
End Function

Public Sub LogButton(frm As Form)
   Dim db As DAO.Database, SQL As String, ctl As Control
   Dim frmPath As String, frmName As String
   
   Set db = CurrentDb
   Set ctl = Screen.ActiveControl
   frmName = frm.Name
   
   If ctl.ControlType = acCommandButton Then
      Do Until IsSubForm(frmName) = False
         frmPath = frm.Name & "\" & frmPath
         
         If IsSubForm(frm.Parent.Name) Then
            Set frm = frm.Parent.Form
            frmName = frm.Name
         Else
            Set frm = frm.Parent
            frmName = frm.Name
         End If
      Loop
      
      frmPath = frmName & "\" & frmPath & ctl.Name & "_Click"
      Debug.Print frmPath
      [green]'SQL = your insert SQL
      'db.RunSQL SQL, dbFailOnError[/green]
   End If
   
   Set ctl = Nothing
   Set db = Nothing
      
End Sub[/blue]
Then in the click event of the buttons, use the following one liner:
Code:
[blue]   Call LogButton(Me)[/blue]
Thats it other than supplying the SQL of your insert query in the spot shown in green and removing the rems.

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Thanks AceMan1.. It does exactly what I wanted... and thanks for adding the subform code too!! It is working great!!
 
Du2good . . .

BTW ...
Code:
[blue]   db.RunSQL SQL, dbFailOnError
Should Be:
   db.[purple][b]Execute[/b][/purple] SQL, dbFailOnError[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top