我的项目是打开一个文本文件并循环遍历数据,当找到合适的数据时我们希望将该行数据插入表中。有四种不同类型的数据可以插入到四个不同的表中。
下面是我需要循环的示例数据以及我的代码。问题是我的代码没有选择任何TRAN数据并调试打印它。感谢阅读和帮助非常感谢。
$TRAN,001003659,0,2,0,2,2550,04/01/10,09:14:00,6
$PAYM,3,Credit Card,2800,2550,0,0,0
$ITEM,00006110,18,1,0,1275,A,,,4,0,0,1275,,0
$ITEM,00006110,18,1,0,1275,A,,,4,0,0,1275,,0
$NOSALE,001003660,04/01/10,09:18:34,6
$TRAN,001003661,23,9,3,13,7010,04/01/10,15:14:27,5
Private Sub Command0_Click() Open "Sharp Sales\TRMSAVE01.txt" For Input As #1 Dim TRAN As String TRAN = "%Tran%" Do Until EOF(1) Line Input #1, varTran Line Input #1, varPaym Line Input #1, varItem Line Input #1, varNosale strTran = Mid(varTran, 4, Len(varTran) - 10) strPaym = Mid(varPaym, 4, Len(varPaym) - 10) strItem = Mid(varItem, 4, Len(varItem) - 10) strNosale = Mid(varNosale, 4, Len(varNosale) - 10) If TRAN = varTran Then Debug.Print varTran DoCmd.RunSQL Transql = " Insert into Transaction (Tran ID, 1, 2, 3, 4, 5, Date, Time, 6)Value ('" & strTran & "', '" & strPaym & "', '" & strItem & "', '" & strNosale & "')" End If Loop Close #1 End Sub
答案 0 :(得分:0)
Line Input
将始终返回下一行!
因此,Line Input #1, varNosale
实际上将返回您的第二个$ ITEM,而不是$ NOSALE行。
然后在第二个循环中,您的第一个语句Line Input #1, varTran
将读取第5行,这是$ NOSALE而不是TRAN,因为您正在检查!
更好的方法就是这个伪代码:
Dim blnFirst As Boolean
blnFirst = True
Do Until EOF(1)
Line Input strLine
Select Case Left(strLine, 5)
Case "$TRAN":
If Not blnFirst Then
Execute insert SQL here, using strTranID, strItemIds, etc.
End If
strTranID = ...
strItemIds = ""
blnFirst = False
Case "$PAYM": strPayment = ...
Case "$ITEM": strItemIds = strItemIds & ... 'maybe use an array here
Case "$NOSA": strNoSale = ...
End Select
Loop