Reputation: 2906
I am using Excel-VBA to insert data into a table. I have a .csv file that was parsed and used to create an insert string. I am using it on multiple tables in a row. The first table processes fine, but the second one gives me an error:
Run-time error '-2147217913 (8004e07)': Data type mismatch in criteria expression.
After researching this, it appears that I am trying to pass data in a bad form, but I'm not seeing where or how, as the first table processed. The final insert string that fails looks like:
INSERT INTO Table_Name VALUES ('Text_entry', 'Long_Integer_entry', 'Double_entry', '')
EDIT: I just tried another test and found the problem: any field that is a Double or Single Integer that gets passed the null '' causes the error. How do I pass a null value to these data types?
Upvotes: 0
Views: 1825
Reputation: 107767
In SQL, you can append and update NULL
in any data type column including string, numeric, or datetime values. So simply pass it in your append query. Be sure not to quote it as it is neither a string or numeric value. The NULL
entity means an unknown value.
INSERT INTO Table_Name VALUES ('Text_entry', 'Long_Integer_entry', 'Double_entry', NULL)
Specifically, your attempted ''
is actually a zero-length string (a non-NULL value) which is why it failed to work in a number column.
Should you use parameterized queries, also pass VBA's Null
to bind to SQL statement:
strSQL = "INSERT INTO Table_Name VALUES (?, ?, ?, ?)"
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = conn
.CommandText = strSQL
.CommandType = adCmdText
.CommandTimeout = 15
End With
' BINDING PARAMETERS
cmd.Parameters.Append _
cmd.CreateParameter("textparam", adVarChar, adParamInput, 255, "Text_entry")
cmd.Parameters.Append _
cmd.CreateParameter("longtxtparam", adVarChar, adParamInput, 255, "Long_Integer_entry")
cmd.Parameters.Append _
cmd.CreateParameter("doubleparam", adDouble, adParamInput, , "Double_entry")
cmd.Parameters.Append _
cmd.CreateParameter("nullparam", adDouble, adParamInput, , Null)
Upvotes: 1