Loading Text File to TextBox using LoadFromFile

C

Chris H

Hi All,

I'm creating a form that allows the user to pick a txt file (dialog) and
then display the path and contents on the form. The code has been cobbled
together as I found the pieces that worked, so bear with. I got the file
picker working and displaying the file name on the form, but the file
contents won't display. I had a feeling the problem had to do with importing
a namespace (see the error in the code when I tried "Imports System.IO") or
with a missing reference.

Using Access 2003.
References: VB for Apps, MS Access 11 Obj Lib, OLE Auto, MS VB for Apps
Exen 5.3, MS VBScript Regular Exps 1.0/5.5, MS DAO 3.6 Obj Lib, MS ActiveX
Data Objs 2.8 Lib, MS Office 12.0 Obj Lib.

Once this part is working, I want to add a second button that will
"transform" and save the file, but need this to work first.

Code (see errors I get under the various tries of LoadFromFile):

Option Compare Database
Option Explicit

'Imports System -> Invalid Outside procedure if not remmed
'Imports System.IO -> same

Private Sub cmdFileDialog_Click()

' This requires a reference to the Microsoft Office 11.0 Object Library.

Dim fDialog As Office.FileDialog
Dim varFile As Variant

' Clear the list box contents.
Me.FileList.RowSource = ""

' Set up the File dialog box.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
' Allow the user to make multiple selections in the dialog box (not).
.AllowMultiSelect = False

' Set the title of the dialog box.
.Title = "Select a Text file to Convert"

' Clear out the current filters, and then add your own.
.Filters.Clear
.Filters.Add "Text Files", "*.TXT"
'.Filters.Add "Access Projects", "*.ADP"
'.Filters.Add "All Files", "*.*"

' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.
If .Show = True Then
' Loop through each file that is selected and then add it to the
list box.
For Each varFile In .SelectedItems
Me.FileList.AddItem varFile
Next
Text6.SetFocus
Dim s As Stream
Set s = New Stream
'Text6.Text = s.LoadFromFile("c:\varFile.txt")
'-> Expected Function or Variable error
'Set Text6.Text = LoadFromFile(varFile)
'-> Sub or Function not Defined error
'Text6.LoadFromFile (varFile)
'-> Method or Data Member not Found error
Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With
End Sub
 
D

Dirk Goldgar

Chris H said:
Hi All,

I'm creating a form that allows the user to pick a txt file (dialog) and
then display the path and contents on the form. The code has been cobbled
together as I found the pieces that worked, so bear with. I got the file
picker working and displaying the file name on the form, but the file
contents won't display. I had a feeling the problem had to do with
importing
a namespace (see the error in the code when I tried "Imports System.IO")
or
with a missing reference.

Using Access 2003.
References: VB for Apps, MS Access 11 Obj Lib, OLE Auto, MS VB for Apps
Exen 5.3, MS VBScript Regular Exps 1.0/5.5, MS DAO 3.6 Obj Lib, MS ActiveX
Data Objs 2.8 Lib, MS Office 12.0 Obj Lib.

Once this part is working, I want to add a second button that will
"transform" and save the file, but need this to work first.

Code (see errors I get under the various tries of LoadFromFile):

Option Compare Database
Option Explicit

'Imports System -> Invalid Outside procedure if not remmed
'Imports System.IO -> same

Private Sub cmdFileDialog_Click()

' This requires a reference to the Microsoft Office 11.0 Object Library.

Dim fDialog As Office.FileDialog
Dim varFile As Variant

' Clear the list box contents.
Me.FileList.RowSource = ""

' Set up the File dialog box.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
' Allow the user to make multiple selections in the dialog box (not).
.AllowMultiSelect = False

' Set the title of the dialog box.
.Title = "Select a Text file to Convert"

' Clear out the current filters, and then add your own.
.Filters.Clear
.Filters.Add "Text Files", "*.TXT"
'.Filters.Add "Access Projects", "*.ADP"
'.Filters.Add "All Files", "*.*"

' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.
If .Show = True Then
' Loop through each file that is selected and then add it to the
list box.
For Each varFile In .SelectedItems
Me.FileList.AddItem varFile
Next
Text6.SetFocus
Dim s As Stream
Set s = New Stream
'Text6.Text = s.LoadFromFile("c:\varFile.txt")
'-> Expected Function or Variable error
'Set Text6.Text = LoadFromFile(varFile)
'-> Sub or Function not Defined error
'Text6.LoadFromFile (varFile)
'-> Method or Data Member not Found error
Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With
End Sub


You can do it with a Stream object, but reading the contents of a text file
can be done more simply than that, using the basic I/O functions that are
built into VBA. Here's how I would do it:

Dim intFile As Integer

intFile = FreeFile()
Open ""c:\varFile.txt"" For Input As intFile
Me.Text6 = Input(LOF(intFile), intFile)
Close intFile

Note that there is no need to set the focus to Text6, so long as you don't
use its .Text property but instead assign the value to the control's .Value
property (which is its default property and so need not be explicitly
named).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top