views:

311

answers:

4

I'm trying to run code that will copy fields into a new table, moving them from a _New table to the original table. The VBA code that does this works as such:

SQLStatement = CStr("INSERT INTO " & TName & " SELECT * FROM " & TName & "_New")
Log.WriteLine "Running query with string """ & SQLStatement & """ "
QueryTimer = Timer
DoCmd.RunSQL SQLStatement
Log.WriteLine "Query time: " & (Timer - QueryTimer)

The log is just a handful of procedures in a class module I threw together. Its output on the error is

@142921:  Running query with string "INSERT INTO Records SELECT * FROM Records_New" 
@142941:  Error Date/Time: 7/21/2009 2:29:40 PM
@142941:  Error # & Description: 3162, You tried to assign the Null value to a variable that is not a Variant data type.

I can confirm that TName and SQLStatement are both valid strings at the time the SQL operation is run, and that the sources (Records and Records_New) are both valid. Option Explicit is set elsewhere in the file to avoid any confusion from typos. The error is thrown on the DoCmd line.

Why would this have a Null value, even though DoCmd.RunSQL doesn't return a value?

A: 

Maybe Timer needs parens?

QueryTimer = Timer()
Carl Manaster
`Timer` is a VBA builtin that returns fractions of a second since 1/1/1970. Its used in other places without issue. The iffy line is the `DoCmd.RunSQL` line.
A. Scagnelli
Timer Function: "Returns a Single representing the number of seconds elapsed since midnight...In Microsoft Windows the Timer function returns fractional portions of a second" (http://office.microsoft.com/en-us/access/HA012289231033.aspx) Note it can be used as a expression in a Access database engine query.
onedaywhen
+2  A: 

Can you post the table descriptions for Records and Records_New tables? I would wager that you are trying to insert a NULL value into one of the columns of the "Records" table (and the column description is NOT NULL).

Hope this helps.

Bart J
This turned out to be the issue. The source (Records_New) was a stored Access Query that contained all the records in a table that did not exist in the Records table (based on Records having a `Null` ID auto number). The `INSERT` tried to apply a `Null` auto number, which isn't allowed. Hiding the result of the ID number check fixed the issue.
A. Scagnelli
+1  A: 

I think it will help if you also change the insert statement to be more specific about which columns it is inserting/selecting. You are asking for bugs by being so non-specific.

This may seem like it is non-responsive to your answer, but I suspect that the columns in the select table and destination table are either not lined up, or there is a field in the destination table that disallows null.

Try this:

In a new Query (in SQL view) paste your query "INSERT INTO Records SELECT * FROM Records_New" in and try to run it manually. I bet you get a more specific error and can troubleshoot the query there before running it with the added complexity of the code around it.

JohnFx
Pasting in the SQL query gives an error dialog with the same message that was trapped in the code. The help on this error doesn't provide any explanation, since I can't figure out what's been set to `Null` in the first place.
A. Scagnelli
In that case, I suggest rewriting the the query to be more explicit about the exact fields in the source and destination table. Also double/triple check that none of the fields in the destination table disallow nulls, and that the field types are the same in both tables.
JohnFx
A: 

INSERT INTO Statement (Microsoft Access SQL)

Your SQL INSERT statement is incorrect - it should be:

INSERT INTO Records SELECT * FROM [Records_New];

Here's what you need to use:

CStr("INSERT INTO " & TName & " SELECT * FROM [" & TName & "_New)"];")
OMG Ponies
What are the `n`s for? I've never seen that in SQL syntax before?
A. Scagnelli
It's a table alias, so you know you're getting * from the RECORDS_NEW table without concern that it might take from RECORDS instead or as well.
OMG Ponies
A. Scagnelli
Try without the table alias - it's a force of habit, sorry.
OMG Ponies
Even without the table alias, it gives a syntax error.
A. Scagnelli
Using `VALUES` here won't help, since its only for a single-record `INSERT`. `VALUES` expects a list of values, one for each field, not a list of records like the `SELECT` would return.
A. Scagnelli
You're right about the use of VALUES - I updated my post to include the MS Access 2007 MSDN link explaining its use.
OMG Ponies
KB article for my previous comment: http://msdn.microsoft.com/en-us/library/bb208861.aspx
A. Scagnelli
I think you guys are on the wrong track with this.
JohnFx