【问题标题】:Insert into query from form when date field is empty日期字段为空时从表单插入查询
【发布时间】: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


    【解决方案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
    

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

    【讨论】:

    • 我希望不会出现这种情况,因为有两个日期字段意味着更长的 If 语句,但如果这是必需的,我将不得不咬紧牙关。
    • 我会选择这个答案,尽管我会使用案例选择,因为我需要考虑两者是否为空,如果只有一个为空,如果只有另一个为空,以及它们是否为空都满了。谢谢。
    【解决方案2】:

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

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

    插入 mytable (mydata) 值 (NULL)

    【讨论】:

      猜你喜欢
      • 2012-02-28
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-13
      • 2018-05-31
      • 1970-01-01
      • 2013-04-16
      相关资源
      最近更新 更多