VBA:为什么我的 INSERT 代码不起作用?

2023-12-13

几周前我就开始工作了,但现在我不确定我做了什么导致它不再工作了。我什至没有收到错误消息来弄清楚可能出了什么问题。当我单击在表中插入行的按钮时,没有任何反应。表单被清除并重新查询表,但代码的 INSERT 部分不执行任何操作。

Public Sub Command125_Click()
    'Add row for downtime

    Dim dbsCurrent As Database
    Set dbsCurrent = CurrentDb

    dbsCurrent.Execute " INSERT INTO tbl_Downtime " _
    & "(job, suffix, production_date, reason, downtime_minutes, comment, shift) VALUES " _
    & "('" & Me.Text116 & "','" & Me.Text118 & "','" & Me.Text126 & "','" & Me.Text121 & "','" & Me.Text123 & "','" & Me.Text128 & "','" & Me.Text144 & "');"

   Call ClearControl(Me.Text116)
   Call ClearControl(Me.Text118)
   Call ClearControl(Me.Text126)
   Call ClearControl(Me.Text121)
   Call ClearControl(Me.Text123)
   Call ClearControl(Me.Text128)
   Call ClearControl(Me.Text144)

   Me.subrpt_DowntimeTable.Requery

End Sub

我正在尝试的代码基于@Hambone 的答案:

Public Sub Command125_Click()

Dim dbsCurrent As Database
Set dbsCurrent = CurrentDb

Dim query As QueryDef
    Dim sql As String

    For Each query In CurrentDb.QueryDefs
      If query.Name = "InsertDowntime" Then
        Exit For
      End If
    Next query

    If query Is Nothing Then
      sql = "parameters " & _
        "P1 text, P2 text, P3 Date, P4 Text, P5 Number, P6 Text, P7 Text;" & _
        "insert into [tbl_Downtime] " & _
        "(job, suffix, production_date, reason, downtime_minutes, comment, shift) " & _
        " VALUES ([P1], [P2], [P3], [P4], [P5], [P6], [P7])"

      Set query = CurrentDb.CreateQueryDef("InsertDowntime", sql)
    End If

    query.Parameters("P1").Value = "test1"
    query.Parameters("P2").Value = "test2"
    query.Parameters("P3").Value = Now()
    query.Parameters("P4").Value = "test3"
    query.Parameters("P5").Value = 15
    query.Parameters("P6").Value = "Miles O'Brien is a darn good transporter chief"
    query.Parameters("P7").Value = "test6"

    query.Execute

    MsgBox query.Parameters("P1").Value & query.Parameters("P2").Value & query.Parameters("P3").Value & query.Parameters("P4").Value & query.Parameters("P5").Value & query.Parameters("P6").Value & query.Parameters("P7").Value

    Me.subrpt_DowntimeTable.Requery

End Sub

MarkB 和 gmiley 关于使用参数的说法绝对正确。前期需要多编写一些代码,之后可以节省无数的时间。而且,这是一个很好的实践。

也就是说,对于本机 Access 查询(不是 ADO 数据库查询),它并不是世界上最直接的过程。在我看来,普通的 ADO 东西在你做了一两次之后就开始有意义了,但是对于 Access 查询,我仍然必须回去抄袭旧的示例才能让它工作。

就你而言,我认为这样的事情可以解决问题:

  Dim query As QueryDef
  Dim sql As String

  For Each query In CurrentDb.QueryDefs
    If query.Name = "InsertDowntime" Then
      Exit For
    End If
  Next query

  If query Is Nothing Then
    sql = "parameters " & _
      "P1 text, P2 text, P3 Date, P4 Text, P5 Number, P6 Text, P7 Text;" & _
      "insert into [tbl_Downtime] " & _
      "(job, suffix, production_date, reason, downtime_minutes, comment, shift) " & _
      " VALUES ([P1], [P2], [P3], [P4], [P5], [P6], [P7])"

    Set query = CurrentDb.CreateQueryDef("InsertDowntime", sql)
  End If

  query.Parameters("P1").Value = "test1"
  query.Parameters("P2").Value = "test2"
  query.Parameters("P3").Value = Now()
  query.Parameters("P4").Value = "test3"
  query.Parameters("P5").Value = 15
  query.Parameters("P6").Value = "Miles O'Brien is a darn good transporter chief"
  query.Parameters("P7").Value = "test6"

  query.Execute

您正在从文本框中提取数据。我使用硬编码值来证明,如果您的值不全是文本,这也可以管理数据键入。无需“引用”文本或#hash#日期。您显然可以将它们改回Me.TextBox123并更改数据类型以匹配您的实际字段tbl_Downtime.

-- 编辑 2015 年 12 月 3 日 --

整个代码段来自For Each query In CurrentDb.QueryDefs一路之前query.Parameters could 理论上如果您已经有一个包含此查询文本的查询,则可以省略(例如您进入 Access,创建一个查询,从设计视图转到 SQL 视图,然后输入该查询并将其命名InsertDowntime):

parameters
P1 text, P2 text, P3 Date, P4 Text, P5 Number, P6 Text, P7 Text;
insert into [tbl_Downtime]
(job, suffix, production_date, reason, downtime_minutes, comment, shift)
VALUES ([P1], [P2], [P3], [P4], [P5], [P6], [P7])

因为你不知道,所以我只是通过代码创建了它。如果您尝试再次创建它,Access 会呕吐,因为InsertDowntime已经存在。

不管怎样,一旦它存在,你管理它的方法就是说

Dim query As QueryDef
Set query = CurrentDb.QueryDefs("InsertDowntime")

然后其他一切should就如我所拥有的那样。

就我个人而言,我会选择选项 2——在 Access 中创建查询并将其保留为持久对象,然后按照我上面列出的方式访问它。我想我可以这么说,但你的问题是一个 VBA 式的问题,我保留了 VBA ——另外,我认为动态创建查询的能力有点酷。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

VBA:为什么我的 INSERT 代码不起作用? 的相关文章

随机推荐