Introduction
We will create a useful data export utility with the GetRows() method of the Recordset Object. The GetRows method is used to load the entire set of data from the table into memory as 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 what this will look like in memory when loaded with the help of the GetRows() method of the Recordset Object:
ID | Name | Birthdate | Height | Weight |
1 | Nancy | 12/10/1980 | 164 | 58 |
2 | Peter | 05/07/1975 | 180 | 80 |
3 | Linda | 17/11/1982 | 170 | 60 |
The VBA Code.
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:
1 | 2 | 3 |
Nancy | Peter | Linda |
12/10/1980 | 05/07/1975 | 17/11/1982 |
164 | 180 | 170 |
58 | 80 | 60 |
The Memory Image of the Data.
The records are not stored in the same row-wise order as they appear in Datasheet view; instead, they are loaded into memory in columns. Once the data is in memory, it is important to understand how to reference the two-dimensional array correctly to access and output each record in the proper order. In the Debug window, you can see that each field value is separated by a comma in the listing.
In a typical two-dimensional array, the first index represents the row number, and the second index represents the column number. However, in this case, the structure is reversed: the first index corresponds to the field order number, and the second index represents the record number. You can confirm this by examining the sample memory arrangement shown above.
Export Data in Text Format
We will write a small utility program to export any MS-Access Table into a comma-delimited Text/CSV File, so that the data can be easily transported through the internet or imported 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 Event procedure 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.
[...] security, Internet, dynamic queries/reports, Mail-merge. MsgBox with Office Assistant. … Read the rest of content…… stLight.options({publisher:'af88d10d-4ec4-486e-8284-c263218fb2dd'}); RELATED [...]
ReplyDelete