using variable as control name?

A

aft3rgl0w

hey all, i have a subform which has 3 buttons. i'm writing a function whose
sole inpt variable will be the name of the button. part of the function
changes the caption on the selected button, and I want to use this variable
in there
i've tried the following:
(flt is the name of the button)

function changebut(flt as string)
forms!frmmain!subfrm! & flt & .caption = "X"
end function

of course, this doesn't work, i get a "Compile error: Expected: =" and it
highlights the first ampersand

what's the proper syntax for using a variable as a control name?
 
R

Rick Brandt

aft3rgl0w said:
hey all, i have a subform which has 3 buttons. i'm writing a
function whose sole inpt variable will be the name of the button.
part of the function changes the caption on the selected button, and
I want to use this variable in there
i've tried the following:
(flt is the name of the button)

function changebut(flt as string)
forms!frmmain!subfrm! & flt & .caption = "X"
end function

of course, this doesn't work, i get a "Compile error: Expected: ="
and it highlights the first ampersand

what's the proper syntax for using a variable as a control name?

forms!frmmain!subfrm(flt).caption = "X"
 
S

Stuart McCall

aft3rgl0w said:
hey all, i have a subform which has 3 buttons. i'm writing a function
whose
sole inpt variable will be the name of the button. part of the function
changes the caption on the selected button, and I want to use this
variable
in there
i've tried the following:
(flt is the name of the button)

function changebut(flt as string)
forms!frmmain!subfrm! & flt & .caption = "X"
end function

of course, this doesn't work, i get a "Compile error: Expected: =" and it
highlights the first ampersand

what's the proper syntax for using a variable as a control name?

Function ChangeBut(ButtonName As String)
Forms!frmMain!SubFrm.Form(ButtonName).Caption = "X"
End Function

Call it like this:

ChangeBut "flt"

Note that this can be made simpler by passing the button object itself:

Function ChangeBut(objButton As Access.CommandButton)
objButton.Caption = "X"
End Function

and calling it like this:

ChangeBut Forms!frmMain!SubFrm.Form("flt")
 

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