G
Guest
Twice a week I receive a Delimited file with 350,000 records and 200 fields
in each record. I want to import all of the records, but only some of the
fields. I want to do this via the vba Split Function, without a
Specification file (ie; with using TransferText). Problem is, I'm getting
each record multiple times. I'm sure it's because of my "For" statement.
But without it, I get RunTime Error 9 (Subscript out of range).
Following is the sample Delimited file and code:
Delimited File (JEG06.txt)
A1,A2,A3,A4,A5
B1,B2,B3,B4,B5
C1,C2,C3,C4,C5
==========================================================
Private Sub cmdDelimited_Click()
DoCmd.Hourglass True
Dim Rst As DAO.Recordset
Set Rst = CurrentDb.OpenRecordset("Delimited")
Do Until Rst.EOF
With Rst
.MoveFirst
.Delete
.MoveNext
End With
Loop
Rst.Close
Set Rst = Nothing
Dim rs1 As DAO.Recordset
Dim FileName As String
Dim FileDate As Date
Dim FileNum As Integer
Dim strFilter As String
Dim strFileDesc As String
Dim strFileExt As String
Dim strRecord As String
Dim strRecordArray() As String
Dim intCount As Integer
Dim InputString
strFilter = ahtAddFilterItem(strFilter, strFileDesc, strFileExt)
strFileDesc = "Text Files (*.txt)"
strFileExt = "JEG06*.txt"
DoCmd.Hourglass False
FileName = ahtCommonFileOpenSave( _
Filter:=strFilter, _
InitialDir:="Y:\HR", _
OpenFile:=True, _
DialogTitle:="Select File for Import... JEG06.txt", _
Flags:=ahtOFN_HIDEREADONLY)
FileDate = FileDateTime(FileName)
FileNum = FreeFile()
Set rs1 = CurrentDb.OpenRecordset("Delimited")
DoCmd.Hourglass True
Open FileName For Input As #FileNum
Do While Not (EOF(FileNum))
Line Input #FileNum, InputString
strRecordArray = Split(InputString, ",", 5)
For intCount = LBound(strRecordArray) To UBound(strRecordArray)
rs1.AddNew
rs1.Fields("PersID") = strRecordArray(0)
rs1.Fields("NameL") = strRecordArray(1)
rs1.Fields("NameF") = strRecordArray(2)
rs1.Fields("MI") = strRecordArray(3)
rs1.Fields("SSN") = strRecordArray(4)
rs1.Update
' Exit For
Next
Loop
rs1.Close
Close #FileNum
DoCmd.Hourglass False
MsgBox "File Import Complete", vbInformation, "Import Status"
End Sub
=============================================
Any help will be greatly appreciated.
in each record. I want to import all of the records, but only some of the
fields. I want to do this via the vba Split Function, without a
Specification file (ie; with using TransferText). Problem is, I'm getting
each record multiple times. I'm sure it's because of my "For" statement.
But without it, I get RunTime Error 9 (Subscript out of range).
Following is the sample Delimited file and code:
Delimited File (JEG06.txt)
A1,A2,A3,A4,A5
B1,B2,B3,B4,B5
C1,C2,C3,C4,C5
==========================================================
Private Sub cmdDelimited_Click()
DoCmd.Hourglass True
Dim Rst As DAO.Recordset
Set Rst = CurrentDb.OpenRecordset("Delimited")
Do Until Rst.EOF
With Rst
.MoveFirst
.Delete
.MoveNext
End With
Loop
Rst.Close
Set Rst = Nothing
Dim rs1 As DAO.Recordset
Dim FileName As String
Dim FileDate As Date
Dim FileNum As Integer
Dim strFilter As String
Dim strFileDesc As String
Dim strFileExt As String
Dim strRecord As String
Dim strRecordArray() As String
Dim intCount As Integer
Dim InputString
strFilter = ahtAddFilterItem(strFilter, strFileDesc, strFileExt)
strFileDesc = "Text Files (*.txt)"
strFileExt = "JEG06*.txt"
DoCmd.Hourglass False
FileName = ahtCommonFileOpenSave( _
Filter:=strFilter, _
InitialDir:="Y:\HR", _
OpenFile:=True, _
DialogTitle:="Select File for Import... JEG06.txt", _
Flags:=ahtOFN_HIDEREADONLY)
FileDate = FileDateTime(FileName)
FileNum = FreeFile()
Set rs1 = CurrentDb.OpenRecordset("Delimited")
DoCmd.Hourglass True
Open FileName For Input As #FileNum
Do While Not (EOF(FileNum))
Line Input #FileNum, InputString
strRecordArray = Split(InputString, ",", 5)
For intCount = LBound(strRecordArray) To UBound(strRecordArray)
rs1.AddNew
rs1.Fields("PersID") = strRecordArray(0)
rs1.Fields("NameL") = strRecordArray(1)
rs1.Fields("NameF") = strRecordArray(2)
rs1.Fields("MI") = strRecordArray(3)
rs1.Fields("SSN") = strRecordArray(4)
rs1.Update
' Exit For
Next
Loop
rs1.Close
Close #FileNum
DoCmd.Hourglass False
MsgBox "File Import Complete", vbInformation, "Import Status"
End Sub
=============================================
Any help will be greatly appreciated.