Private Sub 数量_LostFocus()
On Error GoTo err_数量_lostfocus
Me![进货金额] = Me![数量] * Me![进货价]
exit_数量_LostFocus:
Exit Sub
err__数量_LostFocus:
MsgBox Err.Description
Resume exit_数量_LostFocus
End Sub
Private Sub 提交_Click()
On Error GoTo err_提交_click
Dim i, j As Integer
Dim strtemp As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim rs1 As ADODB.Recordset
Set rs1 = New ADODB.Recordset
Dim rs2 As ADODB.Recordset
Set rs2 = New ADODB.Recordset
strtemp = "select * from 进货单"
rs.Open strtemp, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
strtem = "select * from 进货单历史"
rs1.Open strtemp, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
strtem = "select * from 库存资料"
rs2.Open strtemp, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
'如果"进货单"为空,则退出
If rs.RecordCount <= 0 Then
Exit Sub
End If
rs.MoveFirst
For i = 0 To rs.RecordCount - 1
cunzai = False
If rs2.RecordCount < 1 Then
cunzai = False
Else
'判断记录库存中是否存在,是则更新库存信息
rs2.MoveFirst
For j = 0 To rs2.RecordCount - 1
If rs2("产品ID") = rs("产品ID") Then
rs2("库存数量") = rs2("库存数量") + rs("数量")
rs2("库存单价") = rs2("进货价")
rs2("库存金额") = rs2("库存数量") * rs2("库存单价")
rs2.Update
cunzai = True
Exit For
Else
rs2.MoveNext
End If
Next j
End If
'不存在则添加新记录
If cunzai = False Then
rs2.AddNew
rs2("产品ID") = rs("产品ID")
rs2("产品名称") = rs("产品名称")
rs2("单位") = rs("单位")
rs2("仓库") = rs("仓库")
rs2("库存数量") = rs("库存数量")
rs2("库存单价") = rs("库存单价")
rs2("库存金额") = rs2("库存数量") * rs2("库存单价")
rs2.Update
End If
'保存进货单到历史表中
rs1.AddNew
rs1("供应商ID") = Me![供应商ID]
rs1("供应商名称") = Me![供应商名称]
rs1("进货日期") = Me![进货日期]
rs1("单号") = Me![单号]
rs1("仓库") = Me![仓库]
rs1("备注") = Me![备注]
rs1("制单人") = Me![制单人]
rs1("产品ID") = rs("产品ID")
rs1("产品名称") = rs("产品名称")
rs1("单位") = rs("单位")
rs1("数量") = rs("数量")
rs1("进货价") = rs("进货价")
rs1("进货金额") = rs("进货金额")
rs1.Update
'删除进货单该条记录
rs.Delete 1
Me![单据查询 子窗体].Requery
rs.MoveNext
Next i
Set rs = Nothing
Set rs1 = Nothing
Set rs2 = Nothing
Me![金额总计] = Null
MsgBox "进货单已经提交成功", vbInformation, "提交成功"
exit_提交_click:
Set rs = Nothing
Set rs1 = Nothing
Set rs2 = Nothing
Exit Sub
err_提交_click:
MsgBox Err.Description
Resume exit_提交_click
End Sub