Access.2007.VBA.Bibl..

(John Hannent) #1
Write data to text box on form.

Me![txtImportedText].Value = strData

ErrorHandlerExit:
Exit Sub

ErrorHandler:
MsgBox “Error No: “ & Err.Number _
& “; Description: “ & Err.Description
Resume ErrorHandlerExit

End Sub

The next sections describe the specific techniques used in the code for the ADO, FSO, and VB
methods for loading data from a selected text file.

ADO ..............................................................................................................

For the ADO option, a Stream variable is set, using the Newkeyword, the Streamobject is
opened, and the selected text file is loaded, using the LoadFromFilemethod. The ReadText
method is used with the adReadAllnamed constant as its argument to read in all the text from
the text file, and that data is then written to the strDatavariable. Finally, the Streamobject
is closed.

FSO ..............................................................................................................

For the FSO (FileSystemObject) option, first a variable is set to the FileSystemObject,
and then the text file is opened with the OpenTextFilemethod of the FileSystemObject,
with the ForReadingvalue for the IOModeargument. All of the data from the text file is
read using the ReadAllmethod, and it is saved to the strDatavariable. Finally, the text file
is closed.

VB ................................................................................................................

The last method, VB, uses the Open strTextFileForInput As #2statement to open the file,
and sets up a Do While... Loopstructure to process all of the lines in the text file, saving data
from each line to the strDatavariable, incrementing it line by line with a vbCrLfconstant in
between lines. When all the lines have been processed, the text file is closed.

Finally, however the data has been accumulated, the strDatavariable is written to the
txtImportedText textbox, as shown in Figure 9.15.

Part II Writing VBA Code to Exchange Data between Office Components

Free download pdf