Aren't the field names also required?
In help, form2 of the "Insert Into" example shows:
INSERT INTO target [(field1[, field2[, ...]])]
VALUES (value1[, value2[, ...])
so shouldn't strSQL be:
strSQL = "Insert Into myTable ([field1], [field2]) Values (" & _
IIf(IsNull(rst.Fields(0)), "Null", Chr$(34) & _
rst.Fields(0) & Chr$(34)) & _
", " & rst.Fields(1) & ")"
If I substitute "Test" for rst.fields(0) and 1000 for rst.Fields(1) and
run
the code, I get the run-time error 3346:
"Number of query Values and Destination fields are not the same"
Am I missing something? (wouldn't be the first time <g>)
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)
Douglas J. Steele said:
Actually, you're right: that will work.
(And after looking at it, I'm surprised that mine worked, because I would
have thought that the word Null was explicitly required. However, this
was
one of the times when I did explicitly test before posting!)
--
Doug Steele, Microsoft Access MVP
(no e-mails, please!)
I've found that this also will work:
strSQL = "Insert Into myTable Values (" & _
IIf(IsNull(rst.Fields(0)), "Null", Chr$(34) & _
rst.Fields(0) & Chr$(34)) & _
", " & rst.Fields(1) & ")"
--
Ken Snell
<MS ACCESS MVP>
message
You can insert Null values: you simply use the keyword Null (or a
variable that contains a Null value).
The problem is, you can't have quotes around the Null.
Assuming that the first field in myTable is capable of accepting Null
values, the following will work:
strSQL = "Insert Into myTable Values (" & _
IIf(IsNull(rst.Fields(0)), Null, Chr$(34) & rst.Fields(0) &
Chr$(34)) & _
", " & rst.Fields(1) & ")"
--
Doug Steele, Microsoft Access MVP
(no e-mails, please!)
You can't insert a null value and you can only insert a zero length
string if
the field properties allow it. Why not just tst for it, and exclude
it
from
the insert if the value to insert is null?
:
When the following VBA Code is executed I get a run-time error:
strSQL = "Insert Into myTable Values (" & _
Chr$(34) & rst.Fields(0) & Chr$(34) & ", " & _
rst.Fields(1) & ")"
dbs.Execute strSQL
The following run-time error occurs when the strSQL is executed:
Run-time error '3134':
Syntax error in INSERT INTO statement.
I believe the Null value in "rst.Fields(1)" is causing the problem
since
when code "Nz(rst.Fields(1), 0)" makes the error go away.
Is there any way to preserve the Null value when Inserting into a
table?
Thanks, Mark