Importing a text file(s)

Posted on 2002-03-14
Last Modified: 2010-05-02
One of our systems produces a text file that list rejected records.  These records can reject for any reasons.  The structure is the same always of the file – with different data of course.  The structure is as follows:

Row #     Field
1     Date
2     MQManager Error #:
3     Desc:
4     System Error #:
5     Desc:
6     MQ-Label:
7     Severity:
8     Table:
9     Action:
10     MQ-Body:
11     48 Dashes

Every record has this format.  The length of MQ-Body can be up to maybe more then 1500 Characters.  The longest I have found so far is 1497.

I do feel I can parse the fields without much of a problem, but my concern is how do I start looping through the text file and then write out the records to a table of this format:


Field             Data Type     Matching Row
MQ_Error     Varchar(20)     Row  2
SystemError     Long             Row  4
UpdateTable     VarChar(30)     Row  5
Server             VarChar(20)     Row  6
FormatTable     VarChar(30)     Row  8
Values             VarChar(100)     Row 11

I want to assign to 6 variables the values need to be written to the table.  I would have to start the loop on the date, then scroll through the text file and gather the values in variables.  Once I reach the 48 dashes, I would then write the record to the table.  If not at EOF and see another date the process needs to start over.

Any ideas on how to do this?  It is the looping through the records in the last part is what I am looking for help in.  

Question by:JTD3
  • 2
  • 2
LVL 18

Accepted Solution

mdougan earned 200 total points
ID: 6865696
If I'm understanding correctly, each of the 11 Rows listed above are actual new lines in the text file?  That 1 "record" consists of all 11 lines, and then starts a new "record"

Will all records each have all 11 lines, or is it that some might have 8 lines, some have 10 lines?

Do the rows of text in the text file actually contain the row#?  Or just the data for that row?

My approach would be to have a loop that starts as:

While Not EOF(fhandle)
      Line Input#.....
      Line Input#.....
      Line Input#.....
      Line Input#.....
      Line Input#.....
      Line Input#.....
      Line Input#.....
      Line Input#.....
      Line Input#.....
      Line Input#.....
      Line Input#.....

Then, do a Line Input # for each of the 11 rows, assuming that all records always have all 11 rows.

If the row contains the row# you have to parse this out of the string and assign the significant text to a variable.  Once you've gone through all 11 lines (or EOF) then take the variables you need and update the DB.  Then, loop back to the start of the While statement.

You can get a little fancier if you want to create a user defined data type, or a class that holds the fields.  So, instead of doing:

Dim MQ_Error as string
dim SystemError as long
Dim UpdateTable as string
Dim Server as string
Dim FormatTable as string
Dim Values as string

You'd do this:

Type Reject
   MQ_Error as string
   SystemError as long
   UpdateTable as string
   Server as string
   FormatTable as string
   Values as string
End Type

Dim oReject as Reject
Dim sDummy
While Not EOF(fhandle)
      Line Input# fhandle, sDummy ' 1
      Line Input# fhandle, sDummy ' 2
      oReject.MQ_Error = sDummy
      Line Input# fhandle, sDummy ' 3
      Line Input# fhandle, sDummy ' 4
      oReject.SystemError = CLng(sDummy)

... etc
      Line Input# fhandle, sDummy
      Line Input# fhandle, sDummy
      Line Input# fhandle, sDummy
      Line Input# fhandle, sDummy
      Line Input# fhandle, sDummy
      Line Input# fhandle, sDummy
      Line Input# fhandle, sDummy

Author Comment

ID: 6878394
mdougan ,

1) all 'records' contain the same number of rows
2) the row numbers are not there - I placed them hoping to provide some more clarity.
3) there is acutally 2 more ros for ever record a space before the date and space after dashes - so 13 rows.

If i understand correctly this routine then will pull 13 records from the text file to proccess on each loop?

I am going to try this out in the tomorrow ( I hope), then I will let you know how it works.

Thank you for your input.

LVL 18

Expert Comment

ID: 6881569
Yes, if you know that you will have a standard number of rows, then you can just issue that number of Line Input# statements and you will know that you have read all the rows for each record... just to be sure, you can check the contents of certain rows, like you might want to check that row 12 has ---- in it or whatever.

Then, figure out which rows are important to you, and take the value read in from the Line Input# statement and put it either into a local variable, or into a structure like I mentioned.  Structures are nice, because after you collect the data into a structure, you could call another procedure to actually insert the data into the database, and just pass the variable defined as the user defined data type as a parameter....

Author Comment

ID: 6882326
I started working on it late I am hoping to get back to you on the results today.  But I think I might have this one now :=).

Never knew about using multiple input lines...That's what I love about all this - continue to learn something new.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now