Code to Read Caption on Command Button

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.
 
S

Steve Schapel

JHK,

I think you could use this in your function:
Screen.ActiveControl.Caption
 
G

Guest

Screen.ActiveControl.Caption does appear to be valid. (I'm using 2002)

Any ideas?
 
D

David C. Holley

Its just like a control.... Me!cmdButtonName.Caption

or the full reference [Forms]![frmReservations]![cmdButtonName].caption
 
D

David C. Holley

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
 
D

David C. Holley

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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top