Calling form fields by string-name?

O

Oliverm

Dear All

The name of my form is "form1". It contains two input fields:
"field_1" and "field_2".


For both files I'd like to have the same action, done in function
"TextFieldName".


Function SetTextField(TextFieldName As String)
form1.<<Name_of_Field_n>>.Font = 0
End Function

What is the syntax to pass a string as a form-name-object?

Kind regards
Oliver

I've been trying with:
- form1("TextFieldName").font = ...
- form1.Item("TextFieldName").font = ...
- form1.Index("TextFieldName").font = ...
... with no luck...
 
G

George Lee

Oliverm said:
Dear All

The name of my form is "form1". It contains two input fields:
"field_1" and "field_2".


For both files I'd like to have the same action, done in function
"TextFieldName".


Function SetTextField(TextFieldName As String)
form1.<<Name_of_Field_n>>.Font = 0
End Function

What is the syntax to pass a string as a form-name-object?

Kind regards
Oliver

I've been trying with:
- form1("TextFieldName").font = ...
- form1.Item("TextFieldName").font = ...
- form1.Index("TextFieldName").font = ...
... with no luck...

Get to know the debugger well. It’ll always show the object type in the
Watch window. For lack of any better starting point, you can scroll through
the items, such as form1, to see if anything strikes you as useable, such as
Controls.

In this case, try:

Private Sub CommandButton1_Click()
ChangeByObject form1.field_1
ChangeByName "field_2"
End Sub

Private Sub ChangeByObject(whichItem As Control)
whichItem.Font.Name = "Algerian"
End Sub

Private Sub ChangeByName(controlName As String)
form1.Controls(controlName).Font.Name = "Algerian"
End Sub
 
O

Oliverm

Get to know the debugger well. It’ll always show the object type in the
Watch window. For lack of any better starting point, you can scroll through
the items, such as form1, to see if anything strikes you as useable, such as
Controls.

In this case, try:

Private Sub CommandButton1_Click()
    ChangeByObject form1.field_1
    ChangeByName "field_2"
End Sub

Private Sub ChangeByObject(whichItem As Control)
    whichItem.Font.Name = "Algerian"
End Sub

Private Sub ChangeByName(controlName As String)
    form1.Controls(controlName).Font.Name = "Algerian"
End Sub











- Zitierten Text anzeigen -- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

Hello George

Thank you for your help.
Unfortunately, it does not behave as expected; I am getting "Object
does not support Method or attributes".
See code below. Also no luck with "ChangeByObject".

Further suggestions would be very welcome.

Kind regards
Oliver


BUTTON:
---------------
Private Sub but_Commands_Font_Click()
SetTextField "txt_Commands" ' where txt_Commands is
the field name of a form field.
End Sub


CODE:
-----------

Function SetTextField(TextFieldName As String)

With Application.Dialogs(wdDialogFormatDefineStyleFont)
.Display

frm_Config.Controls(TextFieldName).Font = .Font
frm_Config.Controls(TextFieldName).ForeColor = FontColorRGB
frm_Config.txt_Commands.Font.Bold
= .Bold ' Working example

End With

End Function
 

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