ms access - INSERT INTO fails if inserting a variable with the value of Null -
the sql insert fail if variable inserting has value of null.
the variable costlab variant data type. non-null value decimal.
db.execute ("insert budget (wbsid, category, capture_date, budget_date, budget_type, month_value) " & _ "values ('" & wbsid & "', 'labor', #" & importdate & "#, #" & monthdate & "#, 'forecast', " & costlab & ");")
i want insert null if code sets costlab returns null. if value returned want insert value. write if statement checks null insert "null" directly wanted know if there way without if statement insert nulls via variable.
you use nz()
when build insert
statement. it's similar iif()
approach, more concise.
dim strinsert string strinsert = "insert budget (wbsid, category, capture_date, budget_date, budget_type, month_value) " & _ "values ('" & wbsid & "', 'labor', #" & importdate & "#, #" & monthdate & "#, 'forecast', " & nz(costlab, 'null') & ");" debug.print strinsert ' <- examine finshed statement in immediate window db.execute strinsert, dbfailonerror
debug.print
gives opportunity examine finished statement give db engine execute. in case of trouble, can go immediate window (ctrl+g) view statement text. can copy text , paste sql view of new query testing.
Comments
Post a Comment