Dataset, virtual or in memory table in Access 2007
Is there a way to load data (e.g. from XML) to a dataset - a temporary memory table/recordset which can be linked (bound) to a form for a preview? I know that .NET has datasets, but how about Access 2007?
It is possible to fill a menu/list control with the data from a XML file, but i开发者_运维知识库t would not scale as well as a continuous form would.
No, Access doesn't know datasets.
But you could convert the dataset to an ADODB.Recordset
. Access knows that and you can bind it directly to a continuous form.
Here is a snippet to convert datasets to ADODB.Recordsets (and the other way round):
http://dotnet-snippets.de/dns/ado-recordset-converter-SID525.aspx
The page and the comments in the code are in German, but even if you don't understand German, the code itself is easy to use.
You just need a reference to Microsoft ActiveX Data Objects 2.8
, and then you just need to call this method:
public static ADODB.Recordset ConvertDataTableToRecordset(DataTable table)
Access forms starting with A2000 allow the assignment of a recordset assembled in code to a form's recordset, and that includes ADO recordsets. So, if you opened an ADO recordset from your XML file, you should then be able to assign it to a form.
However, note that this is not something I have ever done as I haven't needed it, and I know that there are issues with ADO vs. DAO recordsets and all sorts of little incompatibilities that people encounter when they do this kind of thing.
Here is a solution based on this example: Create In-Memory ADO Recordsets - remember to add first a reference in Tools > References, find the latest Microsoft ActiveX Data Object
Public Sub createDataSet(mekeNewSet As Boolean)
'requires reference addedd **Microsoft ActiveX Data Object**
Dim rs As ADODB.Recordset
Dim fld As ADODB.Field
Set rs = New ADODB.Recordset
With rs
.Fields.Append "EmployeeID", adInteger, , adFldKeyColumn
.Fields.Append "FirstName", adVarChar, 10, adFldMayBeNull
.Fields.Append "LastName", adVarChar, 20, adFldMayBeNull
.Fields.Append "Email", adVarChar, 64, adFldMayBeNull
.Fields.Append "Include", adInteger, , adFldMayBeNull
.Fields.Append "Selected", adBoolean, , adFldMayBeNull
.CursorType = adOpenKeyset
.CursorLocation = adUseClient
.LockType = adLockPessimistic
.Open
End With
With rs
.AddNew
.Fields(0) = 777
.Fields(1) = "John"
.Fields(2) = "Smith"
.Fields(3) = "john.smith@something.com"
.Fields(4) = 1
.Fields(5) = True
.Update
End With
Set Me.Recordset = rs
End Sub
There is also a possibility to create a new Class in VBA and then build a Collection as in this tutorial: Using Custom Collections in Microsoft Access
For the full solution how to download XML data and enter it into a recordser check here: Access VBA - how to download XML file and enter its data into a recordset
精彩评论