Results 1 to 5 of 5
  1. #1
    2 Star Lounger
    Join Date
    May 2002
    Location
    Quincy, Illinois
    Posts
    138
    Thanks
    0
    Thanked 0 Times in 0 Posts

    For Each Field in a Form (Office 2003)

    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

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: For Each Field in a Form (Office 2003)

    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.

  3. #3
    2 Star Lounger
    Join Date
    May 2002
    Location
    Quincy, Illinois
    Posts
    138
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: For Each Field in a Form (Office 2003)

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

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: For Each Field in a Form (Office 2003)

    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.

  5. #5
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Brussels, Brussel, Belgium
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: For Each Field in a Form (Office 2003)

    Edited by HansV to change <!t>[script] tags to <!t>[code] tags. Please don't use Script - it makes code unreadable <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> 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.)

    <code>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</code>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •