Results 1 to 10 of 10
  1. #1
    grad2009 is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2010
    Posts
    27

    union query with a calculated field


    hello all,

    i used a union query. the last field in that query (balance) is a calculated field. When i open that query it asked to enter credit-debit, i don't want to enter anything because it is a calculated field.

    can you help me please? i will be thankful for you

    Code:
    SELECT Customers.customerNo as No, Customers.customerName as name, Sales.sInvoiceNo as INo, Sales.sDate as date, Sales.total as credit, 0 as debit, [credit-debit] as balance FROM Customers INNER JOIN Sales ON Customers.customerNo = Sales.customerNo UNION SELECT Customers.customerNo, Customers.customerName, Payment.paymentNo, Payment.pDate, 0 as credit, Payment.amount, [credit-debit] as balance FROM Customers INNER JOIN Payment ON Customers.customerNo=Payment.customerNo UNION SELECT Customers.customerNo, Customers.customerName, Received.receiptNo, Received.rDate, Received.amount, 0 as debit, [credit-debit] as balance FROM Customers INNER JOIN Received ON Customers.customerNo = Received.customerNo;

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Are you aware that Name and Date are reserved words? http://www.allenbrowne.com/AppIssueBadWord.html
    You also can not use the alias you assigned when forming another calculation in the same record. You need to use the actual field name.

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    On top of Allan's good points, you're getting a prompt because you have the fields bracketed together. You have this

    [credit-debit] as balance

    you want this

    [credit]-[debit] as balance
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Paul,
    Can you use the alias in this case? I know I've run into problems before doing that.

  5. #5
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    The OP has also not defined both of the fields in each UNION.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    In my experience you can use the alias in the SELECT clause (won't say always, as there are probably exceptions). Where you can run into trouble is using the alias in the WHERE clause. Because the WHERE clause is evaluated before the SELECT clause, the aliases won't work there and will throw a parameter prompt.

    You appear to be correct about the fields not being defined consistently. I didn't notice that.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I think it was using the alias in a WHERE Clause that tripped me up before. Good on ya!

  8. #8
    grad2009 is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2010
    Posts
    27
    Thank you RuralGuy, thank you pbaldy. now it works ok.
    Code:
    SELECT Customers.customerNo as No, Customers.customerName as nam, Sales.sInvoiceNo as  INo, Sales.sDate as dat, Sales.total as credit, 0 as debit,[credit] - [debit] as balance
    FROM Customers INNER JOIN Sales ON Customers.customerNo = Sales.customerNo
    
    UNION SELECT Customers.customerNo, Customers.customerName, Payment.paymentNo, Payment.pDate, 0 as credit, Payment.amount as debit, [credit] - [debit] as balance
    FROM Customers INNER JOIN Payment ON Customers.customerNo=Payment.customerNo
    
    UNION SELECT Customers.customerNo, Customers.customerName, Received.receiptNo, Received.rDate, Received.amount as credit, 0 as debit, [credit] - [debit] as balance
    FROM Customers INNER JOIN Received ON Customers.customerNo = Received.customerNo;

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    We were happy to help.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    That's great! Are you ready to follow the link in my sig and mark this thread as Solved?

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

Similar Threads

  1. Replies: 4
    Last Post: 03-05-2010, 09:56 PM
  2. Need help with a Union Query
    By jdowdy in forum Access
    Replies: 1
    Last Post: 10-13-2009, 05:24 PM
  3. Create table out of union query
    By DKruse1969 in forum Queries
    Replies: 2
    Last Post: 08-28-2009, 09:55 AM
  4. Replies: 0
    Last Post: 12-28-2008, 01:56 PM
  5. Calculated Query Field used in different Query
    By cakdhooper in forum Queries
    Replies: 0
    Last Post: 11-21-2008, 03:07 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