在 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
有两个日期字段,其中任何一个都可能是空的,也可能不是,它有点复杂。