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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Importing Text file

Status
Not open for further replies.

fergy1

IS-IT--Management
Jul 25, 2003
32
US
Hello-

I have a tab delimited text file users will download and by pushing the import list button will get a Browse function which allows them to find the location of that text file. However, I'm not sure how to take the text file and then import it into my database. I have column headers on the text file. I created the Import/export wizard Object but not sure how to access that object once I've chosen my text file location in VB? Any help is appreciated.

Private Sub ImportList_Click()
On Error GoTo Finish

' Select Folder
' See the 'BrowseFolder' function in module 'BrowseForFolder'

Dim strFolderName As String
Dim strCaption As String
' Open the text file

strCaption = "Select folder to load Listings from"
strFolderName = BrowseFolder(strCaption)

If Not IsEmpty(strFolderName) Then
strSearchSpec = strFolderName + "\" + "*.txt"
strFile = Dir(strSearchSpec, vbNormal)



End If

Finish:

End Sub
 
Check out VB help under "file system object" and "text stream object".....
 
I can give you a another method that uses ADO but this method uses the Access object. The only bad thing about that is that you would need Access installed on the target machine:

Private Sub Command1_Click()
' Add a reference to Microsoft Access X.X Object Library
Dim AccApp As Access.Application
Dim ImpSpec As String
Dim ImpTbl As String
Dim ImpFile As String

Set AccApp = New Access.Application
AccApp.OpenCurrentDatabase "C:\UsersDB.mdb", False
ImpSpec = "Testit Import Specification"
ImpTbl = "Testit"
ImpFile = "C:\Testit.txt"

DoCmd.TransferText acImportDelim, ImpSpec, ImpTbl, ImpFile, True

MsgBox "Import Successfully Completed!"
AccApp.CloseCurrentDatabase
Set AccApp = Nothing
End Sub

Swi
 
The only problem with that method is it gives a specific method and location. I want the user to be able to import the text file without me hard coding it in. Access already has a import text feature in it's macros to do what you gave me, which is nice, but not quite what i need.
 
Why don't you just use the Microsoft Common Dialog Control to let the user browse for the file and the use the CommonDialog1.FileName to populate the ImpFile variable in my example?

Swi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top