Tip

Import Formated Text File As You Want !

If you want to import a formated text file and make from it documents, the
easyest way is to create a view and use the import basic tool of Notes. But
this is not very convinient.
What I propose is a Class that handle everything to import AND export documents
from and to text file.

HOW DOES IT WORKS :

_ Methods:
ImportAll
ExportAll
ExportOneDoc
GetFirstRecord
GetNextRecord
New
Delete

-Creation
To create a new SeqFile object, use the New method :
-Syntax:
Dim variableName As New SeqFile(fileName , openMode , fields , fieldType ,
separator , dstart , dstop)
or
Set notesDocument = New SeqFile(fileName , openMode , fields , fieldType ,
separator , dstart , dstop)

-Parameters:
fileName: Path & file name of the text file (string)
openMode: Type of action to perform. Values are IMPORT or EXPORT
fields: array of field name to export (in order) (string())
fieldType: array of the type of the fields sp?cified in "fields" (same order)
(string())
separator: a string to specify the field separator
dstart : delimiter to begin a field (string)
dstop : delimiter to end a field (string)

------------------
ImportAll method

Import all records of a text file as defined in a configuration document.
Multiple values are allowed.

Syntax :
Call SeqFile.ImportAll(form as string)
Parameters :
in: form, form to use to create the documents

----------------
ExportAll method

Export all records of a text file as defined in the SeqFile Object.

Syntax:
Call SeqFile.ExportAll ( docCollection as NotesDocumentCollection )

Parameters:
in: docCollection, collection of documents to export

-------------------
ExportOneDoc method

Export one document in the a text file as a record.

Syntax:
Call SeqFile.ExportOneDoc( doc as NotesDocument )
Parameters:
in: doc, document to export

---------------------
GetFirstRecord method

Get the first Record in the text file and converts in a NotesDocument

Syntax:
Set document = SeqFile.GetFirstRecord()
Parameters: nothing
Return value:The converted document (NotesDocument)

--------------------
GetNextRecord method

Get the next Record in the text file and converts in a NotesDocument

Syntax:
Set document = SeqFile.GetNextRecord()
Parameters: nothing
Return value:
The converted document (NotesDocument)
Const IMPORT = 1
Const EXPORT = 2
Const ERRORTitle = "SeqFile Component"

Class SeqFile

' Properties _________________________________________
'#################################################
Private fileNum As Integer
Private fileName As String
Private fieldList As Variant
Private fieldTypeList As Variant
Private fieldSeparator As String
Private delimiterStart As String
Private delimiterStop As String
'#################################################
' ------------------------------------------------------------------

' Methods __________________________________________
'#################################################
Private Sub Extract(doc As NotesDocument, bound As Integer, txt As String)
' Extract a field in the text file
' if typeVal = 0 => Text field to import
' = 1 => number field
' = 2 => date field

Dim item As NotesItem
Dim extractField As Variant
Dim newValue As Variant
Dim arrayValues() As Variant
Dim begin As Integer
Dim numtot As Integer
Dim lenStart As Integer
Dim lenStop As Integer
Dim lenField As Integer
Dim max As Integer
Dim i As Integer

extractField = LeftExt(txt, Me.fieldSeparator)
lenField = Len( extractField )
lenStart = Len( Me.delimiterStart )
lenStop = Len( Me.delimiterStop )

' amputation of the extracted field
begin = lenField + Len( Me.fieldSeparator ) + 1
numtot = Len( txt )
txt = Mid$( txt, begin, numtot )

' remove the delimiters
extractField = Mid$( extractField, lenStart + 1, lenField - lenStop -
lenStart )

Select Case fieldTypeList(bound)
Case "0" : newValue = Trim$(extractField)
Case "1" : newValue = Val(extractField)
Case "2" : newValue = Cdat(extractField)
End Select

' Add or create the item's value
Set item = doc.GetFirstItem(fieldList(bound))
If item Is Nothing Then
Call doc.ReplaceItemValue(fieldList(bound), newValue)
Else
max = Ubound(item.Values)
Redim arrayValues (max + 1)
For i = 0 To max
arrayValues(i) = item.Values(i)
Next
arrayValues(max + 1) = newValue
item.Values = arrayValues
End If
End Sub
'
--------------------------------------------------------------------------------
------------------
Public Sub ImportAll(form As String)
' Import all records of a text file as defined in a configuration document.

Dim session As New NotesSession
Dim dbcur As NotesDataBase
Dim newDoc As NotesDocument
Dim i As Integer
Dim record As String

If Ubound(fieldList) <> Ubound(fieldTypeList) Then
Messagebox "Error : The field list has not the same lenght as the field type
list.", 16, ERRORTitle
Exit Sub
End If

Set dbcur = session.CurrentDataBase

' ---- For each record we create a doc
Do Until Eof(Me.filenum)
Line Input #Me.filenum, record ' extract a line in text file

Set newDoc = dbcur.CreateDocument
newDoc.Form = form

'----------------- EXTRACT FIELDS ----------------------------
For i = 0 To Ubound( fieldList )
Call Extract(newdoc, i, record)
Next

Call newDoc.Save(True,False)

Loop
End Sub
'-------------------------------------------------------------------------------
-------
Public Sub ExportAll(docCollection As NotesDocumentCollection)
' Export all records of a text file as defined in a configuration document

Dim session As New NotesSession
Dim dbcur As NotesDataBase
Dim doc As NotesDocument
Dim item As NotesItem
Dim i As Integer
Dim j As Integer
Dim max As Integer
Dim record As String

This was first published in November 2000

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.