GetRows Function and Exporting Data
We are going to create a useful data export utility with the GetRows() method of the Recordset Object. The GetRows method is used for loading the entire set of data from table into memory in a doubly dimensioned Variant Array with a single statement. Let us try it with an example to see how this is done? An image of a sample table is given below and we will see how this will look like in memory when loaded with the help of GetRows() method of the Recordset Object:
The following sample VBA Routine loads the above data into memory and a listing is dumped in the Debug Window:
Public Function Test(ByVal tblName As String) Dim db As Database, rst As Recordset, varData As Variant Dim intFields As Integer, intRecords As Integer, j As Integer, k As Integer Dim rec As String, fld_type As Integer Set db = CurrentDb Set rst = db.OpenRecordset(tblName, dbOpenTable) j = rst.RecordCount - 1 k = rst.Fields.Count - 1 varData = rst.GetRows(j + 1) For intRecords = 0 To j rec = "" For intFields = 0 To k fld_type = rst.Fields(intFields).Type If fld_type = 11 Or fld_type = 12 Then GoTo nextField End If rec = rec & varData(intFields, intRecords) & "," nextField: Next rec = Left(rec, Len(rec) - 1) Debug.Print rec Next rst.Close Set rst = Nothing Set db = Nothing End Function
The arrangement of records in memory in a two dimensional array looks like the following:
The records are not arranged in row order as they appear in datasheet view, instead they are loaded in columns. So, after loading the data in memory we must know how to address the two dimensional array to access each record in correct order to output the data. Each field value is separated with a comma in the listing provided in the Debug window.
Normally, in a two dimensional Array the first index value of the array is the row number and the second one is column number. But, in this case the first index value is the Field order Number and the second value is the Record Number. You can check the sample data arrangement in memory shown above.
We will write a small utility program to export any MS-Access Table into comma delimited Text/CSV File, so that the data can be easily transported through internet or to import into other Applications.
The VBA Code of the program is given below:
Public Function CreateDelimited(ByVal xtableName As String, ByVal txtFilePath As String) '----------------------------------------------------- 'Utility: CreateDelimited() 'Author : a.p.r.pillai 'Date : Dec. 2010 'Purpose: Create Comma Delimited Text File from Table 'Rights : All Rights Reserved by www.msaccesstips.com '----------------------------------------------------- Dim db As Database, rst As Recordset Dim varTable() As Variant, j As Long, k As Long Dim rec As String, fld_type As Integer Dim intRecords As Integer, intFields As Integer Set db = CurrentDb Set rst = db.OpenRecordset(xtableName, dbOpenTable) varTable = rst.GetRows(rst.RecordCount) k = rst.Fields.Count - 1 j = rst.RecordCount - 1 Open txtFilePath For Output As #1 rec = "" For intFields = 0 To k fld_type = rst.Fields(intFields).Type If fld_type = 11 Or fld_type = 12 Then GoTo nextField rec = rec & Chr$(34) & rst.Fields(intFields).Name & Chr$(34) & "," nextField: Next rec = Left(rec, Len(rec) - 1) Print #1, rec For intRecords = 0 To j rec = "" For intFields = 0 To k fld_type = rst.Fields(intFields).Type If fld_type = 11 Or fld_type = 12 Then GoTo Next_Field rec = rec & IIf(fld_type = 10, Chr$(34) & varTable(intFields, intRecords) & Chr$(34), varTable(intFields, intRecords)) & "," Next_Field: Next: rec = Left(rec, Len(rec) - 1) Print #1, rec Next Close #1 rst.Close Set rst = Nothing Set db = Nothing End Function
The Utility can be called from a Command Button Click Eventprocedure after setting the Table name and the target file pathname in text boxes. You can test the utility by calling it from the Debug Window (Immediate Window) directly as given below:
CreateDelimited "Products", "C:\Temp\Products.txt"
NB: If MEMO or Photo Fields are present in the Table they are excluded from the output file.
The target file extension can be either .TXT or .CSV.
Menus with Option Group Control