Solved

Newly added Column in Table that Query is not pulling

Posted on 2014-04-19
9
579 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access 2016 7 33
One Drive Login/Configuration Issue 12 38
SQL Insert parts by customer 12 31
Access: Retrieving Current Month's Orders for Invoice 6 27
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

785 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