我有數據庫表需要使用表單進行更新。 但是,表格中的所有字段都不必強制填寫。 我正在寫下面的VB代碼插入然而,我得到一個錯誤,說明在語句中有語法錯誤。據我所知,這是因爲變量中的空值。我知道我需要爲所有的空值使用DBNull.Value。 這裏的事情是有太多的字段來檢查值是否爲空。 任何機構都建議是否有辦法對輸入值爲空的值進行批量檢查?如何在插入查詢中忽略訪問中的空值
VBCode:
Dim StrSQL As String
Dim StrSQL1 As String
Dim tktID As Variant
Dim Assi As Variant
Dim reopened As Variant
Dim valid As Variant
Dim Reopenreason As Variant
Dim ReassignmentAG As Variant
Dim RBSCollab As Variant
Dim SMEconf As Variant
Dim Cloabag As Variant
Dim smeName As Variant
Dim Updat As Variant
Dim Closed As Variant
Dim iss As Variant
Dim ana As Variant
Dim res As Variant
Dim rVariant As Variant
' Assigining values
tktID = Ticket_number.Value
reopened = Ticket_Reopened.Value
valid = Valid_Reopen.Value
Assi = Assiginee.Value
Reopenreason = Reopen_reason.Value
ReassignmentAG = Reassignment_AG.Value
RBSCollab = RBS_Collab.Value
SMEconf = CkBxSMEConfirmation.Value
Cloabag = Collabarated_AG.Value
smeName = SME_Name.Value
Updat = Update.Value
Closed = Issue_Closed.Value
iss = Issue.Value
ana = Analysis.Value
res = Resolution.Value
rdate = Resolve_date.Value
'Insert values into the tables
StrSQL = "INSERT INTO Updates (TicketID,Assiginee,ReassignmentAG,RBSCollab,CollabAG,SMEconfirmation,SMEName,Update,IssueClosed,Issue,Analysis,Resolution,ResolveDate,TicketReopened,ValidReopen,ReopenReason) VALUES ('" & tktID & "','" & Assi & "','" & ReassignmentAG & "','" & RBSCollab & "','" & Cloabag & "','" & SMEconf & "','" & smeName & "','" & Updat & "','" & Closed & "','" & iss & "','" & ana & "','" & res & "','" & rdate & "','" & reopened & "','" & valid & "','" & Reopenreason & "') "
DoCmd.RunSQL StrSQL
在此先感謝您的幫助
最好學會使用參數來避免sql注入和格式化問題。 – LarsTech
我有很多字段,你可以。我不使用參數將有助於 – shravya
你誤解了你會使用參數的原因。考慮一下'foo'的'Reopenreason'會發生什麼) DROP TABLE更新; - '。 – Comintern