Link to home
Start Free TrialLog in
Avatar of dgheck
dgheckFlag for United States of America

asked on

Re-Caption Command Buttons on New Form Instance

I have a form for which I have created the ability to open multiple instances and it works well except for one problem. The form has several command buttons, the captions for which are written upon the form opening by looking into a table, retrieving the value and writing the caption.

THE PROBLEM: It works properly only on the original instance of the form. When I open any multiple form instance after that the captions don't get written to the command buttons.

The Sub to write the proper captions is located in the frmModule. What is preventing it from working when I open another instance of the form?

Thanks for any help.
Avatar of IrogSinta
IrogSinta
Flag of United States of America image

Can you post your code that this pertains to?
Avatar of dgheck

ASKER

Yes, thank you.  This is the Sub in the form's code module that re-captions the command buttons:

Private Sub LoadCommandButtonCaptions()
Dim strCurrentForm As String
strCurrentForm = Me.Form.Name

Dim strButtonCaption As String
Dim strButtonName As String
Dim strControlName As String


Dim intNext As Integer
Dim strNext As String

For intNext = 1 To 42
    strNext = Format(intNext, "0#")
    strButtonName = "cmd_ptb_REM" & strNext
    strControlName = "frmCurrent!" & strButtonName
    strButtonCaption = GetButtonCaption(strButtonName)
   
        With Forms(strCurrentForm).Controls(strButtonName)
        .SetFocus
        .Caption = strButtonCaption
    End With
Next intNext
End Sub

(Please forgive any inelegance in my coding, I am new at this and self-taught.)
Can you post your code that creates another instance of this form?
If you declare a Form object at the module level and then use that to create another instance of the form, you can then use your form variable to refer to your buttons.  For instance, at the top of your form module, you could declare Dim frmNew as Form  Then you would create another instance of your form this way:
Set frmNew = New Form_NameOfYourForm
frmNew.SetFocus

Open in new window

Then you can refer to your controls this way:
frmNew.btnNameOfButton.Caption = GetButtonCaption(strButtonName)
Avatar of dgheck

ASKER

I am still unclear as to the proper locations for the code snippets.

Here is the code contained in the Form's Module to write the captions to the Command Buttons:

Private Sub LoadCommandButtonCaptions()

Dim strCurrentForm As String
strCurrentForm = "frm_add_edit_instrument"

Dim strButtonCaption As String
Dim strButtonName As String
Dim strControlName As String


Dim intNext As Integer
Dim strNext As String

For intNext = 1 To 42
    strNext = Format(intNext, "0#")
    strButtonName = "cmd_ptb_REM" & strNext
    strControlName = "frmCurrent!" & strButtonName
    strButtonCaption = GetButtonCaption(strButtonName)
        'With frmNew
        With Forms(strCurrentForm).Controls(strButtonName)
        .SetFocus
        .Caption = GetButtonCaption(strButtonName)
    End With
Next intNext
End Sub

And here is the code in a Standard Module that opens the new form instance:

ublic Function OpenFormInstance(FormName As String, Optional WhereCondition As String)

'Declare the Form Name
Dim frmNew As Form

Set frmNew = New Form_frm_add_edit_instrument

If WhereCondition <> "" Then
    frmNew.Filter = WhereCondition
    frmNew.FilterOn = True
End If

'Make the Form Visible
frmNew.Visible = True


'Alter Form's Caption


'Need to add a reference to the form so it doesn't immediately close when the form variable goes out of scope
mcolFormInstances.Add frmNew


End Function

Hope this assists you in helping me out. Thank you in advance for any time spent on this.
ASKER CERTIFIED SOLUTION
Avatar of IrogSinta
IrogSinta
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dgheck

ASKER

Bingo! That works. Thanks.