VB 6.0:Recordset without a database/table
I tried to search in the Codebank but did not find any thread showing how to create a disconnected recordset without a database. Here is an easy way to create a recordset without an active connection to the any database. Basically when we create a recordset without a database connection we will need to create the fields and then add records to it.
VB Code:
Dim rsTemp As ADODB.Recordset
Set rsTemp = New ADODB.Recordset
rsTemp.ActiveConnection = Nothing
rsTemp.CursorLocation = adUseClient
rsTemp.LockType = adLockBatchOptimistic
'create fields
rsTemp.Fields.Append "ID", adChar, 5
rsTemp.Fields.Append "Name", adVarChar, 50
rsTemp.Open
'add a new record
rsTemp.AddNew
rsTemp.Fields("ID") = "1"
rsTemp.Fields("Name") = "SomeName"
rsTemp.Update
'print the values
Debug.Print rsTemp.Fields(0).Name & " : " & rsTemp.Fields(0).Value
Debug.Print rsTemp.Fields(1).Name & " : " & rsTemp.Fields(1).Value
'print the recordcount
Debug.Print rsTemp.RecordCount
rsTemp.Close
Set rsTemp = Nothing
It is also important that we keep in mind that this recordset is just created in memory and once the recordset is closed or the program terminates the data saved in the recordset will be lost.
We can also save the data to an XML file or a Binary file using the save method of the recordset object.
VB Code:
'save as binary
rsTemp.Save "C:\TemporaryData.dat", adPersistADTG
'save as XML
rsTemp.Save "C:\TemporaryData.xml", adPersistXML