?
Solved

Function to import a query as a Table

Posted on 2008-06-11
4
Medium Priority
?
783 Views
Last Modified: 2013-11-27
I have an Access Database that uses a snapshot query for ad hoc reports.  I do not want this user going into the Production Database to create new queries against this Query.  I know you can import a query as a Table.  How can I through a Function import a query as a Table from another database and replace the existing Table automatically.

Example the Table in my reporting database that comes from another database's query is named MainRecords_Snapshot.

The query in the database H:\Trucking\2007_SubmissionLog\Database\061008_Trucking_Submission_Log_NewChanges.mdb is named MainRecords_Snapshot

I run the function and it imports the query MainRecords_Snapshot from H:\Trucking\2007_SubmissionLog\Database\061008_Trucking_Submission_Log_NewChanges.mdb and replaces the Table MainRecords_Snapshot without prompts in my reporting database.

How can I do this?  Thanks
0
Comment
Question by:quizzer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 21766875
Why not create a base query (also must be set as a snapshot query) in the 'reporting' database that uses the snapshot query in the other database.

SELECT * FROM thesnapshotquery In "c:\myfolder\OtherDB.mdb"

0
 
LVL 27

Expert Comment

by:MikeToole
ID: 21766899
The attached routine should point the way.
If your table layout is going to stay constant then it would probably be better to delete all records and append the new ones rather than removing and recreating the table as the example shows.
Sub ImportQueryAsTable()
    Const cLocalTable As String = "SupplierLookUp"
    Const cSourceDB As String = "'C:\Users\Miket\Documents\Technical\Northwind 2007.accdb'"
    Const cSourceQuery As String = "qrySuppliers"
    Dim strSQL As String
    strSQL = "SELECT * INTO " & cLocalTable & " FROM " & cSourceQuery & " IN " & cSourceDB
    ' Ignore any error thrown if table doesn't already exist
    On Error Resume Next
    CurrentDb.TableDefs.Delete cLocalTable
    ' switch back to default error handling
    On Error GoTo 0
    DoCmd.SetWarnings False
    DoCmd.RunSQL strSQL
    DoCmd.SetWarnings True
End Sub

Open in new window

0
 
LVL 30

Accepted Solution

by:
hnasr earned 500 total points
ID: 21768707
Here is a try:
Remote database: C:\ManageRemote\db2.mdb
in db2, create a table t
field1      field2      field3
A      10      X
B      20      Y
C      30      Z

and a query q based on t

Select * from t;

The code snipped will create table q1t in local database populated with the result of q
Button Click event to run the code Snippet that follows.




Private Sub ManageRemoteDB_Click()
 
    Dim dfws As Workspace
    Dim rdb As Database 'remote database
    Dim rrs As Recordset 'remote record set
    Dim fld As Integer
    
    Set dfws = DBEngine.Workspaces(0)
    Set rdb = dfws.OpenDatabase("C:\ManageRemote\db2.mdb", True)
   'DoCmd.OpenForm db.Containers(2)("f")
   Set rrs = rdb.OpenRecordset(rdb.QueryDefs("q").Name) ' this is the query in rdb
   'steps to create table is here - keep going
   Dim flds As String
   flds = ""
    For fld = 0 To rrs.Fields.Count - 1
        If fld = 0 Then flds = "'1' As [" + rrs.Fields(fld).Name + "],"
        If fld = rrs.Fields.Count - 1 Then flds = flds + "'1' As [" + rrs.Fields(fld).Name + "]": Exit For
        If fld > 0 Then flds = flds + "'1' As [" + rrs.Fields(fld).Name + "],"
    Next fld
    flds = "SELECT " + flds + " INTO q1t"
    
    DoCmd.SetWarnings False
    DoCmd.RunSQL flds
    DoCmd.RunSQL "Delete * from q1t"
    With rrs
        Do Until rrs.EOF
            For fld = 0 To rrs.Fields.Count - 1
                If fld = 0 Then flds = """" & Nz(rrs.Fields(fld), "0") & ""","
                If fld = rrs.Fields.Count - 1 Then flds = flds + """" & Nz(rrs.Fields(fld), "0") & """": Exit For
                If fld > 0 Then flds = flds + """" & Nz(rrs.Fields(fld), "0") & ""","
            Next fld
            DoCmd.RunSQL "INSERT INTO q1t  VALUES (" & flds & ")"
           rrs.MoveNext
        Loop
    End With
    DoCmd.SetWarnings True
    rrs.Close
    rdb.Close
    Set rrs = Nothing
    Set rdb = Nothing
End Sub

Open in new window

0
 
LVL 30

Expert Comment

by:hnasr
ID: 21768743
<Function import a query as a Table from another database and replace the existing Table automatically.>

Yes: The comment above, does that!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Suggested Courses

770 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