Access.2007.VBA.Bibl..

(John Hannent) #1
The cmdLoadData_Clickevent procedure that reads data from a text file is simpler than the
code that writes data, because two of the three methods (in their own way) can import all the data
from a text file without needing to process each line separately. The cmdLoadData_Clickproce-
dure starts by setting a reference to the textbox that holds the name of the selected text file, and
checks that there is a file name in the box. If there is a file name, it is saved to a variable, and then
a Select Casestatement is set up to process the import separately depending on whether the
user selects ADO, FSO, or VB in an option group:

Private Sub cmdLoadData_Click()

On Error GoTo ErrorHandler

Dim fso As Scripting.FileSystemObject
Dim strText As String
Dim strFile As String
Dim intTextType As Integer
Dim strTitle As String
Dim strPrompt As String
Dim txt As Scripting.TextStream
Dim stm As ADODB.Stream
Dim txtData As Access.TextBox
Dim strTextFile As String
Dim strData As String
Dim strLine As String

Set txtData = Me![txtSelectedTextFile]
strTextFile = Nz(txtData.Value)
If strTextFile = “” Then
strTitle = “No text file selected”
strPrompt = “Please select a text file”
MsgBox prompt:=strPrompt, Buttons:=vbExclamation _
+ vbOKOnly, title:=strTitle
GoTo ErrorHandlerExit
Else
Debug.Print “Text file: “ & strTextFile
End If

intTextType = Nz(Me![fraTextType].Value, 2)

Select Case intTextType

Case 1

ADO

Set stm = New ADODB.Stream

With stm
.Charset = “ASCII”

Part II Writing VBA Code to Exchange Data between Office Components

Free download pdf