【问题标题】:Insert into Access junction table with VBA使用 VBA 插入 Access 联结表
【发布时间】:2021-10-17 06:26:46
【问题描述】:

我有三张桌子:

tbl拒绝 RejectionID, Period, TescoYear, TescoWeek, RejectionDate, Packsize, TPNB, TPND, BatchCode, ProductID

tblReason ReasonID, ReasonDesc, Quantity, CasesInspected, InspectedPacks, PercentageAffected, Display until

tblRejectionReason RejectionReasonID(自动编号)、RejectionID、ReasonID

想法是可以添加拒绝,并且可以添加拒绝的原因。

RejectionReason 表的用途是因为拒绝可以有多个拒绝理由。

我只是想插入在 tblRejection 和 tblReason 中创建的 ID 作为每个表各自主键的外键值。

testSQL1 和 testSQL2 工作正常。但是testSQL3并没有将两个表的主键值作为外键值插入到链接表中。

谁能告诉我 testSQL3 有什么问题?

Dim testSQL1 As String
Dim testSQL2 As String
Dim testSQL3 As String

testSQL1 = "INSERT INTO tblRejection (period, tescoyear, tescoweek, rejectiondate, packsize, TPNB, TPND, batchcode, productID) VALUES ('x','xxxx','xx','xxxxx','xxxxx','xxxxxxx','xxxxxx','xxxxxx',1);"
testSQL2 = "INSERT INTO tblReason (reasondescription, quantity, casesinspected, inspectedpacks, affectedpacks, percentageaffected, displayuntil) VALUES ('zzzzzzz',20,100,50,20,'xxxx','xxxxxx');"

testSQL3 = "INSERT INTO tblRejectionReason (rejectionid, reasonid) " & _
            '"SELECT tblrejection.rejectionid, tblreason.reasonid " & _
            '"FROM tblReason " & _
            '"INNER JOIN (tblRejection INNER JOIN tblRejectionReason ON tblRejection.RejectionID = tblRejectionReason.RejectionID) " & _
            '"ON tblReason.ReasonID = tblRejectionReason.ReasonID;"        

Debug.Print testSQL1
DoCmd.RunSQL testSQL1
Me.Refresh

Debug.Print testSQL2
DoCmd.RunSQL testSQL2
Me.Refresh

Debug.Print testSQL3
DoCmd.RunSQL testSQL3
Me.Refresh`

【问题讨论】:

  • 为什么使用 VBA 和 SQL 而不是绑定表单?为什么使用静态值的 SQL?
  • 我首先使用静态值进行测试。我有一个表单,用户将填写并通过文本框和组合框将静态值替换为表单中的值

标签: sql vba ms-access


【解决方案1】:

JOIN 没有意义 - 删除它们。只需要FROM tblReason, tblRejection。这会导致记录的笛卡尔关联。每个表的每条记录都与其他表的每条记录相关联,以产生每个可能的 ID 字段配对。

将tblRejectionReason中的rejectionID和reasonID字段设置为复合索引,防止重复对。

【讨论】:

  • 这个查询有什么问题? "INSERT INTO tblRejectionReason (RejectionID, ReasonID)" & _ "SELECT tblRejection.RejectionID, tblReason.ReasonID" & _ "FROM tblRejection, tblReason;"
  • 您到底想完成什么?如果您希望 tblRejectionReason 从其他 2 个表中获得所有可能的 ID 字段配对,我的建议就是这样做。如图所示,删除 JOIN 并更改 FROM。 JOIN 没有任何意义,因为 tblRejectionReason 中没有要加入的记录,如果有,则无需插入 ID。研究笛卡尔积查询,以geeksforgeeks.org/sql-join-cartesian-join-self-join开始
  • 我想完成这个。 tblRejection RejectionID, 日期 1, 07/07/2021 2, 07/07/2021 3, 11/07/2021 4, 11/07/2021 tblRejectionReason RejectionReasonID, RejectionID, ReasonID 1, 1, 1 2, 1, 2 3, 3, 3 4, 3, 4 tblReason ReasonID, ReasonComment, 数量 1, 发现问题 - 颜色 (级别: 2), 65 2, 发现问题 - 冰冻损坏 (级别: 2), 90 3, 发现问题 - 腐烂和霉菌 ( Level: 3/4), 125 4, 发现问题 - Rots (Level: 3&4), 108
  • 拒绝可能有一个或多个理由被拒绝的情况。我计划让用户首先出于一个原因填写所有信息。然后创建一个搜索功能,以便能够为现有拒绝添加更多原因。链接到我想要实现的目标:ibb.co/RY9YtVb
  • 不会改变我的 cmets 或建议。
【解决方案2】:

我去重新设计了我的数据库,因为我把它弄得太复杂了。

现在我只有两个表,主键和外键以及一个表单和子表单。

不需要多对多。

【讨论】:

    猜你喜欢
    • 2012-07-11
    • 2012-07-26
    • 1970-01-01
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多