The problem;
Run-time error '438': Object doesn't support this property or method
I'm trying to dynamically load buttons based on the results of a query. I have a recordset setup to index through said results, and set the Captions on the buttons to match each individual result of the query.
This code I initially had attached to the on open event of the subform that contains all the buttons. Each button has a set name. Command 1-15 on the first page, 16-30 on the second, etc. This code worked initially, but I wanted to move it into a module that way I could call it from any of the subforms instead of putting this large codeblock into each subforms on open event (12+).
My first issue was trying to pinpoint the specific button with a variable which I was able to solve below.
Forms![001_frm_Home]![Subform01]![Subform02].['uTargetButton'].Caption = Trim(rs!Itemkey)
I've tried every combination of quotes, setting the .Caption property to a variable and using the variable in the above line. I've seen a few posts on various forums saying this might not be possible for that I may need to define the command button as a command button in this function in order to access the .Caption property. I'll put the code block below;
Set rs = CreateObject("ADODB.Recordset")
rs.CursorType = 2 ' needed to set the recordset for updates
rs.LockType = 2
rs.Open strsql, con, 1 ' 1 = adOpenKeyset
If rs.RecordCount > 0 Then ' We found a good number, ok to move on
rs.MoveFirst
uButtonNumber = uStartButton
uCount = 0
Do While Not rs.EOF
uButtonNumber = uButtonNumber + 1
uCount = uCount + 1
uTargetButton = "Command" & uButtonNumber
Forms![001_frm_Home]![Subform01]![Subform02].['uTargetButton'].Caption = Trim(rs!Itemkey)
rs.MoveNext
Loop
Else 'No Record
Hopefully all that makes sense, let me know if any further info would help. Thanks in advance.
You must access the Form
property of the Subform Control:
Forms![001_frm_Home]![Subform01].Form!uTargetButton.Caption = "Hello"
If you are calling this from the 001_frm_Home
form, you can also write:
Me![Subform01].Form!uTargetButton.Caption = "Hello"
Explanation, when you add a subform to a form, a Subform
control is added to the parent form. This Subform
control has a property Source Object
in the Data
tab of the Property Sheet. Here you can select a form from a drop-down. The form selected as subform is available programmatically through the Form
property.
It seems that you have subforms nested twice. Then this would be:
Forms![001_frm_Home]![Subform01].Form![Subform02].Form(uTargetButton).Caption = "x"
Okay did not understand that uTargetButton
was supposed to be a variable name, because you delimited it by single quotes.
You can access a control by a name given as string with Form(nameAsString)
. The ()
are indexing the controls default collection. You could also write Form.Controls(nameAsString)
. This is also equivalent to Form.Controls!theName
or Form!theName
or Form![theName]
where theName
is the name not given as string.