dbl*_*blE 5 ms-access ms-access-2007
如果我插入的变量的值为Null,则SQL INSERT INTO将失败.
变量costLab是Variant数据类型.非Null值将为Decimal.
db.Execute ("INSERT INTO budget
(wbsid, category, capture_date, budget_date, budget_type, month_value) " & _
"VALUES ('" & wbsid & "', 'Labor', #" & importDate & "#,
#" & monthDate & "#, 'Forecast', " & costLab & ");")
Run Code Online (Sandbox Code Playgroud)
如果设置costLab的代码返回Null,我想插入Null.但是如果返回一个值,我想插入该值.显然我可以编写一个If语句来检查null然后直接插入"Null",但我想知道是否有一种方法没有If语句通过变量插入Nulls.
您可以Nz()在构建INSERT语句时使用.它与IIf()方法类似,但稍微简洁一些.
Dim strInsert As String
strInsert = "INSERT INTO budget (wbsid, category, capture_date, budget_date, budget_type, month_value) " & _
"VALUES ('" & wbsid & "', 'Labor', #" & importDate & "#, #" & monthDate & "#, 'Forecast', " & Nz(costLab, 'Null') & ");"
Debug.Print strInsert ' <- examine the finshed statement in Immediate window
db.Execute strInsert, dbFailOnError
Run Code Online (Sandbox Code Playgroud)
Debug.Print让您有机会检查您为db引擎执行的语句.如果遇到问题,您可以转到立即窗口(Ctrl+ g)查看语句文本.您还可以复制该文本并将其粘贴到新查询的SQL视图中以进行测试.