我需要使用表单将许多数据字段添加到表中

Posted

技术标签:

【中文标题】我需要使用表单将许多数据字段添加到表中【英文标题】:How to add many fields of data to a table using a form 【发布时间】:2021-12-24 23:43:00 【问题描述】:

我正在尝试创建一个大约需要填写的表单。 50个字段的数据以ms访问成一张表。该函数长时间输入为一行,当我尝试换行时,它不断给我以下错误:“INSERT INTO 语句中的语法错误”。有没有不同的方法来写这个。应该在表单中按下提交按钮时调用代码。

表单应该将数据存储到我称为“Performance_Reports”的表中

Private Sub addRecord_Click()

DoCmd.RunSQL "Insert into Performance_Reports" _
& "(Report_Id, Badge_Id, Review_Date, Progression, Overall_Rating, Next_Review, Emp_Expertise/Job_Knowledge, Emp_Quality, Emp_Dependability, Emp_Delivering_Solutions, Emp_EH&S, Emp_Continuous_Improvement, Emp_Initiative, Emp_Teamwork, EMp_Time_Management, Emp_Productivity, Emp_Lead_Self, Emp_Inspire_and_Empower, Emp_Achieve_Results, Emp_Drive_Change_&_Innovation, Emp_Builds_Trust, Emp_Ethics, Strengths/Achievements, Opportunities_for_Development, Area_Preference_1, Area_Preference_2, Area_Preference_3, Area_Preference_4, Job_Rotation_History, Emp_Assessment, Summary, Supervisor_Comments, Employee_Comments, Emp_Signature_Date, Sup_Signature_Date, Sup_Expertise/Job_Knowledge, Sup_Quality, Sup_Dependability, Sup_Delivering_Solutions, Sup_EH&S, Sup_Continuous_Improvement, Sup_Initiative, Sup_Teamwork, Sup_Time_Management, Sup_Productivity, Sup_Lead_Self, Sup_Inspire_and_Supower, Sup_Achieve_Results, Sup_Drive_Change_&_Innovation, Sup_Builds_Trust, Sup_Ethics, Sup_Assessment) VALUES" _
& "(reportRecord, Badge_ID, reviewDate, progOpt, avgRating, nextReview, empExpertise, empQuality, empDependability, empSolutions, empImpact, empEhs, empImprovement, empInitiative, empTeam, empTime, empProduct, empLead, empInspire, empAchieve, empTrust, empDrive, empEthics, txtStrengths, txtOpportunities, area1, area2, area3, area4, txtHistory, empAssess, txtSummary, txtSup, txtEmp, empDateSig, supDateSig, supExpertise, supQuality, supDependability, supSolutions, supImpact, supEhs, supImprovement, supInitiative, supTeam, supTime, supProduct, supLead, supInspire, supAchieve, supTrust, supDrive, supEthics, supAssess)"

End Sub

【问题讨论】:

How to debug dynamic SQL in VBA -- 你会看到问题。 您为什么不只是基于表格创建一个表单并使用它?执行此操作需要零代码。 【参考方案1】:

首先,你错过了空格和括号:

Private Sub addRecord_Click()

    DoCmd.RunSQL "Insert into Performance_Reports " & _
    "(Report_Id, Badge_Id, Review_Date, Progression, Overall_Rating, Next_Review, [Emp_Expertise/Job_Knowledge], Emp_Quality, Emp_Dependability, Emp_Delivering_Solutions, Emp_EH&S, Emp_Continuous_Improvement, Emp_Initiative, Emp_Teamwork, EMp_Time_Management, Emp_Productivity, Emp_Lead_Self, Emp_Inspire_and_Empower, Emp_Achieve_Results, [Emp_Drive_Change_&_Innovation], Emp_Builds_Trust, Emp_Ethics, [Strengths/Achievements], Opportunities_for_Development, Area_Preference_1, Area_Preference_2, Area_Preference_3, Area_Preference_4, Job_Rotation_History, Emp_Assessment, Summary, Supervisor_Comments, Employee_Comments, Emp_Signature_Date, Sup_Signature_Date, [Sup_Expertise/Job_Knowledge], Sup_Quality, Sup_Dependability, Sup_Delivering_Solutions, Sup_EH&S, Sup_Continuous_Improvement, Sup_Initiative, Sup_Teamwork, Sup_Time_Management, Sup_Productivity, Sup_Lead_Self, Sup_Inspire_and_Supower, Sup_Achieve_Results, [Sup_Drive_Change_&_Innovation], Sup_Builds_Trust, Sup_Ethics, Sup_Assessment) VALUES " & _
    "(reportRecord, Badge_ID, reviewDate, progOpt, avgRating, nextReview, empExpertise, empQuality, empDependability, empSolutions, empImpact, empEhs, empImprovement, empInitiative, empTeam, empTime, empProduct, empLead, empInspire, empAchieve, empTrust, empDrive, empEthics, txtStrengths, txtOpportunities, area1, area2, area3, area4, txtHistory, empAssess, txtSummary, txtSup, txtEmp, empDateSig, supDateSig, supExpertise, supQuality, supDependability, supSolutions, supImpact, supEhs, supImprovement, supInitiative, supTeam, supTime, supProduct, supLead, supInspire, supAchieve, supTrust, supDrive, supEthics, supAssess)"

End Sub

接下来,所有值都必须与 SQL连接。查看我的函数CSql。

但是,您最好使用DAO,打开一个记录集,然后使用AddNewUpdate 方法插入记录,这样代码会更简洁。

【讨论】:

【参考方案2】:

根据您要填充到表格中的字段数量,我建议您使用表单向导创建一个绑定表单,并将您要插入的字段作为要添加到表单的表格中的选定字段。

除非您在客户端-服务器架构中工作/访问文件通过网络共享并且您担心进程缓慢,即使这样单个访问文件也可以在网络上正常工作(使用绑定表单)有五个用户访问它。

所以使用绑定表单功能,然后转到控制面板并选择 一个命令按钮,将它放在表单上,​​按照将提示的向导并选择记录选项,然后添加记录,通过给命令按钮一个有意义的标题来完成向导。

【讨论】:

以上是关于我需要使用表单将许多数据字段添加到表中的主要内容,如果未能解决你的问题,请参考以下文章

在 ReactJS 中将表单数据提交到表中

将表单条目保存到变量并添加到表中

Laravel错误将数据从另一个表插入到表中

MS Access 2016:如何将连接字段从表单输入到表中

SQL将计算列添加到表中

MS Access 2003 - Unbound Form 使用 INSERT 语句保存到表中;子表单呢?