Code to Read Caption on Command Button

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm trying to write code to create a Function that will, depending on the
Command Button pressed on a form, take the name label (caption) of the button
and assign the text to a string that I can then do something with.

I know how to do this on an individual button basis as a private sub, but I
have a need to do this with several buttons on a form and I don't want to
have to copy the code and edit it for each button.

Any assistance would be appreciated.
 
JHK,

I think you could use this in your function:
Screen.ActiveControl.Caption
 
Its just like a control.... Me!cmdButtonName.Caption

or the full reference [Forms]![frmReservations]![cmdButtonName].caption
 
Sorry about that...if you want to reuse the code, write the sub and then
call the specific sub from the onClick event of each button. This is
the only way to have the same code execute for multiple buttons, but it
reduces the amount of code.

sub mySubWhatever
(code)
end sub

sub cmdPrint_onClick()
call mySubWhatever
end sub
 
I'm just not quite with it, make is a function and pass the value of the
button to it

function myFunction(strCaption as String)
end function

sub cmdPrint_onClick()
t=myFunction(Me!cmdPrint.caption)
end sub
 
Got it. It works fine. Thanks.

David C. Holley said:
Sorry about that...if you want to reuse the code, write the sub and then
call the specific sub from the onClick event of each button. This is
the only way to have the same code execute for multiple buttons, but it
reduces the amount of code.

sub mySubWhatever
(code)
end sub

sub cmdPrint_onClick()
call mySubWhatever
end sub
 
Back
Top