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 7611533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:41:13+00:00 2026-05-31T01:41:13+00:00

Doubt in vba ADO and Sql query… I have 2 sheets Namely adodc1,adodc2 (In

  • 0

Doubt in vba ADO and Sql query…

I have 2 sheets Namely adodc1,adodc2 (In one workbook)

In adodc1 has columns “Name”,”Dept” and some times its have “Sect” column

In adodc2 has”Name”,”Dept”,”sect” columns

what I want is when i run Query..Vba needs to check whether adodc1 have the Sect column or not ..if it have union the two sheets has usual else

want to return as empty value ..

The below Code taken from ” “altered as per my needs

What it will do is union name and Dept column from two sheets ..now i want query to check whether adodc1 have the Column “sect” or not ..if it has

union “Sect” as usual else .. union as empty value

Sub connecttoexcel()

Dim cn As Object
Dim rs As Object
Dim strFile As String
Dim strCon As String
Dim strSQL As String
Dim s As String
Dim i As Integer, j As Integer

strFile = ActiveWorkbook.FullName
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile & ";Extended Properties=""Excel 12.0 XML;HDR=Yes;IMEX=1"";"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open strCon
'here i want some stuff
strSQL = "Select Name, Dept from [Adodc1$] Union Select Name, Dept from [Adodc2$];"
rs.Open strSQL, cn
Worksheets("Sheet3").Cells(2, 1).CopyFromRecordset rs
Set rs = Nothing
End Sub
  • 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-05-31T01:41:14+00:00Added an answer on May 31, 2026 at 1:41 am

    Vba needs to check whether adodc1 have the Sect column or not

    I suggest you use the OpenSchema method of the Connection object to discover whether the column exists e.g. something like:

    Set rs = cn.OpenSchema(adSchemaColumns, Array(Empty, Empty, "Adodc1$")
    rs.Filter = "COLUMN_NAME = 'Sect'"
    If rs.RecordCount = 1 Then
       ' Column exists
       ...
    

    when it comes to check for 50 columns it will be more difficult i think …

    rs.Filter = "COLUMN_NAME = 'Sect' OR COLUMN_NAME = 'Name' OR COLUMN_NAME = 'Dept' ...
    

    or test each one in a loop using an array etc.

    Is it possible to use NZ Function inside of the sql query

    The NZ() function is not a function of Access SQL, which is what is being used here to access Excel data. Rather, it part of the MS Access object model. In short, unless you are running this from an Access VBA project then NZ() is not available. But the workaround is trivial e.g.

    Nz(Dept, '{{NONE}}')
    

    has the same effect as

    IIF(Dept IS NULL, '{{NONE}}', Dept)
    

    I’ve read Access MVPs (Allen Browne?) say this is preferable to Nz() anyhow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I doubt I am the only one who has come up with this solution,
I have one doubt. I need to write site on embedded system ( memory
One doubt in MSSQL. There are two tables in a databases. Table 1 named
No doubt elements of this question have been asked before, but I'm having trouble
my doubt is the following, I have a network with 5 pcs, each machine
I have a doubt with enums if I defined a enums that inherited from
I have little doubt about adapter class. I know what's the goal of adapter
I have a little doubt, I want to create a table that had a
I have a doubt a long time and never find the answer, so I
I have a doubt of good manners in Rails and I believe MVC in

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.