Solved

Newly added Column in Table that Query is not pulling

Posted on 2014-04-19
9
578 Views
Last Modified: 2014-04-19
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
Comment
Question by:Rayne
  • 6
  • 3
9 Comments
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 40010383
<<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
 
LVL 57
ID: 40010387
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
 

Author Comment

by:Rayne
ID: 40010409
Thank you Jim,

This is Awesome :)
I just put on my developer hat...I am going through it
0
 

Author Comment

by:Rayne
ID: 40010428
this is beautiful and i can customize it to perfection, thank you again Jim
You are a life saver
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 57
ID: 40010431
No problem.

Jim.
0
 

Author Comment

by:Rayne
ID: 40010437
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
 

Author Comment

by:Rayne
ID: 40010438
any ideas to pull the actual field names and not the renamed field names from the query?

tHank you
0
 

Author Comment

by:Rayne
ID: 40010440
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
 

Author Comment

by:Rayne
ID: 40010491
I got it, thanks
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now