
Question: Opening .txt file with vba
Troy,
Here is a little code that reads a file and prints the records in the debug window. I usually start with something like this. You can add the records to a table or do whatever you like from there. There are other options on the fileopen command that you might need to change depending on the format of your file:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Import A Report File in Plain Text & Print 49 lines
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function ImportReportFile(ByVal sFileName As String)
Dim iFileIn As Integer
Dim sTextLine As String, x As Integer, sHdg As String
On Error GoTo Err_ImportReportFile:
iFileIn = FreeFile
Open sFileName For Input As #iFileIn
sHdg = "ROW 1...5...10...15...20...25...30...35...40"
sHdg = sHdg & "...45...50...55...60...65...70...75...80"
sHdg = sHdg & "...85...90...95..100..105..110..115..120"
sHdg = sHdg & "..125..130.."
Debug.Print sHdg
Do While Not EOF(iFileIn)
x = x + 1
Line Input #iFileIn, sTextLine
Debug.Print Format(x, "000") & " " & sTextLine
If x > 49 Then
Exit Do
End If
Loop
Close #iFileIn
ImportReportFile = True
Exit_ImportReportFile:
Exit Function
Err_ImportReportFile:
MsgBox Error$
Resume Exit_ImportReportFile:
End Function
Bill Serrahn
http://home.att.net/~Bill.Serrahn/
Quote:
>I just realized today that with all the Access development I've done,
>I've never opened a text file with vba. This is the problem:
>I have a fixed-width text file which resides on a server (S: drive) that
>I need to get into Access on a daily basis (phone records pulled from a
>UNIX system using ProComm). I have a module that imports all of my other
>data (Paradox and Access tables) every morning. I need to add this text
>file to my list but can't seem to get it to work. Either linking the
>file or simply opening it as a recordset will work as it is only about
>50 records. I didn't think about posting to this newsgroup until a few
>minutes ago and therefore don't have a copy of the code I tried. Any
>help would be greatly appreciated.
>Thanks,
>Troy