get the name of the running proceedure in VBA

  • Thread starter Thread starter mw4
  • Start date Start date
M

mw4

if I click on a button, say "Command356_Click()"

how can I get that as a variable to use to log button clicks?
 
Me.ActiveControl.Name should give you the name of the control that was just
clicked, if that's what you're asking. I think this would work with buttons,
anyway. I've only used it with textboxes before, but pretty sure that would
do it. You'd still have to put code in each button click sub, though, as far
as I can tell, e.g.

Call LogClick(Me.ActiveControl.Name)

or something like that within each button's click event. Not sure if
there's a way to capture any button click with just one routine, if that's
what you're looking for. But this would save you from having to hardcode each
individual button name within each click sub (if I understand what you're
trying to do correctly).
 
Jim, Thanks that worked awesome...thanks for pointing me in the function
direction.

**********
Public Function LogClicks(strWhatUsed As String, strPageUsed As String,
strProcessUsed As String)

strWhatUsed = Me.ActiveControl.Name

DoCmd.SetWarnings False
DoCmd.RunSQL ("INSERT INTO tblButtonLog " _
& "([WhenUsed], [WhoUsed],
[WhatUsed],[PageUsed],[ProcessUsed]) " _
& "SELECT Now(), CurrentUser(), '" & strWhatUsed & "' , '" &
strPageUsed & "', '" & strProcessUsed & "'")
DoCmd.SetWarnings True

End Function

**********

Private Sub Command357_Click()
Call LogClicks("xxx", "xxx", "xxx")
End Sub
 
I would think you should be passing the ActiveControl name to the sub, not
setting it inside the sub, e.g.

Call LogClicks(Me.ActiveControl.Name,...

mw4 said:
Jim, Thanks that worked awesome...thanks for pointing me in the function
direction.

**********
Public Function LogClicks(strWhatUsed As String, strPageUsed As String,
strProcessUsed As String)

strWhatUsed = Me.ActiveControl.Name

DoCmd.SetWarnings False
DoCmd.RunSQL ("INSERT INTO tblButtonLog " _
& "([WhenUsed], [WhoUsed],
[WhatUsed],[PageUsed],[ProcessUsed]) " _
& "SELECT Now(), CurrentUser(), '" & strWhatUsed & "' , '" &
strPageUsed & "', '" & strProcessUsed & "'")
DoCmd.SetWarnings True

End Function

**********

Private Sub Command357_Click()
Call LogClicks("xxx", "xxx", "xxx")
End Sub


Jim Burke in Novi said:
Me.ActiveControl.Name should give you the name of the control that was just
clicked, if that's what you're asking. I think this would work with buttons,
anyway. I've only used it with textboxes before, but pretty sure that would
do it. You'd still have to put code in each button click sub, though, as far
as I can tell, e.g.

Call LogClick(Me.ActiveControl.Name)

or something like that within each button's click event. Not sure if
there's a way to capture any button click with just one routine, if that's
what you're looking for. But this would save you from having to hardcode each
individual button name within each click sub (if I understand what you're
trying to do correctly).
 
I thought that too, but it is working as expected right where it is, plus
it's cleaner and less overall code.
My guess is that me.activecontrol refers to the original calling sub and not
the function.

Jim Burke in Novi said:
I would think you should be passing the ActiveControl name to the sub, not
setting it inside the sub, e.g.

Call LogClicks(Me.ActiveControl.Name,...

mw4 said:
Jim, Thanks that worked awesome...thanks for pointing me in the function
direction.

**********
Public Function LogClicks(strWhatUsed As String, strPageUsed As String,
strProcessUsed As String)

strWhatUsed = Me.ActiveControl.Name

DoCmd.SetWarnings False
DoCmd.RunSQL ("INSERT INTO tblButtonLog " _
& "([WhenUsed], [WhoUsed],
[WhatUsed],[PageUsed],[ProcessUsed]) " _
& "SELECT Now(), CurrentUser(), '" & strWhatUsed & "' , '" &
strPageUsed & "', '" & strProcessUsed & "'")
DoCmd.SetWarnings True

End Function

**********

Private Sub Command357_Click()
Call LogClicks("xxx", "xxx", "xxx")
End Sub


Jim Burke in Novi said:
Me.ActiveControl.Name should give you the name of the control that was just
clicked, if that's what you're asking. I think this would work with buttons,
anyway. I've only used it with textboxes before, but pretty sure that would
do it. You'd still have to put code in each button click sub, though, as far
as I can tell, e.g.

Call LogClick(Me.ActiveControl.Name)

or something like that within each button's click event. Not sure if
there's a way to capture any button click with just one routine, if that's
what you're looking for. But this would save you from having to hardcode each
individual button name within each click sub (if I understand what you're
trying to do correctly).

:

if I click on a button, say "Command356_Click()"

how can I get that as a variable to use to log button clicks?
 
Jim Burke in Novi said:
I would think you should be passing the ActiveControl name to the sub, not
setting it inside the sub, e.g.

Call LogClicks(Me.ActiveControl.Name,...

mw4 said:
Jim, Thanks that worked awesome...thanks for pointing me in the function
direction.

**********
Public Function LogClicks(strWhatUsed As String, strPageUsed As String,
strProcessUsed As String)

strWhatUsed = Me.ActiveControl.Name

DoCmd.SetWarnings False
DoCmd.RunSQL ("INSERT INTO tblButtonLog " _
& "([WhenUsed], [WhoUsed],
[WhatUsed],[PageUsed],[ProcessUsed]) " _
& "SELECT Now(), CurrentUser(), '" & strWhatUsed & "' , '" &
strPageUsed & "', '" & strProcessUsed & "'")
DoCmd.SetWarnings True

End Function

**********

Private Sub Command357_Click()
Call LogClicks("xxx", "xxx", "xxx")
End Sub


Jim Burke in Novi said:
Me.ActiveControl.Name should give you the name of the control that was just
clicked, if that's what you're asking. I think this would work with buttons,
anyway. I've only used it with textboxes before, but pretty sure that would
do it. You'd still have to put code in each button click sub, though, as far
as I can tell, e.g.

Call LogClick(Me.ActiveControl.Name)

or something like that within each button's click event. Not sure if
there's a way to capture any button click with just one routine, if that's
what you're looking for. But this would save you from having to hardcode each
individual button name within each click sub (if I understand what you're
trying to do correctly).

:

if I click on a button, say "Command356_Click()"

how can I get that as a variable to use to log button clicks?
 
The reason I mentioned that is that you have 'strWhatUsed' as a function
argument. If you're just setting the value of that field in the function
unconditionally then it doesn't really make sense to pass it to the function
as an argument. Instead of using strWhatUsed in the RunSQL statement, just
put Me.ActiveControl.Name there and don't bother with the function argument.
It works either way obviously, just a slightly 'cleaner' way of doing it.

mw4 said:
I thought that too, but it is working as expected right where it is, plus
it's cleaner and less overall code.
My guess is that me.activecontrol refers to the original calling sub and not
the function.

Jim Burke in Novi said:
I would think you should be passing the ActiveControl name to the sub, not
setting it inside the sub, e.g.

Call LogClicks(Me.ActiveControl.Name,...

mw4 said:
Jim, Thanks that worked awesome...thanks for pointing me in the function
direction.

**********
Public Function LogClicks(strWhatUsed As String, strPageUsed As String,
strProcessUsed As String)

strWhatUsed = Me.ActiveControl.Name

DoCmd.SetWarnings False
DoCmd.RunSQL ("INSERT INTO tblButtonLog " _
& "([WhenUsed], [WhoUsed],
[WhatUsed],[PageUsed],[ProcessUsed]) " _
& "SELECT Now(), CurrentUser(), '" & strWhatUsed & "' , '" &
strPageUsed & "', '" & strProcessUsed & "'")
DoCmd.SetWarnings True

End Function

**********

Private Sub Command357_Click()
Call LogClicks("xxx", "xxx", "xxx")
End Sub


:

Me.ActiveControl.Name should give you the name of the control that was just
clicked, if that's what you're asking. I think this would work with buttons,
anyway. I've only used it with textboxes before, but pretty sure that would
do it. You'd still have to put code in each button click sub, though, as far
as I can tell, e.g.

Call LogClick(Me.ActiveControl.Name)

or something like that within each button's click event. Not sure if
there's a way to capture any button click with just one routine, if that's
what you're looking for. But this would save you from having to hardcode each
individual button name within each click sub (if I understand what you're
trying to do correctly).

:

if I click on a button, say "Command356_Click()"

how can I get that as a variable to use to log button clicks?
 
Back
Top