transfer text files to tables

niki

axes noob 'll b big 1 day
Local time
Today, 23:13
Joined
Apr 17, 2003
Messages
66
Hello,
I want to have a button dedicated to importing text files into tables on a click. Here's the code I was given but I can"t manage to have it work. Both of my field are memo type is there a special declaration for this type of fields? Is the String type OK for this type of fields?

Is it possible to modify the script so that multiple text files with identical structures (number of fields, field names...) be imported in different tables with also identical structures?

Thanks for your help!
cheers
nico
Code:
Dim dbs As DataBase
Dim rst As Recordset
Dim sql As String
Dim buf As String
Dim ptr As long
Dim Delimiter As String

Delimiter = Chr(ASC(181))  ' for "µ" delimiter 
Set dbs = CurrentDb
sql = "Select * from YourTable"
Set rst = dbs.OpenRecordset(sql)

Open "C:\Prop_Aero.txt" For Input As #1

Line Input #1, buf
While Not EOF(1)
   ptr = 1
   '
   Col1 = ""
   While(Mid(buf, ptr, 1) <> Delimiter)
      Col1 = Col1 & Mid(buf, ptr, 1)
      ptr = ptr + 1
      Wend
   ptr = ptr + 1
   '
   Col2 = ""
   While(Mid(buf, ptr, 1) <> Delimiter)
      Col2 = Col2 & Mid(buf, ptr, 1)
      ptr = ptr + 1
      Wend
   ptr = ptr + 1
   '
   rst.Addnew
   rst!Collaboration = Col1
   rst!ContactPerson = Col2
   rst.Update
   Line Input #1, buf
   Wend
 
nico,

If you put a textbox on a screen for the filename and path
you could do:

Open Me.txtFileName For Input As #1

Then you could put a combo box with a list of all your tables:

sql = "Select * from " *& Me.Combo

How often do you have to process the files, and who is
using the s/w when the import is done? These play a
great role in seeing how "civilized" you make the s/w.

There are also examples around here about how to bring
up a browser (like windows explorer) to select a file to
import.

Also, the String type can hold a memo field.

Wayne
 
nico,

After re-reading your post, I forgot the most important part!
What is wrong with the code? Is the delimiter right? Does
it make any entry to the table? Do the records span input lines
(They are memo fields!).

The code is written to read one record per line, delimited by
Chr(181). If you are talking about memo fields, I get the feeling
that it may take many lines to make up a record.

More details please.

Wayne
 
OK wayne!
I found out the same thing!!
I now manage to import some parts of the txt files but the "µ" delimiter is not recognised and imported as part of the field. Maybe its because the µ is situtated between two" like this:
"blahblah"µ"blahblahablha".... I dunno...
But when it imports the text in the fields, it seems that the delimiter taken is 1. For example, there are multiple phone numbers and when a 1 appears in the phone number it skips the field and puts the rest of the phone number in the following field. Still without caring about the µ.

Anyways you are right, my text files have many many many lines as each txt file can weigh up to 1 meg...

For the multiple files importation, I'll paste the working code for each text file which has to be imported in a separate file, It'll do fine!!
Is this enough for you to understand my problem? thx...

nico
 
niki,

It sounds like the characteristics of the file are just a little bit
different than anticipated.

Can you post a sample of the file, it shouldn't take much?

Wayne
 
Nico,

Post a small sample of your data and we'll get this fixed. Make
sure that it contains any multi-line records, if there are any.

I see that the initial post has not been run, cause it says
"Select * From YourTable".

Wayne
 
sample

here's the sample wayne!
for the sql statement its all good!
thanks for checking this!
cheers nico
 

Attachments

Also,

I did run the initial post with the correct sql statement! I would'nt repost a code I would'nt have checked!! ;)
I am at home right now and I don't have the internet connection on my work laptop... Thats why the code hasn't been updated!
my bad!

nico
 
Nico,

Attached is the DB. When it opens, select the "Read File"
command button. Double-click on a file to select it. In
the dialog box, it will ask for a "NickName". I am not putting
each import into a seperate table, just provide a unique
identifier (anything will do).

I still have a problem or two, like a leading double-quote,
but I've had enough fun with this for a while.

Let me know how it works for you.

Wayne
 

Attachments

Wayne,
thank you very much for your help!! You even went beyond what I hoped!
Is there a way I could add up multiple text files using the SHIFT key when I import the text file?

Any ways it works!! thank you I'll adapt it to my work! thank u!
nico
 
nico,

The API that brings up the "browser" does not support
multiple selections. You could call it multiple times and
save the results in an array, or listbox, but how many
times are you going to do imports?

Feel free to make a copy of the DB and experiment with
running the code with the debugger. It will help you
get rid of the leading double-quote, etc.

Glad to help,

Wayne
 
OK!
For the leading double quote I have made a stripping function which takes them away.
For the multiple selection, I'll have to do the same selection as much time as there are text files. It's long but it works!

thanks again
nico
 
Nico,

You might consider putting a combo-box that has the names
of all "NickNames" that have been imported to date. When
you select one, it will display only those records.

Just base your form on a query (not the table) and use
Forms![frmPersonDB]![cboNickName] as the criteria.

Wayne
 

Users who are viewing this thread

Back
Top Bottom