473,385 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to import multiple text file data sets into their own table.

I collect traffic data from a machine that outputs text files with the data.
I want to import each text file as it's own table into an Access database and do it without having to run the import wizard for each one.

As part of this function, I'd like to strip the first 15 lines of data from the dataset, parse in the first 5 columns, and name the table after the information on line 3 which is the name of the street the data was taken from.

I know this is a tall order but I'm hoping for a nudge in the right direction.

I'm new at Access and know something like this is possible probably through some VBA script.
I've included the original dataset.
Attached Files
File Type: txt 20100823_Kniveveien_detaljert.txt (222.5 KB, 431 views)
Feb 25 '11 #1

✓ answered by TheSmileyCoder

Ive put together some code for you.
To use it, call it with the full path of the txt file, for example:
Expand|Select|Wrap|Line Numbers
  1. importTxt("C:\Trafikdata\20100823_Kniveveien_detaljert.txt")
NOTE: You need to have a reference set to "Windows Script Host Object Model"
Visual Basic->Tools->References

You must also have created an importspec to use.

If you have any questions about the code, and what it does, please ask.
Expand|Select|Wrap|Line Numbers
  1. Public Sub importTxt(strPath As String)
  2. '****************************************************************************
  3. 'Purpose: To open a txt file, get the road name, and import parts of the txt file to access
  4. '
  5. 'Author: TheSmileyCoder @ Bytes.Com
  6. 'Copyright: You may copy and use this code, as long as this notice is left intact
  7. '
  8. 'Input: strPath, the full path of the text file, for examle "C:\ImportFiles\example.txt"
  9. '
  10. 'Output: None, a table is created or appended too if table allready exists
  11. '****************************************************************************
  12.     'Open the file
  13.         Dim fs As filesystemobject
  14.         Set fs = CreateObject("scripting.filesystemobject")
  15.         Dim f As TextStream
  16.         Set f = fs.OpenTextFile(strPath, ForReading)
  17.  
  18.     'Go to line 3 and get road name
  19.         f.SkipLine
  20.         f.SkipLine
  21.         Dim strTableName As String
  22.         'Replace any spaces with underscores
  23.         strTableName = "tbl_" & Replace(f.ReadLine, " ", "_")
  24.  
  25.     'go to line 12, where the table start
  26.         Dim i As Integer
  27.         For i = 3 To 12
  28.             f.SkipLine
  29.         Next
  30.  
  31.  
  32.     'Read the rest of the file into a string
  33.         Dim strContent As String
  34.         strContent = f.ReadAll
  35.  
  36.     'Save this to a textfile
  37.         Dim strOutputPath As String
  38.         strOutputPath = "C:\Documents and Settings\AEC.OSS\Desktop\Bytes DBs\ImportText\ImportFile.txt"
  39.         Dim f2 As TextStream
  40.         Set f2 = fs.CreateTextFile(strOutputPath, True, False)
  41.         f2.Write strContent
  42.         f2.Close
  43.         Set f2 = Nothing
  44.  
  45.     'Now import the file, note a importspec must be specified
  46.         DoCmd.TransferText acImportDelim, "tso_ImportSpec", strTableName, strOutputPath, True
  47.  
  48.     'cleanup
  49.         fs.DeleteFile strOutputPath, True
  50.         Set fs = Nothing
  51.         Set f = Nothing
  52.  
  53. End Sub
  54.  

12 3119
TheSmileyCoder
2,322 Expert Mod 2GB
Ive put together some code for you.
To use it, call it with the full path of the txt file, for example:
Expand|Select|Wrap|Line Numbers
  1. importTxt("C:\Trafikdata\20100823_Kniveveien_detaljert.txt")
NOTE: You need to have a reference set to "Windows Script Host Object Model"
Visual Basic->Tools->References

You must also have created an importspec to use.

If you have any questions about the code, and what it does, please ask.
Expand|Select|Wrap|Line Numbers
  1. Public Sub importTxt(strPath As String)
  2. '****************************************************************************
  3. 'Purpose: To open a txt file, get the road name, and import parts of the txt file to access
  4. '
  5. 'Author: TheSmileyCoder @ Bytes.Com
  6. 'Copyright: You may copy and use this code, as long as this notice is left intact
  7. '
  8. 'Input: strPath, the full path of the text file, for examle "C:\ImportFiles\example.txt"
  9. '
  10. 'Output: None, a table is created or appended too if table allready exists
  11. '****************************************************************************
  12.     'Open the file
  13.         Dim fs As filesystemobject
  14.         Set fs = CreateObject("scripting.filesystemobject")
  15.         Dim f As TextStream
  16.         Set f = fs.OpenTextFile(strPath, ForReading)
  17.  
  18.     'Go to line 3 and get road name
  19.         f.SkipLine
  20.         f.SkipLine
  21.         Dim strTableName As String
  22.         'Replace any spaces with underscores
  23.         strTableName = "tbl_" & Replace(f.ReadLine, " ", "_")
  24.  
  25.     'go to line 12, where the table start
  26.         Dim i As Integer
  27.         For i = 3 To 12
  28.             f.SkipLine
  29.         Next
  30.  
  31.  
  32.     'Read the rest of the file into a string
  33.         Dim strContent As String
  34.         strContent = f.ReadAll
  35.  
  36.     'Save this to a textfile
  37.         Dim strOutputPath As String
  38.         strOutputPath = "C:\Documents and Settings\AEC.OSS\Desktop\Bytes DBs\ImportText\ImportFile.txt"
  39.         Dim f2 As TextStream
  40.         Set f2 = fs.CreateTextFile(strOutputPath, True, False)
  41.         f2.Write strContent
  42.         f2.Close
  43.         Set f2 = Nothing
  44.  
  45.     'Now import the file, note a importspec must be specified
  46.         DoCmd.TransferText acImportDelim, "tso_ImportSpec", strTableName, strOutputPath, True
  47.  
  48.     'cleanup
  49.         fs.DeleteFile strOutputPath, True
  50.         Set fs = Nothing
  51.         Set f = Nothing
  52.  
  53. End Sub
  54.  
Feb 25 '11 #2
You're in Denmark? I'm in Norway. How's your Norwegian?
Thanks for putting this together. Now I have to figure out exactly how to run it. Where to stick it into access etc. Thanks for your help.
Feb 25 '11 #3
TheSmileyCoder
2,322 Expert Mod 2GB
Yes, I am in Denmark.
The code should probably go into a seperate module.
Your welcome.
Feb 25 '11 #4
Hi SmileyCoder,
I copied the text into a module and set the reference set by opening the reference window and checking the box next to "windows Script Host Object Model"
I then hit the play button to run the module and get a pop up window asking which macro to run and the import code I listed isn't in there so it doesn't run.

I created an import spec and named it Import1 and rewrote the line of code as such.
'Now import the file, note a importspec must be specified
DoCmd.TransferText acImportDelim, "Import1", strTableName, strOutputPath, True
Mar 7 '11 #5
TheSmileyCoder
2,322 Expert Mod 2GB
I haven't used the F5/Play button much, but looking at it shortly it seems it only suggests subs that require no input argument.
The sub I wrote for you requires a input argument, the path to the file. In the VBA immediate window (ctrl-G opens the window if you are in the editor) you could simply write:
Expand|Select|Wrap|Line Numbers
  1. importTxt "C:\MyFolder\MyFile.txt"
to test the function.
For more advanced functionalty the code could be tied in with a form, and a filebrowser, but tahts for a seperate topic.
Mar 7 '11 #6
Well I feel like I'm a lot closer. I created a seperate macro and inserted
importTxt (""C:\Documents and Settings\mvadrm\My Documents\Traffic Data\20100823_Kniveveien_detaljert.txt")
and set the strOutputPath also.
Now I'm getting an error that states that it cannot find the import spec.
"Run-time error '3625'
The text files specification 'ImportSpec' does not exist. You cannot import, export, or link using the specification.

I have an import spec named ImportSpec saved so I'm not sure exactly what's happening. But I'm very happy with the progress thus far and I appreciate your help.
Mar 8 '11 #7
TheSmileyCoder
2,322 Expert Mod 2GB
Can you post your Docmd.TransferText line as it looks now?
Mar 8 '11 #8
DoCmd.TransferText acImportDelim, "ImportSpec", strTableName, strOutputPath, True

The name of the Import spec is ImportSpec.
Mar 8 '11 #9
TheSmileyCoder
2,322 Expert Mod 2GB
I dont know what to say. It worked for me.

From the error message you are getting it sounds like the importspec has not been saved properly. I would try to save it again, possibly under a new name.
Mar 8 '11 #10
Uh, as a novice I apologize. When you run the import wizard it asks you to save the import steps. So I saved it as ImportSpec and thought that I was running the importsteps. The import specs are located within the advance button (as you know) and so once I created the proper import spec and saved it in the wizard, I ran it and it worked. Completely awesome.
Now I have to write a quick macro for prompting me for the file and I'm off.
Mar 8 '11 #11
I want to expand upon this now (of course) and during the import, take that road name and dataset name and insert it into a seperate table. This will allow me to link all the data from the imported tables. Should I start a seperate thread for that?
Mar 8 '11 #12
NeoPa
32,556 Expert Mod 16PB
Miguel:
Should I start a seperate thread for that?
The short answer to that is "Yes".

By the time you read this I will probably have moved the posts on the new question to a separate thread (Merge Tables into One), whose link will be posted in here for ease of access.
Mar 17 '11 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: atse | last post by:
Hi, I am still stick on the text file being imported to database. Can anybody help? I have just done the csv format files. And then I work on text and DAT formats, but I have problem to import...
0
by: adrian GREEMAN | last post by:
When I try to import a text file with new data for an existing table I get the error "1148 - the used command is not allowed with this MySQL version." I have tried with both PHPMyAdmin2.3 and...
3
by: Yellowbird | last post by:
I have an HTML form with a pre-built table containing "standard" column headings (for example, First Name, Last Name, SSN, Hours, etc.). I want a user to be able to enter values into the form and...
3
by: Frank Reichenbacher | last post by:
I've been developing an Access database for our small office for several years now so I am not a newbie. However, I have a need for which I am not finding a mechanism to accomplish in the material...
0
by: TJS | last post by:
I need to import a delimited text file to an msde table, I found discussion item below. anything available for msde which is comparable to this Jet example? =================================...
9
by: mabond | last post by:
Hi My text file varies in size on each occasion it is processed (i.e different number of lines) Contents are coma separated, giving data for each column in my sql table (number of columns is...
14
by: mfrsousa | last post by:
hi there, i have a huge large text file (350.000 lines) that i want to import to a MS Acccess Database, of course i don't want to use Access, but do it with C#. i already have tried the...
1
by: goelamit1982 | last post by:
How to Read multiple text file using XML and store into the database
3
by: =?Utf-8?B?S3VsZGVlcCBWaWpheWt1bWFy?= | last post by:
Language: C#.NET 2.0 Technology: ASP.NET 2.0 Database: Oracle 10g Hi All, Could any one of you please suggest the BEST method to: 1. Fetch data from a very large .csv file (around 8 MB) and...
8
by: diasdaman | last post by:
I know how to import text files, but in this case I need to import a text file line by line on an on-the-fly basis, such that the Access will look at the first two digits of a line, and then import...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.