如何在忽略没有数据的字段的同时插入表?

Posted

技术标签:

【中文标题】如何在忽略没有数据的字段的同时插入表?【英文标题】:How to insert into a table while ignoring fields with no data? 【发布时间】:2019-02-21 01:31:20 【问题描述】:

我不想向表中插入数据,但并非所有字段都需要填写,有些字段是可选的。

CurrentDb.Execute "INSERT INTO tbl_Records_of_Violation(Serial_Number,Badge,Rank,Lastname,Firstname,Middlename,Qualifier,Gender,Birthdate,Date_Ent_Svc,Address,MotherUnit,Unit,Date_of_Commission,Reporting_Officer,Report_to_Supervisor,Approving_Authority,Punishment,Remarks,Violation1) VALUES ('" & Me.Serial_Number & "','" & Me.Badge & "','" & Me.Rank & "','" & Me.Lastname & "','" & Me.Firstname & "','" & Me.Middlename & "','" & Me.Qualifier & "','" & Me.Gender & "','" & Me.Birthdate & "','" & Me.Date_Ent_Svc & "','" & Me.Address & "','" & Me.MotherUnit & "','" & Me.Unit & "','" & Me.Date_of_Commission & "','" & Me.Reporting_Officer & "','" & Me.Report_to_Supervisor & "','" & Me.Approving_Authority & "','" & Me.Punishment & "','" & Me.Remarks & "'," & Me.Violation1 & ")"

MsgBox "Record has been updated", vbInformation, "Record Information"

我希望即使其中一个字段为空,它也会保存在我的表格中。

【问题讨论】:

我不确定我是否理解这个问题,但参数化查询不是更好吗?这样一来,您就可以为缺少但不需要的任何内容提供空值。 您似乎也在使用表单。那么为什么不直接将表单绑定到表格呢?然后你可以用更少的代码做更多的事情,它会按照你需要的方式工作。 【参考方案1】:

表 tbl_Records_of_Violation 有 20 列数据被此命令插入。

这 20 列中的每一列都应定义为 Nullable,

并且每个屏幕值(Me.xxx)都需要有有效数据或空值。

【讨论】:

以上是关于如何在忽略没有数据的字段的同时插入表?的主要内容,如果未能解决你的问题,请参考以下文章

插入表时如何避免数据不一致

如何在oracle数据表中一次性插入多条记录?

如何在oracle数据表中一次性插入多条记录?

SQL 表已经建立好了 如何在字段中插入GUID数据

如何在Rails中使数据库字段为只读?

Django 1.4:如何忽略 loaddata 中的字段、模型