Results 1 to 5 of 5
  1. #1
    Learner is offline Novice
    Windows 7 32bit Access 2013 32bit
    Join Date
    Jan 2020

    To retrieve data from 2 Tables with one field name as same & others with different?


    The below code shows data in 4 columns but I would like to have it in 7 columns as shown in the attached image.

    "Select CustomerName,InvoiceDate,InvoiceNumber,SUM(InvoiceAmount) from SalesTable WHERE [CustomerName] Like '%" & txtSearchCustomer.Text & "%' GROUP BY CustomerName,InvoiceDate,InvoiceNumber ORDER BY InvoiceNumber ASC
            UNION Select CustomerName,ReceiptDate,ReceiptNumber,ReceivedAmount from ReceiptsTable WHERE [CustomerName] Like '%" & txtSearchCustomer.Text & "%'"

    Thanks in advance for any kind support.
    Attached Thumbnails Attached Thumbnails Statement_Result.PNG  

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    in the query designer, join the 2 tables on the CustomerID,
    then drag down the fields needed from both tables.

  3. #3
    Learner is offline Novice
    Windows 7 32bit Access 2013 32bit
    Join Date
    Jan 2020
    I am using this query in application. And the result should be like in this image (In earlier image the last table which shows the "Result" is not correct).
    I am not much familiar with queries. Just got this help from one of the forums in 2017.
    Note: I don't have customerID in both Tables.
    Attached Thumbnails Attached Thumbnails Statement_Result.jpg  

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Ottawa, Ontario, Canada; West Palm Beach FL
    How about showing readers your table designs and any relationships you have?
    A screen shot of your relationships window(jpg/png) or a copy of the database(personal info removed) would be helpful.

  5. #5
    CarlettoFed is offline Advanced Beginner
    Windows 7 64bit Access 2013 32bit
    Join Date
    Dec 2019
    If the names of the tables and fields are those in the image, try this query:
    SELECT qryUnion.CustomerName, qryUnion.InoiceDate, qryUnion.InoiceNumber, qryUnion.InoiceAmount, qryUnion.ReceiptDate, qryUnion.ReceiptNumber, qryUnion.ReceivedAmount
    FROM (SELECT SalesTable.CustomerName, SalesTable.InoiceDate, SalesTable.InoiceNumber, SalesTable.InoiceAmount, "-" AS ReceiptDate,"-" AS ReceiptNumber,"-" AS ReceivedAmount, SalesTable.InoiceDate AS DateMove, 1 AS Priority
    FROM SalesTable
    WHERE SalesTable.CustomerName="New Fashion Dresses"
    UNION SELECT ReceiptsTable.CustomerName, "-" AS InoiceDate, "-" AS InoiceNumber, "-" AS InoiceAmount, ReceiptsTable.ReceiptDate, ReceiptsTable.ReceiptNumber, ReceiptsTable.ReceivedAmount, ReceiptsTable.ReceiptDate AS DateMove, 0 AS Priority
    FROM ReceiptsTable
    WHERE ReceiptsTable.CustomerName="New Fashion Dresses") As qryUnion
    ORDER BY qryUnion.DateMove, qryUnion.Priority;
    otherwise correct the name of the tables and fields.

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

Similar Threads

  1. Replies: 5
    Last Post: 04-30-2015, 02:00 PM
  2. Replies: 6
    Last Post: 12-12-2013, 11:44 PM
  3. Replies: 1
    Last Post: 11-08-2012, 02:37 PM
  4. Replies: 3
    Last Post: 08-18-2012, 03:25 AM
  5. Replies: 1
    Last Post: 03-09-2012, 07:43 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 - Senior Forums