I got a problem with the Recordset I'm using in my Excel VBA Project. I'm trying to read a certain range into the Recordset and til now it always worked. But this time around I want to get a incoherent range, which in my case consists of columns of a table, and give this range to the recordset through the following function (got it from @Martin Dreher)
'this is how I set the range
Set SourceRange = ThisWorkbook.Worksheets(1).Range("Tablename[[field1]:[field2]], " & _
"Tablename[[field3]:[field4]], Tablename[[field5]:[field6]]")
'this is the function I set my recordset with
Function GetRecordset(rng As Range)
Dim xlXML As Object
Dim rst As Object
Set rst = CreateObject("ADODB.Recordset")
Set xlXML = CreateObject("MSXML2.DOMDocument")
xlXML.LoadXML Replace(rng.Value(xlRangeValueMSPersistXML), "rs:name="" ", "rs:name=""")
rst.Open xlXML
Set GetRecordset = rst
End Function
When I select the range via VBA and it is the one I want but when calling getRecordset and afterwords copy the data out of the recordset all I get is the data of the first range.
I edited the names of the variables since there is some internal data involved.
Thanks already for your help!
Glad you found it useful. Please make sure to mark helpful answers and close open threads, if you consider the problem solved.
On topic:
Add Debug.Print rng.value(xlRangeValueMSPersistXML)
inside your function for debugging. You'll see that the XML only contains your first range, even if you union your ranges with, well, Union
first.
Afaik there's no fix for this. Try to fix with continuous ranges.
However, if you absolutely need two ranges in one recordset, and you can guarantee they have both the same number of rows and that each Row(i)
in A
belongs to Row(i)
in B
, then a workaround would be to
With ThisWorkbook.Worksheets.Add
Union
-range-values to that sheet in a continuous range starting at .Cells(1,1)
.UsedRange
.Delete
the worksheet with no prompts End with
Put this into your GetRecordset
-Function, since, at the same time, it solves the problem you had with two header rows. If performance is an issue, consider passing an additional Boolean
to your function to determine if your function uses the workaround or not.
Hope that helps.
Edit: We cannot just delete an unused Field
from an open recordset. As you might expect, every workaround is a bit dirty.
Since I would have to test performance, I can only give you some ideas for testing.
with the use of Worksheets.Add
mentioned above, a slower performance is to be expected if you do it repeatedly.
However, instead of creating a Worksheet
in each loop, you could
Worksheet
once,.UsedRange.Cells.Clear
after each iteration .Delete
once at the end. if you use .CopyFromRecordset
, and start with blank output-sheets, it's likely faster to just
Cells
in Rows(1)
of your Ouput.EntireColumn.Delete
I would give 1. a try.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.