The following create an "alias" query for every table in the collection. I'm sure you can modify this to do what you want.Good LuckSub createAliasQueries() Dim wks As Workspace Dim db As Database Dim dbMe As Database Dim rs As Recordset Dim tdf As TableDef Dim qdf As QueryDef Dim myfield As Field Dim intResponse As Integer Dim strDBPath As String Dim strField As String Dim strSQL As String Dim strSQLTableName As String Dim strSQLColumnName As String 'JR 'JR1 'strDBPath = "E:\Technology Services\Projects\Tax Compliance\TaxDB_LedgerData\Development\TaxDB_LedgerData.mdb" 'JR2 strDBPath = "E:\Technology Services\Projects\Tax Compliance\TaxDB_ReviewData\Development\TaxDB_ReviewData.mdb" strField = Chr(13) strSQL = "SELECT " 'JR Set wks = DBEngine.CreateWorkspace("temp", "Admin", "pwd") 'Set wks = DBEngine.CreateWorkspace("temp", "sysadmin", "pwd") Set db = wks.OpenDatabase(strDBPath) Set dbMe = CurrentDb db.TableDefs.Refresh MsgBox "Number of TableDefs: " & db.TableDefs.Count 'Loop trhough all the TableDefs in the database For Each tdf In db.TableDefs 'Ignores system tables, which begin with "MSys" prefix If InStr(tdf.Name, "MSys") = 0 Then strSQLTableName = tdf.Name 'Look for table names with zero first character (not allowed in SQL Server) If Left(strSQLTableName, 1) = "0" Then strSQLTableName = "Z" & strSQLTableName End If 'Look for tables names with spaces (not allowed in SQL Server) While InStr(strSQLTableName, " ") > 0 Mid(strSQLTableName, InStr(strSQLTableName, " "), 1) = "_" Wend 'Grab all the column names 'JR1 'Set qdf = dbMe.QueryDefs("qrySQLServerTableColumns1") 'JR2 Set qdf = dbMe.QueryDefs("qrySQLServerTableColumns2") qdf![parTableName] = strSQLTableName Set rs = qdf.OpenRecordset 'Skips any table names not contained in the lookup table If rs.RecordCount > 0 Then rs.MoveFirst 'Loop through all the fields in the TableDef and add them to the message and SQL strings For Each myfield In tdf.Fields strSQLColumnName = rs![FieldName] strField = strField & Chr(13) & myfield.OrdinalPosition & " " & myfield.Name 'JR If strSQLColumnName = myfield.Name Then strSQL = strSQL & "[" & strSQLColumnName & "] , " & Chr(13) Else strSQL = strSQL & "[" & strSQLColumnName & "] AS [" & myfield.Name & "] , " & Chr(13) End If rs.MoveNext Next 'Chop the last comma and space off the sql statement, then add a space before the "FROM" clause strSQL = Left(strSQL, Len(strSQL) - 3) & " " & Chr(13) 'Complete the SQL statement strSQL = strSQL & "FROM [dbo_" & strSQLTableName & "]" intResponse = MsgBox(tdf.Name & " will be converted to a query named z_" & tdf.Name & "." & Chr(13) & _ "If the SQL statement below appears correct, click 'Yes.'" & Chr(13) & _ "If not, click 'No' and the query will be named zBAD_" & tdf.Name & "." & _ Chr(13) & Chr(13) & strSQL, vbYesNo) If intResponse = vbYes Then 'Create a new query to replace the table 'JR Set qdf = dbMe.CreateQueryDef(tdf.Name, strSQL) 'Set qdf = db.CreateQueryDef("z_" & tdf.Name, strSQL) Else 'Create a new query to replace the table, but name it differently so user knows to edit the query Set qdf = dbMe.CreateQueryDef("zBAD_" & tdf.Name, strSQL) End If End If 'Reset the two strings strField = "" & Chr(13) strSQL = "SELECT " End If Next MsgBox "Done." rs.Close qdf.Close dbMe.Close db.Close wks.CloseEnd Sub
Brett8-)