• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 588
  • Last Modified:

Newly added Column in Table that Query is not pulling

Hello,

is there a way to compare a query definition to a table definition and tell if the query is missing any newly added column from the table.

Reason - i have a query that pulls all the columns from that same table but renames all of them to increase readability. I cant store the readable columns as headers in the original table as they have special characters and spaces in them.

so the only way is to >> create  query to pull those renamed columns by the column names i need.

now bad news - if a new column is added to table, the query has to be updated to include that new column too

so is there  query or vba to tell me what columns are in table1 which are not getting pulled by query?


Thank you
0
Rayne
Asked:
Rayne
  • 6
  • 3
1 Solution
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
<<so is there  query or vba to tell me what columns are in table1 which are not getting pulled by query?>>

 That would be doable in VBA.   A TableDef(), QueryDef, and Recordset all have a fields collection which allows you to look at the fields. So if you went to the debug window and did:

? CurrentDB().TableDefs("myTable").Fields(0).Name

 You would get the field name of the first filed in that table.

 Below is some example code from the online help with working with the fields collection and the field object.

 That should get your started.

Jim.

Sub FieldX()

      Dim dbsNorthwind As Database
      Dim rstEmployees As Recordset
      Dim fldTableDef As Field
      Dim fldQueryDef As Field
      Dim fldRecordset As Field
      Dim fldRelation As Field
      Dim fldIndex As Field
      Dim prpLoop As Property

      Set dbsNorthwind = OpenDatabase("Northwind.mdb")
      Set rstEmployees = _
            dbsNorthwind.OpenRecordset("Employees")

      ' Assign a Field object from different Fields
      ' collections to object variables.

Set fldTableDef = _
            dbsNorthwind.TableDefs(0).Fields(0)
      Set fldQueryDef =dbsNorthwind.QueryDefs(0).Fields(0)
      Set fldRecordset = rstEmployees.Fields(0)
      Set fldRelation =dbsNorthwind.Relations(0).Fields(0)
      Set fldIndex = _
            dbsNorthwind.TableDefs(0).Indexes(0).Fields(0)

      ' Print report.
      FieldOutput "TableDef", fldTableDef
      FieldOutput "QueryDef", fldQueryDef
      FieldOutput "Recordset", fldRecordset
      FieldOutput "Relation", fldRelation

FieldOutput "Index", fldIndex

      rstEmployees.Close
      dbsNorthwind.Close

End Sub

Sub FieldOutput(strTemp As String, fldTemp As Field)
      ' Report function for FieldX.

      Dim prpLoop As Property

      Debug.Print "Valid Field properties in " & strTemp

      ' Enumerate Properties collection of passed Field
      ' object.
      For Each prpLoop In fldTemp.Properties
            ' Some properties are invalid in certain
            ' contexts (the Value property in the Fields

' collection of a TableDef for example). Any
            ' attempt to use an invalid property will
            ' trigger an error.
            On Error Resume Next
            Debug.Print "    " & prpLoop.Name & " = " & _
                  prpLoop.Value
            On Error GoTo 0
      Next prpLoop

End Sub
0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
I should add that the fields collection also has a Count Property, so you can easily do a loop on the table, store all the field names, then do the same for the querydef and figure out if all fields are referenced or not.

Jim.
0
 
RayneAuthor Commented:
Thank you Jim,

This is Awesome :)
I just put on my developer hat...I am going through it
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
RayneAuthor Commented:
this is beautiful and i can customize it to perfection, thank you again Jim
You are a life saver
0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
No problem.

Jim.
0
 
RayneAuthor Commented:
Hello Jim,

i was able to pull in all the actual field names from the original table. Now i twicked the loop a bit so that it goes through each field in query and tells me their actual Source names (Not the renamed column names) - but its not doing that. Its pulling renamed column names only

Sub FieldX()

    Dim dbsNorthwind As Database
    Dim rstEmployees As Recordset
    Dim fldTableDef As Field
    Dim fldQueryDef As Field
    Dim fldRecordset As Field
    Dim fldRelation As Field
    Dim fldIndex As Field
    Dim prpLoop As Property

    Set dbsNorthwind = CurrentDb
    'open query
    Set rstEmployees = _
    dbsNorthwind.OpenRecordset("qryRenamed")

    'display query columns-
    For Each flds In rstEmployees.Fields
        Set fldTableDef = flds
        ' Print report.
        FieldOutput "TableDef", fldTableDef
    Next flds

    rstEmployees.Close
    dbsNorthwind.Close

End Sub

Open in new window

0
 
RayneAuthor Commented:
any ideas to pull the actual field names and not the renamed field names from the query?

tHank you
0
 
RayneAuthor Commented:
further i modified the diplay function a bit like this to only show field name

Sub FieldOutput(strTemp As String, fldTemp As Field)
' Report function for FieldX.

    Dim prpLoop As Property

    'Debug.Print "Valid Field properties in " & strTemp

    ' Enumerate Properties collection of passed Field
    ' object.
    For Each prpLoop In fldTemp.Properties
        ' Some properties are invalid in certain
        ' contexts (the Value property in the Fields

        ' collection of a TableDef for example). Any
        ' attempt to use an invalid property will
        ' trigger an error.
        If LCase(prpLoop.Name) = "name" Then
            On Error Resume Next
            'Debug.Print "    " & prpLoop.Name & " = " & _
             prpLoop.Value
            Debug.Print prpLoop.Value
            Exit For
            On Error GoTo 0
        End If
    Next prpLoop
    '    Stop
End Sub

Open in new window

0
 
RayneAuthor Commented:
I got it, thanks
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

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