Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 81
  • Last Modified:

Retrieving info about Source Table from Access 2016 Query

I connect to to Access with Excel VBA/ADO. I know how to retrieve meta data 'FieldNames', 'DataType'.

When the 'table' is a 'query', I'd like to retrieve the name of the source table for the field, and the original fieldname (not the alias in the Query).

Is this possible with Access 2016?

Thanks
0
hindersaliva
Asked:
hindersaliva
  • 2
1 Solution
 
PatHartmanCommented:
You can't do it with a queyr but you can do it by examining the QueryDefs collection.  This procedure comes from my documentation tool.  It adds the attributes to a table.  you would need to run it whenever you changed/added a query to keep the table up to date.

Hopefully, you can interpret this to suit your need.  I run it in databaseA to document databaseB so you will have to change some of the references if you want to run it within the database you are documenting.
Sub Create_tblQueryFields()

    Dim db As DAO.Database
    Dim qryLoop As DAO.QueryDef
    Dim fldLoop As DAO.Field
    Dim TD1 As DAO.TableDef
    Dim QD1 As DAO.QueryDef
    Dim TempSet1 As DAO.Recordset
    Dim strDatabase As String
    Dim ThisDB As DAO.Database
    Dim CountQueries As Integer
    
    On Error GoTo Err_Create_tblQueryFields
    strDatabase = Forms!frmPrintDoc!txtDBName
    
    CountQueries = 0
    Set ThisDB = CurrentDb()
    If strDatabase = "" Then
        Set db = CurrentDb()
    Else
        Set db = DBEngine.Workspaces(0).OpenDatabase(strDatabase)
    End If
    
    db.Containers.Refresh

    Set TD1 = ThisDB.TableDefs!tblQueryFields
    Set TempSet1 = TD1.OpenRecordset

    ' Enumerate QueryDefs collection.
    For Each qryLoop In db.QueryDefs
        ' Enumerate Fields collection of each
        ' QueryDef object.
                
        CountQueries = CountQueries + 1
        Forms!frmPrintDoc!TxtQueryCount = CountQueries
        Forms!frmPrintDoc!txtQueryName = qryLoop.Name
        Forms!frmPrintDoc.Repaint
        'Debug.Print qryLoop.Name
        'Debug.Print qryLoop.SQL
        For Each fldLoop In qryLoop.Fields
            If Left(qryLoop.Name, 1) = "z" Or Left(qryLoop.Name, 2) = "xx" Then     'don't ignor leading ~ since those are embedded queries
            Else
                TempSet1.AddNew
                TempSet1!QueryName = qryLoop.Name
                'Debug.Print qryLoop.Name & "-" & fldLoop.Name & "-" & fldLoop.Attributes
                TempSet1!FieldName = fldLoop.Name
                TempSet1!SourceField = fldLoop.SourceField
                TempSet1!SourceTable = fldLoop.SourceTable
                TempSet1!OrdinalPosition = fldLoop.OrdinalPosition
                TempSet1!RecordsetType = qryLoop.Type
                TempSet1!SQL = qryLoop.SQL
                TempSet1!AllowZeroLength = fldLoop.AllowZeroLength
                TempSet1!DefaultValue = fldLoop.DefaultValue
                'TempSet1!FieldSize = fldLoop.FieldSize
                TempSet1!Required = fldLoop.Required
                TempSet1!Type = fldLoop.Type
                TempSet1!ValidationRule = fldLoop.ValidationRule
                TempSet1.Update
            End If
        Next fldLoop
    Next qryLoop

Exit_Create_tblQueryFields:
    db.Close
    Exit Sub

Err_Create_tblQueryFields:
    Select Case Err.Number
        Case 3043, 3055
            MsgBox "Please select a valid database. Error #" & Err.Number, vbOKOnly
        Case 91   ' db was not opened so it cannot be closed.
            Exit Sub
        Case Else
            MsgBox Err.Number & "-" & Err.Description
    End Select
    Resume Exit_Create_tblQueryFields
    Resume
End Sub

Open in new window

0
 
hindersalivaAuthor Commented:
Thanks Pat.
0
 
PatHartmanCommented:
You're welcome.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now