Question

So I have a VBA code that is importing excel sheets into Access as new tables, deleting data out of existing tables and then using the SQL INSERT function to move the data from the new tables into the existing tables to preserve the relationships that exist. I have 3 tables this is done to and for some reason only one of them isn't working. The code seems to execute with no errors but when I open up the table there is no data. The table being imported has the same column names and the same data types on all fields as the table I am trying to insert into. Does anyone have an idea as to why only the one table is not importing correctly? Below is the code I am using, and the Part Number the one that isn't working:

SQL = "INSERT INTO [Reps] SELECT RepsX.* FROM RepsX;"
CurrentDb.Execute SQL
SQL = "INSERT INTO [Part Number] SELECT PartNumberX.* FROM PartNumberX;"
CurrentDb.Execute SQL
SQL = "INSERT INTO [Supplier link] SELECT SupplierLinkX.* FROM SupplierLinkX;"
CurrentDb.Execute SQL

I feel like it's something little, but I've spent most of my day trying to figure it out...

Was it helpful?

Solution

I can't give you a definitive answer, just suggestions: carefully examine the data types of the columns in the Part Number table, then compare them to the data types of the columns in the PartNumberX table. Make sure the corresponding columns are compatible, if needed by converting the PartNumberX table columns into appropriate data types using the built-in conversion functions, as shown below.

Also, don't rely on the column orders matching between source and destination table--always explicitly type out the column names, like so:

insert into [Part Number] (c1, c2, ..., cn)
select cint(c1), cdbl(c2), ..., cstr(cn)
from PartNumberX

Make sure that any columns in the Part Number table that are non-null (i.e., do not allow null values) indeed do always get non-null values. If necessary by testing for and then handling null values:

insert into [Part Number] (c1, ...)
select cint(iif(c1 is null, 0, c1)), ...
from PartNumberX

Make sure the insert does not violate any referential integrity rules (relationships). You can visually examine relationships with the Database Tools > Relationships > Relationships command, then clicking Relationship Tools > Design > Relationships > All Relationships.

Finally, you don't have to run the insert query in VBA. You can just as easily run it to test it out in the Access query editor's SQL view. That will give you some more feedback on possible errors.

OTHER TIPS

Paste the SQL into the SQL view of a query window and run it. There should be a message telling you how many records will be appended, or if not the reason why.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top