0
在MS Access中,我有一個未綁定的表單,其中包含兩個字段,其格式爲「短日期」,因此我可以讓日期選擇器顯示(Access 2007) 。 上的按鈕的點擊,數據被插入到表使用該SQL查詢在VBA當日期字段爲空時插入到表單中的查詢中
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在表中的時間/日期字段和被設置爲「需要=否」。
如果表單上的兩個日期字段被填充,但如果其中任何一個留空,我會得到一個「set 1 fields to Nul due to type conversion failure」錯誤,因爲它試圖將一個空字符串放入日期/時間字段中。
有沒有人知道一種方式,我可以離開這些領域的空白,仍然有我的插入查詢工作?
感謝您的幫助。 如果有人想知道,我就是這麼做的。
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
有兩個日期字段,其中任何一個可能已經空或不復雜一點。
我希望這不會是這樣,有兩個日期字段意味着更長的If語句,但如果這是必需的,我將不得不硬着頭皮。 – Almeister9
我要回答這個問題,雖然我會使用一個case select,因爲如果兩個都是空的,如果只有一個是空的,如果只有另一個是空的,並且它們都滿了,我需要考慮。謝謝。 – Almeister9