Site Search:
Sign in | Join | Help

This Blog

Syndication

Tags

No tags have been created or used yet.

Microsoft Access

Tricks and Tips for Microsoft Access

Code to loop through the querydefs collection in Access

 Recently I was migrating some tables to SQL from an old Access database. The original programmer had put spaces and other punctuation in the field names that SQL didn't allow so I changed them, but that broke Access when I linked the table back. This code will loop through the querydef collection and find all instances of a table name or field name.

 

 Sub custom()
    Dim strSQL As String
    Dim qry As QueryDef
    Dim db As Database
    Set db = CurrentDb
    
    For Each qry In db.QueryDefs
        strSQL = qry.SQL
        If InStr(strSQL, "[Product Master].[Product Number]") > 0 Then
            
            Debug.Print qry.Name
            
            
            'MsgBox qry.Name
            Debug.Print strSQL
            Debug.Print "======================="
        End If
        
    Next


End Sub

Comments

 

steve gray said:

That is a neat peice of coding.

I commented out the if instr section to

get a list of all the sql statements in my database.

This is a useful function to review all the queries in the current database.

May 5, 2009 12:30 PM
 

parshant said:

I m working on a stock inventory system. I make the grouping and subgrouping for storing the item according the category.i want to make a report of stock balance of all item including (subgrouping vlaue under the main group)like this for all subgroup but i m not able to do this.i m using sqlserver 2005 and  c#. I m able only to get the sum of all item under primary group not the item of all subgroup under primary group.

          plz help me.

July 23, 2010 8:54 PM

Leave a Comment

(required)  
(optional)
(required)  
Add