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.
12 3126
Ive put together some code for you.
To use it, call it with the full path of the txt file, for example: - 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. - Public Sub importTxt(strPath As String)
-
'****************************************************************************
-
'Purpose: To open a txt file, get the road name, and import parts of the txt file to access
-
'
-
'Author: TheSmileyCoder @ Bytes.Com
-
'Copyright: You may copy and use this code, as long as this notice is left intact
-
'
-
'Input: strPath, the full path of the text file, for examle "C:\ImportFiles\example.txt"
-
'
-
'Output: None, a table is created or appended too if table allready exists
-
'****************************************************************************
-
'Open the file
-
Dim fs As filesystemobject
-
Set fs = CreateObject("scripting.filesystemobject")
-
Dim f As TextStream
-
Set f = fs.OpenTextFile(strPath, ForReading)
-
-
'Go to line 3 and get road name
-
f.SkipLine
-
f.SkipLine
-
Dim strTableName As String
-
'Replace any spaces with underscores
-
strTableName = "tbl_" & Replace(f.ReadLine, " ", "_")
-
-
'go to line 12, where the table start
-
Dim i As Integer
-
For i = 3 To 12
-
f.SkipLine
-
Next
-
-
-
'Read the rest of the file into a string
-
Dim strContent As String
-
strContent = f.ReadAll
-
-
'Save this to a textfile
-
Dim strOutputPath As String
-
strOutputPath = "C:\Documents and Settings\AEC.OSS\Desktop\Bytes DBs\ImportText\ImportFile.txt"
-
Dim f2 As TextStream
-
Set f2 = fs.CreateTextFile(strOutputPath, True, False)
-
f2.Write strContent
-
f2.Close
-
Set f2 = Nothing
-
-
'Now import the file, note a importspec must be specified
-
DoCmd.TransferText acImportDelim, "tso_ImportSpec", strTableName, strOutputPath, True
-
-
'cleanup
-
fs.DeleteFile strOutputPath, True
-
Set fs = Nothing
-
Set f = Nothing
-
-
End Sub
-
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.
Yes, I am in Denmark.
The code should probably go into a seperate module.
Your welcome.
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
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: - 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.
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.
Can you post your Docmd.TransferText line as it looks now?
DoCmd.TransferText acImportDelim, "ImportSpec", strTableName, strOutputPath, True
The name of the Import spec is ImportSpec.
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.
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.
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?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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
|
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...
|
by: goelamit1982 |
last post by:
How to Read multiple text file using XML and store into the database
|
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.
|
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....
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
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...
| |