【发布时间】:2014-01-02 18:40:00
【问题描述】:
这是我第一次开发数据库,也是第一次使用 MS Access!我正在尝试通过链接到查询的表单将 1 条新记录附加到表中。
Private Sub cmdAdjustStock_Click()
'Declare Vars
Dim newqty As Long
Dim Qty As Control
Dim change As Control
Dim BoxType As Control
Dim sql As String
'Set form controls to vars
Set Qty = Forms!formMain!txtQty
Set change = Forms!formMain!txtQtyChange
Set BoxType = Forms!formMain!txtBoxType
'Arithmetic and SQL
newqty = Qty + change
sql = "INSERT INTO tblHistory (BoxType, QtyChange, NewQty) VALUES ('&BoxType&','&change&','&newqty&')"
MsgBox "New Quantity = " & newqty & ", Box Type = " & BoxType 'For Debugging
DoCmd.RunSQL sql
End Sub
“tblHistory”有以下字段:PID、logDate、BoxType、QtyChange、NewQty。必填项。 "logDate" 默认值 = Date() 并且 PID 是自动编号。 “tblHistory”目前没有记录,这个追加将是第一个!
“BoxType”与包含 BoxType(Primary)的主列表及其相应数量的表“tblBoxList”的一对多关系的多端。
My MsgBox 显示正确的数量值和表单中的 BoxType(即“RMA-834”)与“tblBoxList”上的主 BoxType ('RMA-834') 匹配。
通过 MsgBox 验证数据后,Append 由于 1 个密钥违规而失败。我假设此违规与 PID(这是一个自动增量编号)或从表单传递的 BoxType 以某种方式与 tblBoxList 中的主节点不匹配有关。这令人困惑,因为 MsgBox 显示的值看起来与 Primary 相同。
仅供参考:此项目中的表已链接。我在网络上的“后端”中有 3 个表。这是只有表单的“前端”。
【问题讨论】:
标签: sql ms-access ms-access-2007 vba