Introduction.
Converting the contents of an Access table into a comma-delimited text file (CSV format) makes it easier to transport data over the Internet. Such files can be uploaded back into an Access table or imported into virtually any other database system at the receiving end.
We have already explored the data exporting procedure in an earlier example. You can refer to that discussion, which also demonstrates the use of Access’s GetRows() function. This function efficiently copies the entire table contents into a two-dimensional array in memory with a single operation, making it an excellent tool for preparing data for export.
Microsoft Access already has built-in data Import and Export options.
Here, we will learn how to read a Text File containing the names of Employees, each separated by a comma, and write them into a new Access table. An Image of a sample text file is given below:
An Image of the output Access Table, with a single column, is given below for reference.
First, create a text file using Notepad or any other text editor, and enter some sample data. Each line should contain a few names (people or products) separated by commas. Place each set of items on a new line, as shown in the earlier example.
⚠️ Important: Do not put a comma at the end of any line.
Save the Text file with the name: Names.txt, in your database folder, or in any folder you like.
Open your Access Database to try out the Program given below.
Open the VBA Editing Window and insert a new Standard Module.
Copy and paste the following VBA Code into the Module and save it:
Creating a Table From Comma-Separated Text File.
Public Function NamesList() '----------------------------------------------------- 'Utility: Creating Access Table from ' : comma separated text data. 'Author : a.p.r.pillai 'Date : April 2016 'Rights : All Rights Reserved by www.msaccesstips.com '----------------------------------------------------- Dim db As Database, rst As Recordset, tdef As TableDef Dim strH As String, fld As Field, j As Integer Dim x_Names As Variant, tblName As String, fldName As String Dim txtfile As String On Error GoTo NamesList_err tblName = "NamesList" fldName = "Names" txtfile = "d:\mdbs\Names.txt" 'Change this line to correctly point where your text file is saved. 'create the NamesList table with a single field: Names Set db = CurrentDb Set tdef = db.CreateTableDef(tblName) With tdef .Fields.Append .CreateField(fldName, dbtext, 50) End With db.TableDefs.Append tdef db.TableDefs.Refresh 'Open the NamesList table to write names with the text file Set rst = db.OpenRecordset(tblName) 'Open the Names.txt file to read text data Open txtfile For Input As #1 'setup a loop to read the data till the end-of-file reached Do While Not EOF(1) 'read the first line of names, separated with commas and 'terminated with carriage return (Enter key),into String variable strH Line Input #1, strH 'extract each name separated with comma and load into the Array variable x_Names x_Names = Split(strH, ",") 'Read each name from array elements 'and write into the NamesList table With rst For j = 0 To UBound(x_Names) .AddNew !Names = x_Names(j) .Update Next End With 'Repeat till the End-Of-Text File is reached Loop close #1 NamesList_Exit: rst.Close db.Close Set rst = Nothing Set db = Nothing Exit Function NamesList_err: If Err = 3010 Then 'Table already exists 'continue executing from the next line onwards Resume Next Else MsgBox Err & ": " & Err.Description, "NamesList()" Resume NamesList_Exit End If End Function
Find this line in the vba code: txtfile = "d:\mdbs\Names.txt" and make the change in the correct location of your text file. Click somewhere in the middle of the Code and press the F5 Key to run the Code.
Note: If everything went well, then you will find the Table Names list in the Navigation Pane. If you could not find it, then right-click on the top bar of the Navigation Pane and select Search Bar. Type NamesList in the Search Bar to bring up the table in view. Click on it to open and view the data.
How This Works.
When the program runs for the first time, it creates the NamesList Table, with a single field: Names. Subsequent runs of the program ignore the table creation action and simply append the data to the existing table from the text file.
The program reads the first text line (for example, three names separated by commas) into the string variable
strH
.We use the
Line Input
statement here instead of theInput
statement.-
Line Input
reads the entire line of text, stopping only when it encounters a carriage return (the Enter key at the end of the line). -
Input
, on the other hand, treats the comma as a delimiter. So, it would only read the first name and stop at the comma, ignoring the rest of the line.
-
The Split() Function will break up the names separately and load them into the Variant Array Variable: x_Names.
The Array variable x_Names will be automatically dimensioned/re-dimensioned by the Split() Function, for the number of items on the input line, and each item is loaded into the elements of the array.
In the next step, a new record is added to the Access Table for each item loaded into the array and written to the table from the array elements.
This process is repeated for all the lines in the text file. When the end of the text file is reached, all files are closed, and the function stops.
Next week, we will learn how to work with text files that have several fields of data in a record.
No comments:
Post a Comment
Comments subject to moderation before publishing.