473,520 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

39 New Member
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, 433 views)
Feb 25 '11 #1
12 3126
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
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
Miguel Valenzue
39 New Member
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 Recognized Expert Moderator Top Contributor
Yes, I am in Denmark.
The code should probably go into a seperate module.
Your welcome.
Feb 25 '11 #4
Miguel Valenzue
39 New Member
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 Recognized Expert Moderator Top Contributor
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
Miguel Valenzue
39 New Member
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 Recognized Expert Moderator Top Contributor
Can you post your Docmd.TransferText line as it looks now?
Mar 8 '11 #8
Miguel Valenzue
39 New Member
DoCmd.TransferText acImportDelim, "ImportSpec", strTableName, strOutputPath, True

The name of the Import spec is ImportSpec.
Mar 8 '11 #9
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
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
Miguel Valenzue
39 New Member
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
Miguel Valenzue
39 New Member
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,560 Recognized Expert Moderator MVP
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
4838
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 these kinds of formats to MySQL ( and MS SQL) Please see the attachments, and the text format is changed from DAT format, delimited by tabs or...
0
2142
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 with MySQLFront 2.5. Both these GUI programmes have an "import from text file" command which I have used successfully several times to add entries to...
3
9768
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 then submit it. The data will be emailed to an administrator who will then print the month's report. I also plan to extract the data and store it in...
3
2867
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 that I have read. We use an antique BBX/BASIC accounting program which we use to generate invoices. Every morning we save the previous day's...
0
1255
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? ================================= The TEXT ODBC driver calls the Jet database engine and reads text files via Jet's Text IISAM driver. You may try code similar to the following: Dim...
9
4295
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 constant) My problem is not knowing how many lines are in my file. I want to use this
14
11152
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 AddRow method or Insert, reading each line of the text file, the problem of course is velocity, it would take more than 4 hours to add all lines/records...
1
2119
by: goelamit1982 | last post by:
How to Read multiple text file using XML and store into the database
3
9382
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 Inert the same into Oracle Table using a Bulk Insert.
8
2992
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 the rest of the line based on a given specification into an appropriate table. I'm not that familiar with VBA, but I can pick it up if I need to....
0
7219
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7177
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7576
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5756
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3289
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3287
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1663
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
515
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.