473,585 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Text File Import

Hello,

I'd appreciate any help which is offered to my problem. I'm using
Acess 97.

I have a flat text file which lists names and addresses. Sometimes,
these can be 4 lines and sometimes 5 lines, with a blank line
separating them...
e.g.

Michael Wilde
Foxrock
Coolagh
Dublin

Tommy Williams
Ratoath
Lucan Drive
Finglas
Meath

etc...

I need to import this to a table whaich has 5 fields. When it is a 4
line address i need to leave the fifth field blank, and start back at
field 1 for the next record...

The following code works to a point. It does what i require but:
1. after each 5 line address it inserts a blank line
2. it omits the last entry of name and address
3. i get an error 62 - "input past end of file"
Function ImportFile()

Dim intImportFile As Integer
Dim strImportFile As String
Dim strTableName As String
Dim strInputLine As String
Dim dbs As Database
Dim rst As Recordset

strImportFile = "Y:\databases\m ilk\suppliers.t xt"
strTableName = "TempTable"

Set dbs = CurrentDb
Set rst = dbs.OpenRecords et(strTableName , dbOpenDynaset)

intImportFile = FreeFile
Open strImportFile For Input As #intImportFile

'Do Until EOF(intImportFi le)
While Not EOF(intImportFi le)
rst.AddNew
Line Input #intImportFile, strInputLine
If Len(strInputLin e) = 0 Then
GoTo cont
Else
rst!Line1 = strInputLine
End If
Line Input #intImportFile, strInputLine
If Len(strInputLin e) = 0 Then
GoTo cont
Else
rst!Line2 = strInputLine
End If
Line Input #intImportFile, strInputLine
If Len(strInputLin e) = 0 Then
GoTo cont
Else
rst!Line3 = strInputLine
End If
Line Input #intImportFile, strInputLine
If Len(strInputLin e) = 0 Then
GoTo cont
Else
rst!Line4 = strInputLine
End If
Line Input #intImportFile, strInputLine
If Len(strInputLin e) = 0 Then
GoTo cont
Else
rst!Line5 = strInputLine
End If
cont:
If EOF(intImportFi le) Then GoTo end1
rst.Update

Wend
'loop
end1:
Close intImportFile
End Function
Any help would be sincerely appreciated...

Thank you....
Nov 13 '05 #1
5 2672
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I'd do it like this (untested):

' use the same code you used to open the file & the recordset

Dim strLine(1 to 5) As String
Dim bytLine as Byte
Dim i as byte

' initialize
bytLine = 1
Line Input #intImportFile, strLine(bytLine )

Do While Not EOF(intImportFi le)
' Check for blank line, which means add record
if len(strLine(byt Line))= 0 then
rs.AddNew
for i = 1 to bytLine - 1
rs("Line" & i) = strLine(i)
next i
rs.Update
bytLine = 0 ' reset for next record lines
end if
bytLine = bytLine + 1 ' increment for next line
Line Input #intImportFile, strLine(bytLine )
Loop

' Catch any fields from last record which didn't end on blank line
If bytLine > 1 then
rs.AddNew
for i = 1 to bytLine - 1
rs("Line" & i) = strLine(i)
next i
rs.Update
end if

--
MGFoster:::mgf0 0 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQRvDb4echKq OuFEgEQLCpgCgzr b61ql9ALOTLJGQo kZX19+aLjIAnick
V9QDrvdqqg+Z6tu UZV5Jn5Je
=EXkG
-----END PGP SIGNATURE-----

osmethod wrote:
Hello,

I'd appreciate any help which is offered to my problem. I'm using
Acess 97.

I have a flat text file which lists names and addresses. Sometimes,
these can be 4 lines and sometimes 5 lines, with a blank line
separating them...
e.g.

Michael Wilde
Foxrock
Coolagh
Dublin

Tommy Williams
Ratoath
Lucan Drive
Finglas
Meath

etc...

I need to import this to a table whaich has 5 fields. When it is a 4
line address i need to leave the fifth field blank, and start back at
field 1 for the next record...

The following code works to a point. It does what i require but:
1. after each 5 line address it inserts a blank line
2. it omits the last entry of name and address
3. i get an error 62 - "input past end of file"
Function ImportFile()

Dim intImportFile As Integer
Dim strImportFile As String
Dim strTableName As String
Dim strInputLine As String
Dim dbs As Database
Dim rst As Recordset

strImportFile = "Y:\databases\m ilk\suppliers.t xt"
strTableName = "TempTable"

Set dbs = CurrentDb
Set rst = dbs.OpenRecords et(strTableName , dbOpenDynaset)

intImportFile = FreeFile
Open strImportFile For Input As #intImportFile

'Do Until EOF(intImportFi le)
While Not EOF(intImportFi le)
rst.AddNew
Line Input #intImportFile, strInputLine
If Len(strInputLin e) = 0 Then
GoTo cont
Else
rst!Line1 = strInputLine
End If
Line Input #intImportFile, strInputLine
If Len(strInputLin e) = 0 Then
GoTo cont
Else
rst!Line2 = strInputLine
End If
Line Input #intImportFile, strInputLine
If Len(strInputLin e) = 0 Then
GoTo cont
Else
rst!Line3 = strInputLine
End If
Line Input #intImportFile, strInputLine
If Len(strInputLin e) = 0 Then
GoTo cont
Else
rst!Line4 = strInputLine
End If
Line Input #intImportFile, strInputLine
If Len(strInputLin e) = 0 Then
GoTo cont
Else
rst!Line5 = strInputLine
End If
cont:
If EOF(intImportFi le) Then GoTo end1
rst.Update

Wend
'loop
end1:
Close intImportFile
End Function
Any help would be sincerely appreciated...

Thank you....


Nov 13 '05 #2
Thank you very much - MGFoster

Dim strLine(1 to 5) As String - once I changed the 5 to 6 it work
perfectly.

Thank you for sharing your coding with me.....

osmethod
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #3
Fixed it... I wrote everything to a variant array first and then wrote
it to the table... (Hey James Fortune, could ya make this pretty for
me? You know, fix it so it's *really* right, instead of just
functional, add error trapping, etc)

Option Compare Database
Option Explicit

Public Sub ImportTextFile( ByVal strFileName As String)
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim varAddress(0 To 4) As Variant
Dim strTextLine As String
Dim intCounter As Integer
Dim intFieldsCounte r As Integer

Dim intFileNo As Integer

Set db = CurrentDb
Set rs = db.OpenRecordse t("Table1", dbOpenTable, dbAppendOnly)
intFileNo = FreeFile
Open strFileName For Input As #intFileNo

Do Until EOF(intFileNo)
'Do While Not EOF(intFileNo) ' Loop until end of
file.
Line Input #intFileNo, strTextLine ' Read line into
variable.
strTextLine = Trim(strTextLin e) ' strip off blanks

'--if the line isn't blank, write to the array.
'--if it IS blank, assume end of record and write array values
to table.
If Len(strTextLine ) > 0 Then
varAddress(intC ounter) = strTextLine
intCounter = intCounter + 1
Else
'--reset the counter
intCounter = 0
'--write the record to the table
'--add a new record
rs.AddNew

'--fill in the values from the array
For intFieldsCounte r = 0 To 4
Debug.Print
rs.Fields(intFi eldsCounter).Va lue =
varAddress(intF ieldsCounter)
Next intFieldsCounte r

'--save the data to the record
rs.Update

'--clear the array
For intFieldsCounte r = 0 To 4
varAddress(intF ieldsCounter) = Null
Next intFieldsCounte r

End If

Loop

Close #intFileNo ' Close file.
rs.Close
Set rs = Nothing
Set db = Nothing

End Sub

Form level test code....

Private Sub Command0_Click( )
Dim varFileName As Variant
varFileName = GetOpenFile("C: \", "Pick a file, any file!")

If IsNull(varFileN ame) Then
MsgBox "uhh... you like gotta choose a file, man!"
Else
ImportTextFile varFileName
End If
End Sub

All the GetOpenFile code is right here (so your user can browse for
the file, if you want...)
http://www.mvps.org/access/api/api0001.htm
Nov 13 '05 #4
os******@eircom .net (osmethod) wrote in message news:<9c******* *************** ****@posting.go ogle.com>...
Hello,

I'd appreciate any help which is offered to my problem. I'm using
Acess 97.

I have a flat text file which lists names and addresses. Sometimes,
these can be 4 lines and sometimes 5 lines, with a blank line
separating them...
e.g.

Michael Wilde
Foxrock
Coolagh
Dublin

Tommy Williams
Ratoath
Lucan Drive
Finglas
Meath

etc...

I need to import this to a table whaich has 5 fields. When it is a 4
line address i need to leave the fifth field blank, and start back at
field 1 for the next record...

The following code works to a point. It does what i require but:
1. after each 5 line address it inserts a blank line
2. it omits the last entry of name and address
3. i get an error 62 - "input past end of file"
Function ImportFile()

Dim intImportFile As Integer
Dim strImportFile As String
Dim strTableName As String
Dim strInputLine As String
Dim dbs As Database
Dim rst As Recordset

strImportFile = "Y:\databases\m ilk\suppliers.t xt"
strTableName = "TempTable"

Set dbs = CurrentDb
Set rst = dbs.OpenRecords et(strTableName , dbOpenDynaset)

intImportFile = FreeFile
Open strImportFile For Input As #intImportFile

'Do Until EOF(intImportFi le)
While Not EOF(intImportFi le)
rst.AddNew
Line Input #intImportFile, strInputLine
If Len(strInputLin e) = 0 Then
GoTo cont
Else
rst!Line1 = strInputLine
End If
Line Input #intImportFile, strInputLine
If Len(strInputLin e) = 0 Then
GoTo cont
Else
rst!Line2 = strInputLine
End If
Line Input #intImportFile, strInputLine
If Len(strInputLin e) = 0 Then
GoTo cont
Else
rst!Line3 = strInputLine
End If
Line Input #intImportFile, strInputLine
If Len(strInputLin e) = 0 Then
GoTo cont
Else
rst!Line4 = strInputLine
End If
Line Input #intImportFile, strInputLine
If Len(strInputLin e) = 0 Then
GoTo cont
Else
rst!Line5 = strInputLine
End If
cont:
If EOF(intImportFi le) Then GoTo end1
rst.Update

Wend
'loop
end1:
Close intImportFile
End Function
Any help would be sincerely appreciated...

Thank you....


Close. Try:

Dim intImportFile As Integer
Dim strImportFile As String
Dim strTableName As String
Dim varInputLine As Variant
Dim dbs As Database
Dim rst As Recordset
Dim i As Integer

strImportFile = "C:\Input.t xt"
strTableName = "TempTable"

Set dbs = CurrentDb
Set rst = dbs.OpenRecords et(strTableName , dbOpenDynaset)

intImportFile = FreeFile
Open strImportFile For Input As #intImportFile

Do Until EOF(intImportFi le)
rst.AddNew
i = 1
varInputLine = Null
Do Until varInputLine = "" Or EOF(intImportFi le)
Line Input #intImportFile, varInputLine
If Trim(varInputLi ne) <> "" Then
varInputLine = Trim(varInputLi ne)
rst("Line" & i) = varInputLine
i = i + 1
End If
Loop
rst.Update
Loop
Close intImportFile

This took me less than 10 minutes to write so be kind.

James A. Fortune
Nov 13 '05 #5
pi********@hotm ail.com (Pieter Linden) wrote in message news:<bf******* *************** ****@posting.go ogle.com>...
Fixed it... I wrote everything to a variant array first and then wrote
it to the table... (Hey James Fortune, could ya make this pretty for
me? You know, fix it so it's *really* right, instead of just
functional, add error trapping, etc)


Perhaps the following code will help:

'--------begin form code
Private Sub cmdSelectImport Directory_Click ()
'Code adapted from post by Emile Besseling
Dim BInfo As MyBrowseInfo
Dim strDir As String
Dim strFile As String
Dim BrowseView As Long
Dim lngID As Long
Dim strFolder As String

With BInfo
.pidlRoot = 0
.lpszTitle = "Please choose an import directory."
.lpfn = 0
.lParam = 0
.iImage = 0
End With
lngID = SHBrowseForFold er(BInfo)
strDir = Space(255)
If lngID <> 0 Then
If SHGetPathFromID List(ByVal lngID, strDir) Then
strFolder = Left(strDir, InStr(strDir, Chr(0)) - 1)
End If
End If
strDir = strFolder
cbxShowFiles.Ro wSourceType = "Value List"
cbxShowFiles.Ro wSource = ""
'Only look for files ending with .txt
strFile = Dir(strDir & "\*.txt")
If strFile = "" Then
MsgBox ("No .txt files found in " & strDir)
Exit Sub
End If
Do While strFile <> ""
If cbxShowFiles.Ro wSource = "" Then
cbxShowFiles.Ro wSource = strFile
Else
cbxShowFiles.Ro wSource = cbxShowFiles.Ro wSource & strFile
strFile = Dir()
If strFile <> "" Then cbxShowFiles.Ro wSource =
cbxShowFiles.Ro wSource & ";"
End If
Loop
cbxShowFiles.Va lue = Null
MsgBox ("Please select a file to import.")
End Sub
'--------end form code

'--------begin module code
Option Compare Database
Option Explicit

Type MyBrowseInfo
hwndOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

Declare Function SHBrowseForFold er Lib "shell32.dl l" (ByRef lpbi As
MyBrowseInfo) As Long
Declare Function SHGetPathFromID List Lib "shell32.dl l" (pidl As Long,
ByVal sPath As String) As Long
'--------end module code

I'm trying not to use On Error unless I'm using a .mde or writing
something commercial so I'll let you do that. I didn't look at your
code but it's probably "really" right already.

James A. Fortune
Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5890
by: Chris | last post by:
Background: I am using a MS Access 2000 front end with SQL Server 8.0 back end. I have the requirement to import all text files (regardless of filename) from a given folder on the network into a table within SQL Server. This import needs to run continuously, as more text files will be saved in the folder by a separate system and they need...
1
9738
by: DCM Fan | last post by:
Access 2K, SP3 on Windows 2K, SP4 All, I have an import spec set up with quoted Identifiers and comma-separated values. The text file is produced by a 3rd-party program of which I have no control. It outputs all text fields surrounded by quotes, and all numeric fields w/o quotes. All fields are separated with commas. This has been...
5
11556
by: Johnny Meredith | last post by:
I have seven huge fixed width text file that I need to import to Access. They contain headers, subtotals, etc. that are not needed. There is also some corrupt data that we know about and can correct once the data is in Access (or during the import process itself). Furthermore, the text files are poorly set up, such that some records may be...
2
2937
by: Michael Thomas | last post by:
Hi everyone Do any of you know if this is possible in Access either by using the import tool or by writing a module in VB: I have a database containing information for a retail chain which, amongst others, has a table with fields for product description, quantity at locationx, etc. The problem is that very often I need to update it after...
2
2707
by: Phil Latio | last post by:
We have despatch process which kicks out picking information into text files at hourly intervals. The text files are named sequentially by process; 'ABC.txt', 'ABC01.txt', 'ABC02.txt' for one process, or 'XYZ.txt', 'XYZ01.txt' for another process. The folder contents would look like this; ABC.txt ABC01.txt
1
3761
by: ghadley_00 | last post by:
Hi, I have a MS access database table for which I regularly need to import fixed width text data. At present I have to to cut and paste the text data from its source to a text file, save the file, import the text file as fixed width text , and then run an update query to copy the appropriate info into fields of a different table. Is it...
13
4173
by: DH | last post by:
Hi, I'm trying to strip the html and other useless junk from a html page.. Id like to create something like an automated text editor, where it takes the keywords from a txt file and removes them from the html page (replace the words in the html page with blank space) I'm new to python and could use a little push in the right direction, any...
1
4169
by: Child of His | last post by:
I have been through every trick I know, or has been suggested. I have a one to two million line fixed field database in text format. I want to bring it into Access 97. When I use the external data import, the list of files to import does not include text files of any type, only other database formats. On a 102,000 line text file, I was...
4
12511
by: chimambo | last post by:
I have 2 problems: 1. I want to import a single text file into an access table using a Macro. I am however getting an error that I need to put a specification name argument. What does this mean? It also suggests that I add a schema.ini in the source folder. What does this .ini do and how do I create and use it? 2. After this is resolved,...
0
10746
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not...
0
7836
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...
0
8199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8336
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8212
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...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5389
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
3835
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...
1
2343
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
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.