Create query from 2 tables (1 Viewer)

Mr_P

Registered User.
Local time
Today, 09:04
Joined
Nov 27, 2013
Messages
18
I need to get the MAX id from two tables for use in vb.net. For example:

Code:
Table1                 Table2
id, customer           id, customer
How do I write a query that will return the last or MAX id from both tables based on a customer criteria. As a new user to access and vb.net any guidance would be an immense help Many thanks
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 02:04
Joined
Jan 20, 2009
Messages
12,852
Open a recordset against an Aggregate query with a Union subquery.

SELECT Max(id) AS MaxID
FROM
(SELECT id FROM Table1
UNION ALL
SELECT id FROM Table2)
AS A
 

Mr_P

Registered User.
Local time
Today, 09:04
Joined
Nov 27, 2013
Messages
18
@Galaxiom

Thank you for reply. However, it is only returning the value from the first table. Where have i gone wrong. Thanks

Code:
SELECT Max([Request no]) AS MaxID
FROM
(SELECT [Request no] FROM Requests
UNION ALL
SELECT [Request no] FROM [Request Boxes])
AS A
WHERE Customer = "DEMO";
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 02:04
Joined
Jan 20, 2009
Messages
12,852
I misunderstood your requirement.
Try this one:

Code:
SELECT RequestID, RequestBoxID
FROM
   (
    SELECT Max([Request no]) AS RequestID
    FROM Requests 
    WHERE Customer = "DEMO"
    ) AS A
,
    (
     SELECT Max[Request no] As RequestBoxID
     FROM [Request Boxes])
     WHERE Customer = "DEMO"
     ) AS B
;
 

Mr_P

Registered User.
Local time
Today, 09:04
Joined
Nov 27, 2013
Messages
18
Hi that creates an error of which I have attached screenshot. Thanks
 

Attachments

  • access-error.png
    access-error.png
    14.3 KB · Views: 70

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 02:04
Joined
Jan 20, 2009
Messages
12,852
Sorry. An extra closing bracket crept in somehow in this line:
FROM [Request Boxes])
 

Users who are viewing this thread

Top Bottom