I also want to get the line number with the Select statement by ADODB.

Asked 2 years ago, Updated 2 years ago, 38 views

Excel VBA ADODB retrieves sheet information in SELECT statements.

The original data is as follows.
TableA
Enter a description of the image here

TableB
Enter a description of the image here

And the code is as follows:

Dim myCon As Object
    Dim myRS As Object
    Set myCon=CreateObject("ADODB.Connection")
    Set myRS = CreateObject("ADODB.Recordset")


     With myCon
          .Provider="Microsoft.ACE.OLEDB.12.0"
          .Properties("Extended Properties") = "Excel 12.0; HDR=YES; IMEX=1"
          .Open ThisWorkbook.FullName
     End With
     myRS.Open "SELECT A.ID, A.Name, B.CountryName FROM ([TableA$A1:C10] ASA LEFT JOIN [TableB$A1:B10] ASBON A.CountryID=B.CountryID), myCon, 1, 1
     Dim myVar As Variant
     'myVar=myRS.GetRows
     'myVar=WorksheetFunction.Transpose(myVar)
     Worksheets ("Result").Cells.Clear
     Worksheets("Result").Range("A1").CopyFromRecordset myRS

I'd like to ask you a question here
I would like to display the line number of the original data specified in the FROM clause in the result of the SELECT running.
I would like to have a row number in the column that says TableA row number and TableB row number in the results below.

Please tell me how to do it and how it can be realized in the first place.
You can use ROW_NUMBER() on SQL Server, but what do you do with ADODB on VBA...

"Also, I would like to inform you which line is defective when there is a defect in the content."
I look forward to your kind cooperation.

Enter a description of the image here

vba

2022-09-29 21:57

1 Answers

Why don't you insert a subquery of TABLE A and B in SQL and shake the ID for the line number?
Even DBs that cannot use Row_Number() such as Access can search for samples such as SQL to create subqueries and get line numbers.
However, I think that excessive subqueries will make it less readable.

If I were myself, I would take out all the items in Select*from and combine them with dictionary etc.


2022-09-29 21:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.