• For Each Field in a Form (Office 2003)

    Author
    Topic
    #436207

    I’m attempting to write a function that will return a text string with all field names in a given form. I’ve tried using For Each [control] in [form], but I can’t get it to work. Does anyone have an example that I could take a look at or another way to retrieve this information.

    Thanks in advance,
    Drew

    Viewing 1 reply thread
    Author
    Replies
    • #1033352

      What kind of form do you mean? A userform created in the Visual Basic Editor, or a form in a Word document using controls from the Forms or Control Toolbox toolbars?

      For a userform, you can use

      Dim ctl As Control
      For Each ctl In UserForm1.Controls

      Next ctl

      where UserForm1 is the name of the userform. If the code resides in the module associated with the userform, you can use Me.Controls instead of UserForm1.Controls.

      • #1033938

        The form I am using is an Access form.
        I see what I was doing wrong based on what you have indicated.
        Thank you

        • #1033942

          Glad you solved it. In the future, please post Access questions in the Access forum, even if they are about Access VBA, since it it rather different from the other Office apps.

    • #1033914

      Edited by HansV to change [script] tags to tags. Please don't use Script - it makes code unreadable cheers Hans, i just was a little bit rusty as you might have guessed.

      the following code will create a new document with all the field names in case you are talking about form fields on a document. it will also list the names in the direct window (CTRL+G in the VB editor.)

      Sub ListFormfields()
      Dim FormFieldNames As String
      Dim aFormField As FormField

      For Each aFormField In ActiveDocument.FormFields
      Debug.Print aFormField.Name
      FormFieldNames = (FormFieldNames & aFormField.Name & vbCrLf)
      Next
      Documents.Add.Range.Text = FormFieldNames
      End Sub

    Viewing 1 reply thread
    Reply To: For Each Field in a Form (Office 2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: