Question: Opening .txt file with vba 
Author Message
 Question: Opening .txt file with vba

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



Sun, 03 Sep 2000 03:00:00 GMT  
 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



Sun, 03 Sep 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Opening a File Open window for file selection in VBA

2. Adding .TXT files to a VB program without the .TXT file

3. Question about writing a string to a text file (file.txt)

4. Reading txt files in VBA

5. show a .txt file from VBA on excel

6. Help on using TXT files with VBA

7. Using VBA Automation to Open Word at the File Open Locatiion

8. open txt file and read strings

9. opening a .txt file in ASP

10. Opening .txt file in BASIC

11. open a txt file for the user to view and read

12. Opening a txt file in Excel using VB6

 

 
Powered by phpBB® Forum Software