3

I have an MS Access ADP with a SQL server backend. I want to take a bunch of values from a form and insert them into a table. Some of the values can be null. How do I use vba to insert null into an integer field in the SQL Server table?

I tried

"insert...values(" & nz(me.myInt, null) & ",..."

and

"insert...values(" & nz(me.myInt, "null") & ",..."

Neither worked.

1
  • the thing that i'm running into is your nz... if me.myint IS null... isnt it enough just to put THAT value in w/o the nz around it?.. unless me is inside its own class module... then any property of me cannot be null.. Commented Nov 3, 2010 at 17:19

3 Answers 3

4

IF the field will accept nulls then simply leave it out of the list of fields and it will get a null value. So given a table with Name, Address, phone, doing the equivalent of

INSERT table(Name, Address) VALUES('fred','Toytown')

will leave the phone number with a null value

Sign up to request clarification or add additional context in comments.

Comments

3

your second example looks good

("insert...values(" & nz(me.myInt, "null") & ",...")

but only if me.myInt is a variant, a control, or a field. If myInt is an integer or a long, then it cannot be null.

Comments

1

what error are you getting when you run your code?

say we have table... [test] with columns [id], [test] and [test 2].

you could write;

insert into [test] ([test], [test 2]) values ( null, null)

make sure that your string that you are passing the command in on shows the null w/o any kind of decoration... no single or double quotes.

1 Comment

I found a solution: 1) Add a numeric field to the table with null default value (called NullValue) 2) if you want to assign null to another field: rst!Field = rst!NullValue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.