0

在 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

有两个日期字段,其中任何一个都可能是空的,也可能不是,它有点复杂。

4

2 回答 2

1

我不确定,但我相信你不能存储空值。如果我是你,我会运行一个 if 语句来检查它们是否为空。查看下面的代码:

    if len(me.fday.value) > 0  and len(me.reqday.value) > 0 then
          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 & "')"
    else
        strInsert = "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 & "')"
    end if

看看这是否有效,如果没有,请评论答案并告诉我。

于 2013-08-20T14:01:52.030 回答
-2

在 sql 字符串中使用以下查询:

插入 mytable (mydata) 值 (#01/01/2020#)

或者

插入 mytable (mydata) 值 (NULL)

于 2019-11-21T09:49:57.997 回答