【发布时间】:2013-08-20 13:08:58
【问题描述】:
在 MS Access 中,我有一个未绑定的表单,其中包含两个格式为“短日期”的字段,因此我可以让日期选择器显示(Access 2007)。 单击按钮时,使用 vba 中的此 sql 查询将数据插入到表中
strInsert = "INSERT INTO atbl_invoice (cust_id, inv_date, job_no, item_desc, job_desc, type, fday, reqday) " & _
"VALUES(" & strCid & ", Date(),'" & Me.JobNo & "','" & Me.item_desc & "','" & Me.job_desc & "','" & Me.cboType & "','" & Me.fday & "','" & Me.reqday & "')"
表中的 fday 和 reqday 是时间/日期字段,设置为“required=No”。
如果填写了表单上的两个日期字段,一切正常,但如果其中任何一个为空,我会收到“由于类型转换失败而将 1 字段设置为 Nul”错误,因为它试图将在日期/时间字段中输入一个空字符串。
有没有人知道我可以将这些字段留空并仍然让我的插入查询工作的方法?
感谢您的帮助。 如果有人想知道,这就是我最终的做法。
Dim strInsert0 As String
Dim strInsert1 As String
Dim strInsert2 As String
Dim strInsert3 As String
Dim strCid As String
strCid = Forms!frmCustomer!cid
strInsert0 = "INSERT INTO atbl_invoice (cust_id, inv_date, job_no, item_desc, job_desc, type) " & _
"VALUES(" & strCid & ", Date(),'" & Me.JobNo & "','" & Me.item_desc & "','" & Me.job_desc & "'," & _
"'" & Me.cboType & "')"
strInsert1 = "INSERT INTO atbl_invoice (cust_id, inv_date, job_no, item_desc, job_desc, type, fday) " & _
"VALUES(" & strCid & ", Date(),'" & Me.JobNo & "','" & Me.item_desc & "','" & Me.job_desc & "'," & _
"'" & Me.cboType & "','" & Me.fday & "')"
strInsert2 = "INSERT INTO atbl_invoice (cust_id, inv_date, job_no, item_desc, job_desc, type, reqday) " & _
"VALUES(" & strCid & ", Date(),'" & Me.JobNo & "','" & Me.item_desc & "','" & Me.job_desc & "'," & _
"'" & Me.cboType & "','" & Me.reqday & "')"
strInsert3 = "INSERT INTO atbl_invoice (cust_id, inv_date, job_no, item_desc, job_desc, type, fday, reqday) " & _
"VALUES(" & strCid & ", Date(),'" & Me.JobNo & "','" & Me.item_desc & "','" & Me.job_desc & "'," & _
"'" & Me.cboType & "','" & Me.fday & "','" & Me.reqday & "')"
If IsNull(Me.reqday) And Not IsNull(Me.fday) Then
DoCmd.RunSQL strInsert1
ElseIf IsNull(Me.fday) And Not IsNull(Me.reqday) Then
DoCmd.RunSQL strInsert2
ElseIf IsNull(Me.reqday) And IsNull(Me.fday) Then
DoCmd.RunSQL strInsert0
Else: DoCmd.RunSQL strInsert3
End If
有两个日期字段,其中任何一个都可能为空,这有点复杂。
【问题讨论】:
标签: ms-access datetime sql-insert string