将表格信息添加到表中 [英] Add Form Information to a Table

查看:67
本文介绍了将表格信息添加到表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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条错误消息.因此,起初一切似乎都工作正常.表格中输入的所有信息都被添加到我的加班表中的正确列中.问题是在关闭并重新打开数据库后发生的.我真的不确定如何从这一点开始.

Hello, I have created a Form and a Table in Microsoft Access 2010. The Form is called pbicovertime it has five unbound text boxes which all have unique names and three buttons. I would like the information that has been entered in the Form to be added to the Table called Overtime when the Add button is pressed. The code above does add the data from the Form to the table, however I get a Run-timer error '3061": Too few parameters. Expected 1 error message after closing and reopening the database. So initially everything seemed to be working fine. All the information entered in the Form was being added to the correct column in my Overtime Table. The issue took place after closing and reopening the database. I am not really sure how to proceed from this point.

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

FYI this is my first time working with Forms in Access !

推荐答案

以记录集的形式打开表并添加一行.这样可以避免因要添加的值中必需的/缺少的引号或日期分隔符而引起的麻烦.

Open your table as a recordset and add a row. That will avoid complications based on required/missing quotes or date delimiters in the values you're adding.

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之间的行之一上引发错误,因此您应该能够快速确定哪个名称拼写错误.

If the original missing parameter error was because of a misspelled field name, this code will throw an error on one of the lines between AddNew and Update, so you should be able to quickly identify which name is misspelled.

注意:始终在代码模块的声明"部分中包含Option Explicit.然后从VB编辑器的主菜单中运行调试"->编译".在花时间对代码进行故障排除之前,请更正编译器抱怨的所有内容.

Note: Always include Option Explicit in the Declarations sections of your code modules. And then run Debug->Compile from the VB Editor's main menu. Correct anything the compiler complains about before you spend time troubleshooting the code.

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

I don't know what Me.Refreshenter is. It looks like a misspelling of Me.Refresh. If so, that is something Option Explicit will warn you about. However, if you wanted Refresh, I suggest you substitute Me.Requery. The reason is that Refresh will pull in changes to any of the existing rows in the form's recordset, but not newly added rows. Requery gets new rows in addition to changes to existing rows.

这篇关于将表格信息添加到表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆