【问题标题】:Add Form Information to a Table将表单信息添加到表中
【发布时间】:2013-12-18 09:12:55
【问题描述】:
Option Compare Database

Private Sub cmdAdd_Click()
    CurrentDb.Execute "INSERT INTO Overtime(Todays_Date, Employee_Name, " & _
        "Start_Date, End_Date,Comments) " & _
        " VALUES(" & Me.txtCurrentday & ",'" & Me.txtName & "','" & _
        Me.txtBegin & "','" & Me.txtEnd & "','" & Me.txtComment & "')"

    Me.Refreshenter        
    cmdClear_Click
End Sub

Private Sub cmdClear_Click()
    Me.txtCurrentday = ""
    Me.txtName = ""
    Me.txtBegin = ""
    Me.txtEnd = ""
    Me.txtComment = ""

    Me.txtCurrentday.SetFocus
End Sub

Private Sub cmdClose_Click()
    DoCmd.Close
End Sub

您好,我在 Microsoft Access 2010 中创建了一个表单和一个表格。该表单名为 pbicovertime,它有五个未绑定的文本框,它们都有唯一的名称和三个按钮。我希望在按下“添加”按钮时将已在表单中输入的信息添加到名为“加班”的表中。上面的代码确实将表单中的数据添加到表中,但是我得到一个运行计时器错误“3061”:参数太少。关闭并重新打开数据库后预期会出现 1 条错误消息。所以最初一切似乎都工作正常。表单中输入的所有信息都已添加到我的加班表中的正确列中。问题发生在关闭并重新打开数据库后。我不确定如何从这一点开始。

仅供参考,这是我第一次在 Access 中使用表单!

【问题讨论】:

    标签: forms ms-access vba


    【解决方案1】:

    将表作为记录集打开并添加一行。这将避免基于您要添加的值中所需/缺少的引号或日期分隔符的复杂情况。

    Option Compare Database
    Option Explicit ' <- add this
    
    Private Sub cmdAdd_Click()
        Dim db As DAO.database
        Dim rs As DAO.Recordset
        Set db = CurrentDb
        Set rs = db.OpenRecordset("Overtime",  dbOpenTable, dbAppendOnly)
        With rs
            .AddNew
            !Todays_Date = Me.txtCurrentday
            !Employee_Name = Me.txtName
            !Start_Date = Me.txtBegin
            !End_Date = Me.txtEnd
            !Comments = Me.txtComment
            .Update
            .Close
        End With
        'Me.Refreshenter ' <- what is this?
        cmdClear_Click
    End Sub
    

    如果最初的缺少参数错误是由于字段名称拼写错误,则此代码将在AddNewUpdate 之间的一行中抛出错误,因此您应该能够快速识别哪个名称拼写错误。

    注意:始终在代码模块的声明部分中包含 Option Explicit。然后从 VB 编辑器的主菜单运行 Debug->Compile。在花时间对代码进行故障排除之前,纠正编译器抱怨的任何问题。

    我不知道Me.Refreshenter 是什么。它看起来像是Me.Refresh 的拼写错误。如果是这样,那是Option Explicit 会警告你的事情。但是,如果您想要Refresh,我建议您替换为Me.Requery。原因是Refresh 将拉入对表单记录集中任何现有行的更改,而不是新添加的行。 Requery 除了对现有行进行更改外,还获取新行。

    【讨论】:

    • 我已经替换了您的新代码并在表格中添加了一行数据。我收到运行时错误 3251 Operation is not supported for this type of object。在代码的 .AddNew 行上。
    • 好的,我刚刚意识到我为OpenRecordset 建议了错误的选项。请尝试更新版本。
    • 似乎运行良好!感谢您的帮助!
    【解决方案2】:

    我敢打赌,它崩溃的就是这条线。

    CurrentDb.Execute "INSERT INTO Overtime(Todays_Date, Employee_Name, " & _
        "Start_Date, End_Date,Comments) " & _
        " VALUES(" & Me.txtCurrentday & ",'" & Me.txtName & "','" & _
        Me.txtBegin & "','" & Me.txtEnd & "','" & Me.txtComment & "')"
    

    特别是Me.txtCurrentday,因为它将被评估为纯文本,并且根据您的 PC 设置方式,它可能会混淆 SQL。例如,它可能看起来像这样:

    INSERT INTO Overtime(Todays_Date, Employee_Name, Start_Date, End_Date,Comments) 
    VALUES ( Dec 1, 2013, 'JoeSmith', 'Jan 1, 2013', 'Dec 31, 2013', 
    'Some important comment');
    

    您应该在#'s 中包含日期:

    INSERT INTO Overtime(Todays_Date, Employee_Name, Start_Date, End_Date,Comments) 
    VALUES ( #Dec 1, 2013#, 'JoeSmith', #Jan 1, 2013#, #Dec 31, 2013#, 
    'Some important comment');
    

    它会变得更顺畅。同样以这种方式构建 SQL 会使您容易受到注入(作为攻击或错误)的攻击。想象一下,如果评论是“这是 Susie 的工作”,在这种情况下,额外的撇号会弄乱插入内容。

    【讨论】:

      猜你喜欢
      • 2016-09-12
      • 2021-12-25
      • 1970-01-01
      • 2020-07-25
      • 2014-08-16
      • 1970-01-01
      • 2021-11-15
      • 2021-10-24
      • 1970-01-01
      相关资源
      最近更新 更多