• Loop through query fields with DAO. (2000)

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Loop through query fields with DAO. (2000)

    Author
    Topic
    #407367

    I’m trying to use some code I wrote a while ago but it doesn’t seem to work. Now I’m wondering if it ever did.
    When it gets to the line ‘For Each qfield In qdef.Fields’ it drops out to the last ‘End if’.

    If I modify it to work with tables it works fine.

    Sub InspectMyQueries()
    ‘cycle through the queries
    ‘retrieve those used to make tables for arcview – begin with ‘p’
    ‘get any fields in those queries longer than 10 characters

    Dim db As DAO.Database
    Dim qdef As DAO.QueryDef
    Dim qfield As DAO.Field
    Dim strInfo As String

    Set db = CurrentDb

    For Each qdef In db.QueryDefs
    If Left(qdef.Name, 1) = “p” Then
    ‘Debug.Print qdef.Name

    For Each qfield In qdef.Fields

    If Len(qfield.Name) > 10 Then

    strInfo = qdef.Name & vbTab & qfield.Name
    Debug.Print strInfo

    End If

    Next

    End If

    Next qdef

    End Sub

    Viewing 1 reply thread
    Author
    Replies
    • #851603

      Your code works fine in my test database. Please note that it only returns information about fields whose name is longer than 10 characters in queries whose name starts with ‘p’. Perhaps there are currently no such fields/queries in your database?

      Added later: one other thing to keep in mind is that the Fields collection for action queries (update queries, delete queries etc.) is empty.

      • #851607

        I’ve just read you ‘added later’ bit. The queries in question are indeed action queries (make table queries) so I guess that’s why its not working clever

        When I first used it I must have run it on select queries to check they were okay before I turned them into make table queries.

        Never mind, I guess I can just run the ‘table’ version.

        Can you explain why the fields collection is empty though.

        • #851609

          The Fields collection represents the columns displayed in the datasheet view of the query. In a Select query, fields used only to set criteria and/or to specify the sort order (the Show check box is cleared) do not belong to the Fields collection. Action queries do not display columns at all, so their Fields collection is empty.

          • #851697

            Thats worth knowing, I’d have probably been caught out at some point with a select query otherwise.

            Thanks again, for making everything crystal clear as usual bravo

          • #851698

            Thats worth knowing, I’d have probably been caught out at some point with a select query otherwise.

            Thanks again, for making everything crystal clear as usual bravo

        • #851610

          The Fields collection represents the columns displayed in the datasheet view of the query. In a Select query, fields used only to set criteria and/or to specify the sort order (the Show check box is cleared) do not belong to the Fields collection. Action queries do not display columns at all, so their Fields collection is empty.

      • #851608

        I’ve just read you ‘added later’ bit. The queries in question are indeed action queries (make table queries) so I guess that’s why its not working clever

        When I first used it I must have run it on select queries to check they were okay before I turned them into make table queries.

        Never mind, I guess I can just run the ‘table’ version.

        Can you explain why the fields collection is empty though.

    • #851604

      Your code works fine in my test database. Please note that it only returns information about fields whose name is longer than 10 characters in queries whose name starts with ‘p’. Perhaps there are currently no such fields/queries in your database?

      Added later: one other thing to keep in mind is that the Fields collection for action queries (update queries, delete queries etc.) is empty.

    Viewing 1 reply thread
    Reply To: Loop through query fields with DAO. (2000)

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

    Your information: