Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8279691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:24:39+00:00 2026-06-08T09:24:39+00:00

I am using VBA to generate SQL queries in Access. I have two SQL

  • 0

I am using VBA to generate SQL queries in Access. I have two SQL queries that return two lists of items with a status column that shows Required or Optional:

"SELECT *, 'Required' as status FROM tblMain WHERE " & where_1
"SELECT *, 'Optional' as status FROM tblMain WHERE " & where_2

*where_1* and *where_2* are string variables I build within the VBA–it’s not really important what they are though.

What I’d like to do is have one combined list, and if an item shows in both lists, I’d like that item to show just once with a “Required” status (“Required” trumps “Optional”). I started with a union query, but I’m not sure how to eliminate the duplicate rows with the “Optional” statuses.

SELECT * FROM
((SELECT *, 'Required' as status FROM tblMain WHERE where_1)
UNION
(SELECT *, 'Optional' as status FROM tblMain WHERE where_2))

I’m thinking a DISTINCT or possibly a FIRST could be used, but unfortunately I’m having trouble working out the syntax.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T09:24:40+00:00Added an answer on June 8, 2026 at 9:24 am

    I suggest you eliminate IDs selected in the first statement from the second statement.

    SELECT *, 'Required' as status FROM tblMain WHERE where_1
    UNION
    SELECT *, 'Optional' as status FROM tblMain WHERE where_2
    AND ID Not IN (SELECT ID FROM tblMain WHERE where_1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using VBA i have a set of functions that return an ADODB.Recordset where all
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I am looking to run SQL queries using VBA code in an Excel file.
I have an SQL database and want to connect to it using VBA in
In an Access 2003 database, I have an employee status/feedback report that is generated
I'm using Access 2003 and have a form that allows the user to pick
I am using VBA to paste charts/graphs from Access to PowerPoint. Once pasted, I
I'm trying to access a query using VBA code. I think I've done it
I have an Excel workbook that is used as a starting point to generate
I would like to know if using VBA I can compare two similar excel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.