Full Version: Getting Name Of Every Command Button
UtterAccess Discussion Forums > Microsoft® Access > Access Modules
gavinclarke
Hi

I have a form from which a user can select all the forms currently in the database via a combo box.

What i want to do is create another combo box that contains the name of all the command buttons on the form that the user selected from the combo box above, I have the following code to get the names of the button's but my problem is specifying the name of the form

frmName = Me.txtName.Value

Set frm = Forms!frmName 'PROBLEM LINE!!
For Each ctl In frm
' Check to see if control is command button.
If ctl.ControlType = acCommandButton Then
'I will set the record source for a combo box here with the button names
End If
Next ctl

As you can see the Set frm.Name = frmName doesn't work as the varibable frmName is a string.
How can i fix that so it sets the form correctly? (i.e. to the form the user selected in the text box 'txtName'??

Thanks!
schroep
Set frm = Forms(frmName)

Note that the form will need to be OPEN in order to reference its controls.
R. Hicks
I think frmName .. must be enclosed within quotations if the literal name of the form is used.

Forms("frmName")

RDH
schroep
It was a variable in his code, so no quotes needed. If it was the actual name of the form, then yes.
R. Hicks
I agree .. but in the eaxmple they posted they have:

frmName = Me.txtName.Value

But the variable type has not been declared (or I don't see it) ..
The would need:

Dim frmName As Form

RDH
schroep
I think in this case, it needs to be declared as a string, based on what was being assigned to it (the value of a textbox containing the name of a form):

Dim frmName as String

Use of "frm" as the prefix for this, however, makes it confusing; I would have used "strName". In fact, it's not even necessary to have a variable to hold this, since the value is in a control:

Dim frm As Form
Dim ctl As Control

Set frm = Forms(Me.txtName.Value)
...

will work just fine.

It's possible the OP just left off the declaration code, but I suspect the OP may not be using OPTION EXPLICIT to require variable declaration.

I would strongly recommend the OP do this:
1) Open up the VBE (code editor)
2) Select TOOLS/OPTIONS
3) Check the box that says "REQUIRE VARIABLE DECLARATION"
4) Click OK

This will automatically insert "OPTION EXPLICIT" in all NEW modules created. Existing modules would need to have this done manually. It is considered good coding practice to define all variables before using them, and this will enforce it.
gavinclarke
Thanks for your help

I have declared the variables and i do have Option Explicit declared, originally frmName was a string value (i have since removed it and replaced it with Me.txtName.Value

How can i go about populating the combo box with each command button's name?

Here's the code so far,
-------------------------------------------------------------------------------------
Private Sub cmdNext_Click()

Dim frm As Form
Dim ctl As Control

If tabWizard.Value = 1 Then
If Me.optForm.Value = True Then 'User Choose a Form

DoCmd.OpenForm Me.txtName.Value
Set frm = Forms(Me.txtName.Value)
DoCmd.OpenForm frm.Name, acNormal, , , , acHidden

For Each ctl In frm
' Check to see if control is command button.
If ctl.ControlType = acCommandButton Then
'Need to populate Combo box!
End If
Next ctl
DoCmd.Close acForm, frm.Name
tabWizard.Value = tabWizard.Value + 1
Else
tabWizard.Value = tabWizard.Value + 2
End If
ElseIf Not tabWizard.Value = 3 Then
tabWizard.Value = tabWizard.Value + 1
Else
cmdPrevious.SetFocus
End If

End Sub
--------------------------------------------------------------------------------
gavinclarke
I have it finished now - with the combo box updating, for completeness

----------------------------------------------------------------
Private Sub cmdNext_Click()
On Error GoTo Err_cmdNext:

Dim frm As Form
Dim ctl As Control
Dim frmName As String
Dim flagFirst As Boolean

flagFirst = True

If tabWizard.Value = 1 Then
If Me.optForm.Value = True Then 'User Choose a Form
frmName = Me.txtName.Value
DoCmd.OpenForm frmName, , , , , acHidden
Set frm = Forms(frmName)

For Each ctl In frm
' Check to see if control is command button
If ctl.ControlType = acCommandButton Then
'Populate Combo box with command button names
If flagFirst = True Then
'First Item in Combo Box
Me.txtFormButtonName.RowSource = ctl.Name & ";"
flagFirst = False
Else
Me.txtFormButtonName.RowSource = Me.txtFormButtonName.RowSource & ctl.Name & ";"
End If

End If
Next ctl
DoCmd.Close acForm, frmName
tabWizard.Value = tabWizard.Value + 1
Else
tabWizard.Value = tabWizard.Value + 2
End If
ElseIf Not tabWizard.Value = 3 Then
tabWizard.Value = tabWizard.Value + 1
Else
cmdPrevious.SetFocus
End If

Exit Sub

Err_cmdNext:

If Err.Number = 40036 Then
MsgBox "Ensure " & frmName & " has only one Click Event per button.", vbOKOnly, "Ambiguity Error"
Else
MsgBox Err.Description
End If
End Sub
--------------------------------------------------------------------------------------------------
schroep
Glad you got it working.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.