Excel VBA ADODB retrieves sheet information in SELECT statements.
The original data is as follows.
TableA
TableB
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.
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.
© 2024 OneMinuteCode. All rights reserved.