Get External Data ?

murlen

Registered User.
Local time
Today, 10:09
Joined
Nov 18, 2002
Messages
37
I hope someone can help me with this,
I have a text file that is formatted in the following way.

USER: Tom
LOCATION: T2
CODE: qwe
VALUE: 754
STRING: deerii reree rerer 3ejij98 ed33 djie3

USER: Jane
LOCATION: Q4
CODE: xr8
VALUE: 986
STRING: fghjfg fg88e cv9jds as288 zxzx8 swqq --223 2

USER: Harry
LOCATION: C32
CODE: 9r7
VALUE: 653
STRING: qazx6 hhgds sdd78 cvc8 urq11a
...

How can I import the data into a table so that USER, LOCATION, CODE, etc are Fields in the table and the text across from each are records?

Thanks,
Murlen
 
How big is this data file? Meaning how many of those entries do you have? Is this a one-time import, or do you have to do it on a recurring basis?
 
If this is a one off import, I would drop it into Word and do a search and replace so that
USER: Tom
LOCATION: T2
CODE: qwe
VALUE: 754
STRING: deerii reree rerer 3ejij98 ed33 djie3
becomes
Tom,T2,qwe,754,deerii reree rerer 3ejij98 ed33 djie3

Then do a straight text file import.

If this is to be a routine import, then you will need to read the file in and parse it using code - beyond my experience!
 
the file as about 5000 lines or more and it changes all the time.
I need to import it over time, meaning I need to import it once now and again ever couple of months or so.

the records are always changing, much like this
this week,

USER: tom
CODE: qwe

next week,

USER: tom
CODE: xr4
 
Given that this is something you will do many times, your best bet is to write some VBA code.

You will need to look up the following topics, which will comprise your tool set for this operation.

Open statement
Close statement
Input Line statement
InStr function
Len function
Mid function
Left function
Right function
Edit function

OpenRecordset method
CurrentDB function
Close method (applied to recordset objects)
AddNew method (if you regenerate the tables from scratch)
Edit method and Seek method (if you update the tables)
Update method

In overview, you can do this from a form or a macro, both of which are capable of running code. If you do it from a macro, you have to write the code as a function. If you do it from a form, even an unbound one, you can write the code as a simple subroutine with no return value.

I might take the approach of importing the new data to an empty (but pre-existing) staging table to allow you to do some preliminary data massaging, then use an update or append query as appropriate.

The import routine I would used in the scenario I suggested looks like this, in broad-brush terms:

1. Open the input text file
2. Open the recordset
3. Try to read a new line. If you are not at EOF, proceed.
4. Add a new record to the recordset
5. Parse the individual lines of your data groups using InStr to find the constant part that represents your field names. Also find the colons that separate the field name from the field data.
6. For each line that applies to the record, verify the field name, and if it passes, insert the field data.
7. When you have finished the data grouping, update the recordset.
8. Go back to step 3 unless at end of input file.
9. Close everything you opened.

OK, that takes care of the import. From there, all you have to do can probably be done with no more than three or four queries, and maybe only one or two queries.

Now, I'll step back and touch on my line-item 6 a bit more.

If your format for each input line is exactly <fieldname>:<fielddata>, then you can use InStr to find the colon. The position of the colon gives you two values. First, you know where the field name ends. Second, you know where the data area begins. If the import table's field's correct name appears to the left of the colon, use the Left function to extract it. If the import table's data appears to the right of the colon, use the Right function to extract it. Let's say you assign the long integer loPos to the position of the colon.

loPos = InStr( 1, Line, ":" ) {or is it InStr( 1, ":", line) .... please check that one, I always forget and have to look it up...}

recset.Fields(Left$(Line,loPos-1)) = Right$(Line, Len(Line)-loPos)

This method DOES NOT WORK if the field name in the staging table doesn't exactly match the field name that appear in your text file. You might also wish to impose an Edit function on the data string before you store it.
 
Thanks for the help everyone!
I appreciate it.

Murlen:)
 

Users who are viewing this thread

Back
Top Bottom