# 2 Re: [RESOLVED] Loops within Loops within Loops within open File for input! Help!
Well, here's the code. lol.
Private Sub mnuImpDatabase_Click()
Cnt = 0
CommonDialog1.DialogTitle = "Select file to Import..."
CommonDialog1.Filter = "Text Documents(*.txt)|*.txt"
CommonDialog1.ShowOpen
If CommonDialog1.FileName <> "" Then
Open CommonDialog1.FileName For Input As #1
Do While Not EOF(1)
DoEvents
Line Input #1, dData
Form2.Caption = "Inputting File, Line #: " & Cnt
LnInfo(Cnt).UPC = Left(dData, 20)
dData = Right(dData, Len(dData) - 20)
LnInfo(Cnt).OrderNum = Left(dData, 20)
dData = Right(dData, Len(dData) - 20)
LnInfo(Cnt).Descript = Left(dData, 40)
dData = Right(dData, Len(dData) - 40)
LnInfo(Cnt).BatchNum = Left(dData, 12)
dData = Right(dData, Len(dData) - 12)
LnInfo(Cnt).Measure = Left(dData, 12)
dData = Right(dData, Len(dData) - 12)
LnInfo(Cnt).Quant = Left(dData, 8)
dData = Right(dData, Len(dData) - 8)
LnInfo(Cnt).Size = Left(dData, 6)
dData = Right(dData, Len(dData) - 6)
LnInfo(Cnt).ExDescript = Left(dData, 40)
dData = Right(dData, Len(dData) - 40)
LnInfo(Cnt).LabelType = Left(dData, 8)
dData = Right(dData, Len(dData) - 8)
LnInfo(Cnt).PrintAmt = Left(dData, 4)
dData = Right(dData, Len(dData) - 4)
LnInfo(Cnt).LikeCodes = Left(dData, 20)
dData = Right(dData, Len(dData) - 20)
dData = Right(dData, Len(dData) - 22)
LnInfo(Cnt).ProdType = Left(dData, 8)
dData = Right(dData, Len(dData) - 8)
LnInfo(Cnt).Price = Left(dData, 9)
dData = Right(dData, Len(dData) - 9)
LnInfo(Cnt).PriceSplit = Left(dData, 3)
dData = Right(dData, Len(dData) - 3)
LnInfo(Cnt).SaleEnds = Left(dData, 20)
dData = Right(dData, Len(dData) - 20)
LnInfo(Cnt).SaleBegi = Left(dData, 20)
dData = Right(dData, Len(dData) - 20)
LnInfo(Cnt).Department = Left(dData, 20)
dData = Right(dData, Len(dData) - 20)
For i = 0 To UBound(LnInfo)
DoEvents
If Cnt <> i Then
If LnInfo(i).UPC = LnInfo(Cnt).UPC Then 'same UPC
ReDim Preserve LnInfo(UBound(LnInfo) - 1)
Cnt = Cnt - 1
Exit For
End If
End If
Next i
ReDim Preserve LnInfo(UBound(LnInfo) + 1)
Cnt = Cnt + 1
Loop
ReDim Preserve LnInfo(UBound(LnInfo) - 1)
Close #1
Open App.Path & "\main.db" For Input As #1
Cnt = 0
Do While Not EOF(1)
Line Input #1, dData2
TruFalse = False
For i = 0 To UBound(LnInfo)
Form2.Caption = "Checking Against UPC #: " & i
DoEvents
dData = dData2
If LnInfo(i).UPC <> Left(dData, 20) Then
Else
TruFalse = True
End If
Next i
If TruFalse = False Then
ReDim Preserve LnInfo(UBound(LnInfo) + 1)
LnInfo(UBound(LnInfo)).UPC = Left(dData, 20)
dData = Right(dData, Len(dData) - 20)
LnInfo(UBound(LnInfo)).OrderNum = Left(dData, 20)
dData = Right(dData, Len(dData) - 20)
LnInfo(UBound(LnInfo)).Descript = Left(dData, 40)
dData = Right(dData, Len(dData) - 40)
LnInfo(UBound(LnInfo)).BatchNum = Left(dData, 12)
dData = Right(dData, Len(dData) - 12)
LnInfo(UBound(LnInfo)).Measure = Left(dData, 12)
dData = Right(dData, Len(dData) - 12)
LnInfo(UBound(LnInfo)).Quant = Left(dData, 8)
dData = Right(dData, Len(dData) - 8)
LnInfo(UBound(LnInfo)).Size = Left(dData, 6)
dData = Right(dData, Len(dData) - 6)
LnInfo(UBound(LnInfo)).ExDescript = Left(dData, 40)
dData = Right(dData, Len(dData) - 40)
LnInfo(UBound(LnInfo)).LabelType = Left(dData, 8)
dData = Right(dData, Len(dData) - 8)
LnInfo(UBound(LnInfo)).PrintAmt = Left(dData, 4)
dData = Right(dData, Len(dData) - 4)
LnInfo(UBound(LnInfo)).LikeCodes = Left(dData, 20)
dData = Right(dData, Len(dData) - 20)
dData = Right(dData, Len(dData) - 22)
LnInfo(UBound(LnInfo)).ProdType = Left(dData, 8)
dData = Right(dData, Len(dData) - 8)
LnInfo(UBound(LnInfo)).Price = Left(dData, 9)
dData = Right(dData, Len(dData) - 9)
LnInfo(UBound(LnInfo)).PriceSplit = Left(dData, 3)
dData = Right(dData, Len(dData) - 3)
LnInfo(UBound(LnInfo)).SaleEnds = Left(dData, 20)
dData = Right(dData, Len(dData) - 20)
LnInfo(UBound(LnInfo)).SaleBegi = Left(dData, 20)
dData = Right(dData, Len(dData) - 20)
LnInfo(UBound(LnInfo)).Department = Left(dData, 20)
dData = Right(dData, Len(dData) - 20)
End If
Loop
Close #1
dData = ""
For i = 0 To UBound(LnInfo)
Form2.Caption = "Working on UPC #: " & i & "/" & Cnt
DoEvents
If LnInfo(i).UPC = "" Or LnInfo(i).UPC = " " Then
Else
If dData = "" Then
dData = LnInfo(i).UPC & LnInfo(i).OrderNum & LnInfo(i).Descript & LnInfo(i).BatchNum & LnInfo(i).Measure & LnInfo(i).Quant & LnInfo(i).Size & LnInfo(i).ExDescript & LnInfo(i).LabelType & LnInfo(i).PrintAmt & LnInfo(i).LikeCodes & LnInfo(i).UPC & " 1" & LnInfo(i).ProdType & LnInfo(i).Price & LnInfo(i).PriceSplit & LnInfo(i).SaleEnds & LnInfo(i).SaleBegi & LnInfo(i).Department
Else
dData = dData & vbCrLf & LnInfo(i).UPC & LnInfo(i).OrderNum & LnInfo(i).Descript & LnInfo(i).BatchNum & LnInfo(i).Measure & LnInfo(i).Quant & LnInfo(i).Size & LnInfo(i).ExDescript & LnInfo(i).LabelType & LnInfo(i).PrintAmt & LnInfo(i).LikeCodes & LnInfo(i).UPC & " 1" & LnInfo(i).ProdType & LnInfo(i).Price & LnInfo(i).PriceSplit & LnInfo(i).SaleEnds & LnInfo(i).SaleBegi & LnInfo(i).Department
End If
End If
Next i
Form2.Caption = "Writing to Database File..."
Open App.Path & "\main.db" For Output As #1
Print #1, dData
DoEvents
Close #1
Form2.Caption = "Complete."
End If
End Sub
The first chunk of code inputs all of the information from the file to update the database with, while doing so it also makes sure there are no duplicates within itself. So I won't end up having around 60 entries in the array including the same data.
Then it opens the main database for input and brings it in line by line, checking to see if the UPC it just brought in is present within the array. If it is, it skips it. If it is not, then it adds it to the array. A potential problem for this is that since it actually adds it to the array it is searching through, it becomes exponentially longer depending on how many UPCs are being added.
Here is an example of a line from the text file being imported first. The whole file is generated by a program so it follows the same template.
2073511001 00066174TURKEY HILL VANILLA 1FZ 0.00 56.00 6UPBY1 1 ALE 1 1LIQUID 5.00 27/7/07 1ASADV 7
Oops, here's the declarations in the Module
Option Explicit
Type AdItem
ItemCode As Integer
Descript As String * 40
ExDescript As String * 40
UPC As String * 20
Size As String * 6
BatchNum As String * 12
OrderNum As String * 20
Measure As String * 12
Quant As String * 8
LabelType As String * 8
PrintAmt As String * 4
LikeCodes As String * 20
ProdType As String * 8
Seq As String * 2
Scan As String * 10
PriceSplit As String * 3
SaleEnds As String * 20
SaleBegi As String * 20
Department As String * 20
Price As String * 9
End Type
Public LnInfo() As AdItem
Public dData As String
Public ddData As String
Public dddData() As String
Public dData2 As String
Public TruFalse As Boolean
Public Cnt As Long
So anywho, thanks for any input.
The1nk at 2007-11-9 19:36:47 >
