Private Sub InsertMisclAddInTranCons()
'Insert the Debit/Credit tran of Miscl Exp into the Tran Consolidated table under the Sale voucher group header//:
Dim SQLTranCons As String = "Select * From TranConsolidated"
Dim DATranCons As New OleDb.OleDbDataAdapter
Dim DSTranCons As New DataSet
Dim TCNewRowDebit As DataRow
Dim TCNewRowCredit As DataRow
DATranCons = New OleDb.OleDbDataAdapter(SQLTranCons, Con)
DATranCons.Fill(DSTranCons, "DSTranConsHere")
'Dim Row As DataRow
Dim I As Integer
' MsgBox(DSMisclExpDetTemp.Tables(0).Rows.Count)
For I = 0 To DSMisclExpDetTemp.Tables(0).Rows.Count - 1
Dim SQLDebitMax As String = "SELECT Max(TranConsolidated.ID) AS MaxOfID FROM TranConsolidated;"
Dim DADebitMax As New OleDb.OleDbDataAdapter
Dim DSDebitMax As New DataSet
Dim DebitMax As Double
DADebitMax = New OleDb.OleDbDataAdapter(SQLDebitMax, Con)
DADebitMax.Fill(DSDebitMax, "DSDebitMaxHere")
DebitMax = DSDebitMax.Tables(0).Rows(0).Item(0)
For Each Row In DSMisclExpDetTemp.Tables(0).Rows
TCNewRowDebit = DSTranCons.Tables(0).NewRow
With TCNewRowDebit 'This block will create the debit row//
.Item(0) = DebitMax + 1
If txtIfEditingSaved.Text = "" Then
.Item(1) = txtTranNo.Text
Else
.Item(1) = txtIfEditingSaved.Text
End If
.Item(2) = txtInvNumber.Text
.Item(3) = dtpSaleInvoice.Value
.Item(4) = txtComboAccount.Text 'Inserts the Debit account selected in the voucher
.Item(5) = DSMisclExpDetTemp.Tables(0).Rows(I).Item(2) 'Amount is entered here
.Item(6) = DSMisclExpDetTemp.Tables(0).Rows(I).Item(4) 'This is the opposite account ID which will be used for the ledger display from TranConsolidated.
.Item(10) = 2 'This is a sub transaction of sales tran
.Item(11) = frmAddMisclinSale.txtTranSubType.Text
End With
DSTranCons.Tables(0).Rows.Add(TCNewRowDebit)
'Credit Entry===
'Get the max ID for credit//:
Dim SQLCreditMax As String = "SELECT Max(TranConsolidated.ID) AS MaxOfID FROM TranConsolidated;"
Dim DACreditMax As New OleDb.OleDbDataAdapter
Dim DSCreditMax As New DataSet
Dim CreditMax As Double
DACreditMax = New OleDb.OleDbDataAdapter(SQLCreditMax, Con)
DACreditMax.Fill(DSCreditMax, "DSCreditMaxHere")
CreditMax = DSCreditMax.Tables(0).Rows(0).Item(0)
TCNewRowCredit = DSTranCons.Tables(0).NewRow
With TCNewRowCredit
.Item(0) = CreditMax + 1
If txtIfEditingSaved.Text = "" Then
.Item(1) = txtTranNo.Text
Else
.Item(1) = txtIfEditingSaved.Text 'THIS NUMBER IS INCORRECTLY INSERTED.Check
End If
.Item(2) = txtInvNumber.Text
.Item(3) = dtpSaleInvoice.Value
.Item(7) = DSMisclExpDetTemp.Tables(0).Rows(I).Item(4)
.Item(8) = DSMisclExpDetTemp.Tables(0).Rows(I).Item(2) 'Amount is entered here
.Item(9) = txtComboAccount.Text ' 'This is the opposite account ID which will be used for the ledger display from TranConsolidated. 'Inserts the Debit account selected in the voucher
.Item(10) = 2 'This is a sub transaction of sales tran
.Item(12) = frmAddMisclinSale.txtTranSubType.Text
End With
DSTranCons.Tables(0).Rows.Add(TCNewRowCredit)
' Loop
Next I
End Sub
在上面的代码中,我必须将一些项目保存到表中(TranConsolidated)。根据其他表中的记录数,我计算表中的行,然后多次运行循环。 请注意,在目标表(TranConsolidated)中,我必须创建不是AutoNumber的主数据库,并通过获取表中最后一条记录的最大数量然后向其中添加1来获取每个新密钥。
但是,我得到一个" Insert Into"语法错误。注意我已经反复检查了表中的任何系统关键字并找到了none.Also,还有另一个地方可以保存到同一个表中没有任何问题。 即使在这里,我之前已经能够成功保存,但在设置代码以在每次迭代时插入新的主键时遇到了问题。
哈立德。