Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Import text file into Access table 1

Status
Not open for further replies.

rockfish12

Technical User
Feb 12, 2002
31
US
I've read thru some other tek-tips.com posts on how to programmatically import text files into a table in Access, and I came across some code (below) that I'm trying to make work:
Code:
----------------------------------------
Sub ImportTarFiles(strFileName As String)

Dim dbs As Database
Dim rst As Recordset

Set dbs = CurrentDb
Set rst = dbs.TableDefs("tblTAR").OpenRecordset

With rst
    Open strFileName For Input As #1
    Do While Not EOF(1)
        .AddNew
        Line Input #1, rst("filepath")
        .Update
    Wend
    Close #1
End With

rst.Close

End Sub
----------------------------------------
When trying to run it, I experience an error on the "Line Input #1" line - the compiler tells me "variable required - can't assign to this expression."

The end goal here is to take a text file, read in each line, and assign it to a record...in effect, one line equals one record.

Is this code the right way to go about doing this? If so, what's wrong with my syntax? Or should I try something altogether different?

 
RockFish,

I guess the table is just a collection of filepaths, but
here goes:


Sub ImportTarFiles(strFileName As String)

Dim dbs As Database
Dim rst As Recordset
Dim strText As String

Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("tblTAR")

Open strFileName For Input As #1
Line Input #1, strText
Do While Not EOF(1)
rst.Edit ' Unsure if needed
rst.AddNew
rst!filepath = strText
rst.Update
Line Input #1, strText
Wend
Close #1
set rst = Nothing

End Sub

hth,
Wayne

 
Thanks, Wayne. You're exactly right - the text file is just a bunch of filenames (with full paths) that I want to read into that table, except for a few header lines at the top.

Anyways, that fixed the "variable required" problem, but now I'm getting a "type mismatch" error on the "Set rst = dbs.OpenRecordset("tblTAR")" line. Any ideas?

 
Wait, I may have figured it out - it I had to uncheck the ActiveX 2.5 library. Your solution worked great. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top