希望这是有道理的。我已经修改了一个代码,允许我为发票创建一个报表输入表单。
原始代码:http://windowssecrets.com/forums/sho...cess-2000-SR-1)
我有一个子表单(从tbl_RepeatTemp创建),只有一个字段: ScheduleDate
对于在此子表单中输入的每一行,应在主表中输入一行。这是当前正在运行的代码:
Private Sub btn_CreateInvoices_Click()
'------------------------------------------------------------
Dim StrSQL As String
Dim RecordIDValue As Long
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim rst As DAO.Recordset
Dim fld As DAO.Field
Set dbs = CurrentDb
Set tdf = dbs.TableDefs("tbl_RepeatTemp")
Set rst = tdf.OpenRecordset
'Find the next RecordID to use
RecordIDValue = DMax("[RecordID]", "[tbl_MainData]") + 1
RowIDValue = DMax("[RowID]", "[tbl_MainData]")
UpdateCount = 0
If Me.FormProformaStatus = "" Then myStatus = "" Else myStatus = "Scheduled to Raise"
' Loop through the acheduled dates entered
Do Until rst.EOF
For Each fld In rst.Fields
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'Insert fields in to tbl_MainData
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
StrSQL = ""
StrSQL = StrSQL & " INSERT INTO tbl_MainData"
StrSQL = StrSQL & " (RecordID, ScheduledDate, InvoiceID, ProformaID, TransactionType, InputBy, InputDate, ProjectCode, ServiceCode, InvoiceValue, RPIIncrease, Terms, Status, InvFreq)"
StrSQL = StrSQL & " VALUES"
StrSQL = StrSQL & " (" & RecordIDValue & ", #" & Format(fld.Value, "Medium Date") & "#, '" & Me.FormInvoiceID & "', '" & Me.FormProformaID & "', 'Invoice', '" & Me.FormInputBy & "', #" & Format(Me.InputDate, "Medium Date") & "#, '" & Me.FormProjectCode & "', '" & Me.FormProductCode & "', " & Me.FormInvoiceValue & ", '" & Me.RPIIncrease & "', '" & Me.FormTerms & "','" & myStatus & "', '" & Me.FormInvFreq & "')"
Debug.Print StrSQL 'Print values to immediate window for debugging
CurrentDb.Execute StrSQL 'Execute SQL
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'Insert fields into tbl_DebtTracker
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'UpdateRow ID to match tbl_MainData
RowIDValue = RowIDValue + 1
StrSQL = ""
StrSQL = StrSQL & " INSERT INTO tbl_DebtTracker"
StrSQL = StrSQL & " (RowID,RecordID, ProjectCode, InvoiceID)"
StrSQL = StrSQL & " VALUES"
StrSQL = StrSQL & " (" & RowIDValue & ", " & RecordIDValue & ", '" & Me.FormProjectCode & "', '" & Me.FormInvoiceID & "');"
Debug.Print StrSQL 'Print values to immediate window for debugging
CurrentDb.Execute StrSQL 'Execute SQL
UpdateCount = UpdateCount + 1
Next fld
rst.MoveNext
Loop
rst.Close
MsgBox UpdateCount & " Records successfully created.", , "Success !"
Set rst = Nothing
Set fld = Nothing
Set tdf = Nothing
Set dbs = Nothing
Me.Refresh
End Sub
我现在想在tbl_Repeat临时表
中再添加2个字段RPI增加替换Me.RPIIncrease 将RPID作为新字段
从我所看到的变量fld在有一个字段时起作用 - 这就是为什么ScheduledDate是fld.value - 我怎么能添加其他2个字段?我附上了我的子表单的图像,这可能有助于可视化我在做什么
答案 0 :(得分:3)
我认为rst
中字段的嵌套循环会使得很难有效地构建SQL字符串。我建议只改写这样的整个循环(删除fld
循环并显式引用rst
记录集中的每个字段):
Do Until rst.EOF
StrSQL = ""
StrSQL = StrSQL & " INSERT INTO tbl_MainData"
StrSQL = StrSQL & " (RecordID, ScheduledDate, myNewField1, myNewField2, InvoiceID, ProformaID, TransactionType, InputBy, InputDate, ProjectCode, ServiceCode, InvoiceValue, RPIIncrease, Terms, Status, InvFreq)"
StrSQL = StrSQL & " VALUES"
StrSQL = StrSQL & " (" & RecordIDValue & ", #" & rst![Schedule Date] & "#, '" & rst!myNewField1 & "', '" & rst!myNewField2 & "', '" & Me.FormInvoiceID & "', '" & Me.FormProformaID & "', 'Invoice', '" & Me.FormInputBy & "', #" & Format(Me.InputDate, "Medium Date") & "#, '" & Me.FormProjectCode & "', '" & Me.FormProductCode & "', " & Me.FormInvoiceValue & ", '" & Me.RPIIncrease & "', '" & Me.FormTerms & "','" & myStatus & "', '" & Me.FormInvFreq & "')"
Debug.Print StrSQL
CurrentDb.Execute StrSQL
RowIDValue = RowIDValue + 1
StrSQL = ""
StrSQL = StrSQL & " INSERT INTO tbl_DebtTracker"
StrSQL = StrSQL & " (RowID,RecordID, ProjectCode, InvoiceID)"
StrSQL = StrSQL & " VALUES"
StrSQL = StrSQL & " (" & RowIDValue & ", " & RecordIDValue & ", '" & Me.FormProjectCode & "', '" & Me.FormInvoiceID & "');"
Debug.Print StrSQL
CurrentDb.Execute StrSQL
UpdateCount = UpdateCount + 1
rst.MoveNext
Loop
要使用2个新字段进行此操作,您显然需要先将它们添加到 tbl_MainData 和 tbl_RepeatTemp 表中。