Results 1 to 5 of 5
  1. #1
    Whizbang is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Posts
    37

    Union query from a changing number of databases


    We have an SQL server here at work that is used to hold customer transactions. Each customer has their own database. I had nothing to do with the design of the databases/server. It is an application provided by a vendor. Each new customer gets a new database with the same structure.

    My task is to simply generate some better reports. There are reports for each customer, but there is nothing that totals all the customer transactions up to do company-wide reports. I know how to do a UNION query to combine two similar tables, but I don't know how to do that when the number of tables may change over time, and the tables themselves come from different databases on the same server.

    So, get a list of databases, then append the various tables into a single table to be totaled up. Any ideas or resources?

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    You might be able to use VBA to modify the QueryDef of the saved query object. However, the only time I tried to use VBA to work with UNION query failed. If UNION query was anywhere in a sequence of queries, VBA would not open recordset. I had to modify procdure to eliminate UNION query. Modifying QueryDef might be different, maybe VBA won't care that the SQL statement has UNION.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Whizbang is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Posts
    37
    Here is a small sql statement I have worked out.

    Code:
    SELECT  'Customer1' as [CustomerName], [Field1], [Field2], [Field3], [Field4], [Field5], [Field6]
    FROM [Customer1].[dbo].[TransTable]
    UNION ALL
    SELECT  'Customer2' as [CustomerName], [Field1], [Field2], [Field3], [Field4], [Field5], [Field6]
    FROM [Customer2].[dbo].[TransTable];
    This is a passthrough query using the ODBC Connect Str of:
    Code:
    ODBC;Description=MYSERVER;DRIVER=SQL Server;SERVER=MYSERVER;Trusted_Connection=Yes;
    I can manually add to this SQL statement for each of the current customers. This will work just fine, except that as new customers come on and old customers leave or go inactive, I will need to update this string. Is there a way I can somehow have the DB name be variable, and somehow use a list of customers to generate the results?

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Not in a saved Access query object. Again, would need VBA code to modify the QueryDef. The code could refer to control on form as variable for the db name, construct the SQL string, modify the QueryDef.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Since all Customer databases have the same structure, why not (create some vba to) import the data to a "TempTable", and design query/queries to do what ever manipulations/ recordsource for Report(s) as necessary?

    You say a varyiing number of Cistomers -- how does that work?

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Union Query Help
    By pmp in forum Queries
    Replies: 4
    Last Post: 10-28-2011, 06:41 AM
  2. Query several databases to UPDATE one
    By Kelrizzo in forum Queries
    Replies: 4
    Last Post: 10-27-2011, 10:26 AM
  3. Please help me out with union query
    By radicrains in forum Access
    Replies: 1
    Last Post: 10-29-2010, 01:48 AM
  4. Need help with a Union Query
    By jdowdy in forum Access
    Replies: 1
    Last Post: 10-13-2009, 05:24 PM
  5. VBA SQL Query of Multiple Databases
    By VBA_Rookie in forum Programming
    Replies: 0
    Last Post: 12-02-2008, 12:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums