Importing 70 Text Files

jrncrank

New Member
Joined
Oct 20, 2003
Messages
14
Hi all,

I'm working with Acces 97 and I'm having trouble with the code to import TXT Files.

I have 2 part question:

Is there an easier way to import 70 txt files into access without having to use the transfertext file action in the marco setup? eg: my marco in 70 lines long - to import each txt file.

Is there any code out there that alllows me to import all txt file within a particular folder.

The 2nd part of my question is:

My txt files names change on a monthly basis..... eg. txt1Nov03 to txt1Dec03 - txt2Nov03 to txt2Dec03 and so on.....

Can the marco in the first question be changed to import all txt file that contain ......Nov03 or .....Dec03?

:rolleyes:
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi,

This code can be used to import all files in a folder using a predefined import specification:-

Code:
Sub ImportAllFilesInFolder()
    Dim strFolderName As String
    Dim oFSObj As Object, oFSFolder As Object, oFSFile As Object


    strFolderName = "H:\temp\text files"

    'Create an instance of the MS Scripting Runtime - we can use
    'this to easily work with files and folders
    Set oFSObj = CreateObject("Scripting.FileSystemObject")


    Set oFSFolder = oFSObj.getfolder(strFolderName)

    For Each oFSFile In oFSFolder.files

        DoCmd.TransferText acImportDelim, "MyImportSpec", "MyTable", oFSFile.Path, True

    Next oFSFile


    Set oFSFile = Nothing
    Set oFSFolder = Nothing
    Set oFSObj = Nothing

End Sub

You'll need to change 3 things in the code:-

<ul>[*]The folder that strFolderName is pointing to.[*]The import spec name in the DoCmd.TransferText line.[*]The table name in the DoCmd.TransferText line.[/list]

If you want to specify certain files then you can just include an If statement to check if the filename contains the text 'Nov03' or whatever e.g.

Code:
......

        If oFSFile.Name Like "*Nov03*" Then

            DoCmd.TransferText acImportDelim, "MyImportSpec", "MyTable", oFSFile.Path, True

        End If

......

Hope that helps you out mate. Let me know how you get on :)
 
Upvote 0
:biggrin: Sensational works a treat.....

Thanks very much for the help, your gonna make me look like a right clever ****.

But hey you beat us in the rugby!!!

cheers again!!!

from a wounded wallaby :LOL:
 
Upvote 0
Just incase anybody else is interested.

To make it easier for the individuals using my database, I created a couple of new varibles to trigger input boxes for them a to

a) search for certain text and
b) create new tables name. default set to my main table.

This way nobody has to go in and edit the wonderful code DK supplied.

last of all i added an error check aswell - incase somebody didn't want to import the files and table

Code:
Sub ImportAllFilesInFolder()

On Error GoTo Xit

    Dim strFolderName As String
        Dim oFSObj As Object, oFSFolder As Object, oFSFile As Object
            
'new varibles
Dim TextType As String, TableName As String

TextType = InputBox("Import Text Files that contain:" & vbCrLf & vbCrLf & _
            "eg *Nov* " & vbCrLf & vbCrLf & "Your text must be written between the *'s", _
            "Search for Files", "* *")
                            
TableName = InputBox("What is the Table name?", "Table Name", "Results")
 
strFolderName = "C:\TimeSheet" 'Change the textfile location here.
'-------------------------------------------------------

    'Create an instance of the MS Scripting Runtime - we can use
    'this to easily work with files and folders
    
    Set oFSObj = CreateObject("Scripting.FileSystemObject")

    Set oFSFolder = oFSObj.getfolder(strFolderName)

    For Each oFSFile In oFSFolder.files
    
           If oFSFile.Name Like TextType Then

            DoCmd.TransferText acImportDelim, , TableName, oFSFile.Path, True

            End If

    Next oFSFile


    Set oFSFile = Nothing
    Set oFSFolder = Nothing
    Set oFSObj = Nothing

Exit sub

Xit: MsgBox "Table and files not Imported", vbOKOnly + vbCritical, "Error"

End Sub

Hope it useful, it works for me - sorry if the codes a little lumpy

DK cheers (y)
 
Upvote 0

Forum statistics

Threads
1,221,581
Messages
6,160,630
Members
451,661
Latest member
hamdan17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top