Read fixed width text file *resolved/open to tips*
I'm starting a task where I'm going to need to read a fixed length text file and write it to SQL 2000 which I haven't done before. I don't want to start from scratch and run into the normal pitfalls if I can help it. Can someone point me to some existing code or a good tutorial on it? The SQL part is not a problem. What I'm looking for is a way to predefine the fields and the lengths to read into. I've run across snippets of code in my search but not a working model.
EDITED ***
Just in case someone else searches on this topic this is what I did and it seems to work. I'm open to constructive criticism and could use some tips on error handling for this. I just grabbed pieces from here and there to put it together. This is just the basic starting point to my task so if any one does have some tips now is the time.
VB Code:
Private strBWCClaimNumber As String
Private strMCONumber As String
Private strNoteCreateDate As String
Private strNoteTitle As String
Private strAuthorUserid As String
Private strAuthorLastName As String
Private strAuthorFirstName As String
Private strNoteID As String
Private strNotePrimary As String
Private strNoteContinuation As String
Public Type NotesExport
BWCClaimNumber As String * 10 'BWCClaimNum
MCONumber As String * 5 '10005
NoteCreateDate As String * 10 'mm/dd/yyyy format
NoteTitle As String * 40 'Subject or Category of Note
AuthorUserid As String * 30 'Note Creator Userid
AuthorLastName As String * 30 'Last Name from refUserInfo
AuthorFirstName As String * 20 'First Name from refUserInfo
NoteID As String * 50 '10005 + pkNote
NotePrimary As String * 4012 'Primary text of note
NoteContinuation As String * 4012 'Continuation text of note
End Type
Private NR As NotesExport
Public Sub ImportNotes()
Dim iFile As Integer
Dim L As Long
iFile = FreeFile()
Open frmMain.txtImport.Text & frmMain.lvImport.SelectedItem.SubItems(1) For Random As #iFile Len = Len(NR)
For L = 1 To Int(LOF(iFile) / Len(NR))
Get #iFile, L, NR
strBWCClaimNumber = NR.BWCClaimNumber
strMCONumber = NR.MCONumber
strNoteCreateDate = NR.NoteCreateDate
strNoteTitle = NR.NoteTitle
strAuthorUserid = NR.AuthorUserid
strAuthorLastName = NR.AuthorLastName
strAuthorFirstName = NR.AuthorFirstName
strNoteID = NR.NoteID
strNotePrimary = NR.NotePrimary
strNoteContinuation = NR.NoteContinuation
DoEvents
Next
Close #iFile
End Sub
Re: Read fixed width text file
Have you looked at simply using the BULK INSERT command that SQL 2000 has?
We have little TEXTIMPORT programs we have developed here - they take fixed-width TEXT files and load them into tables. We analyze the table first to see how wide each column is and then use MID() to cutup each line read from the TEXT file and build an INSERT statement for each line and execute that. We have .INI files that control and override certain things during these INSERT's...