473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Importing excel into Access: Reports primary key violation when there is not!

Hello!
I am writing VBA code in Access 97 to import an Excel 2000
Spreadsheet. I had hoped to do this using ODBC, but that was not to
be, for who knows what reason.

The problem is that I import the excel file into a table in Access
that I name X. Then I run an append query to import the data into
another table.

I get an error message that says it "can't append all the records" due
to "validation rule violations"

There ARE NO validation rules! If I copy and paste these rows manully,
Access accepts it with no errors.

The rows in the excel file look perfectly okay.

Any ideas??!!
Nov 12 '05 #1
7 10715
"Validation Rule violations" include things like:
- field is marked Required, but the entry is blank;

- field is marded No for Allow Zero Length (as it should be), but the entry
is a zero-length string (as distinct from blank/null);

- field is a foreign key with referential integrity, and the value is not in
the lookup table

as well as the validation rule on the fields, and on the table.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Bonnie R" <bo****@aol.com> wrote in message
news:72*************************@posting.google.co m...

I am writing VBA code in Access 97 to import an Excel 2000
Spreadsheet. I had hoped to do this using ODBC, but that was not to
be, for who knows what reason.

The problem is that I import the excel file into a table in Access
that I name X. Then I run an append query to import the data into
another table.

I get an error message that says it "can't append all the records" due
to "validation rule violations"

There ARE NO validation rules! If I copy and paste these rows manully,
Access accepts it with no errors.

The rows in the excel file look perfectly okay.

Any ideas??!!

Nov 12 '05 #2
"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message news:<40***********************@freenews.iinet.net .au>...
"Validation Rule violations" include things like:
- field is marked Required, but the entry is blank;

- field is marded No for Allow Zero Length (as it should be), but the entry
is a zero-length string (as distinct from blank/null);

- field is a foreign key with referential integrity, and the value is not in
the lookup table

as well as the validation rule on the fields, and on the table.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.


===================
Thanks for your reply, but I certainly had checked all that out. THat
is why it is such a mystery and why I posted a question to the group.

The table will accept the data if I copy and paste the rows one by
one. It is the same data, but the "violation" doesn't occur.

Bonnie
Nov 13 '05 #3
Here is some code you could try (copy and paste directly into a Standard
code modue)that links your Excel sheet to Access and then reads the data
in the sheet with a recordset object variable. I think I got most of
this code from the Access97 Help Files on tabledefs connect property
(except for the $ which you have to append to the sheet name- which here
is "sheet1$" - they forgot to mention about the $). Note: if the
ConnectOutput... line errors out, just get rid of the underscores and
put everything on one line - I broke it up with the underscores so it
would fit on the page here.

Sub TestConnect()
Dim DB As Database
Set DB = CurrentDb
ConnectOutput DB, "ExcelTbl", _
"Excel 5.0;HDR=YES;IMEX=2;DATABASE=C:\dir\Test1.xls", _
"Sheet1$" '--if this line errors out - get rid of "_"
End Sub 'and put it all on one line

Sub ConnectOutput(dbsTemp As Database, strTable As String, strConnect As
String, strSourceTable As String)
Dim tdfLinked As TableDef
Dim rstLinked As Recordset
Dim intTemp As Integer

' Create a new TableDef, set its Connect and
' SourceTableName properties based on the passed
' arguments, and append it to the TableDefs collection.
Set dbsTemp = CurrentDb
Set tdfLinked = dbsTemp.CreateTableDef(strTable)

tdfLinked.Connect = strConnect
tdfLinked.SourceTableName = strSourceTable
dbsTemp.TableDefs.Append tdfLinked
Application.RefreshDatabaseWindow
Set rstLinked = dbsTemp.OpenRecordset(strTable)
' Display the first three records of the linked table.
intTemp = 1
With rstLinked
Do While Not .EOF And intTemp <= 3
Debug.Print , .Fields(0), .Fields(1)
intTemp = intTemp + 1
.MoveNext
Loop
.Close
End With

End Sub

Rich

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #4
Bonnie R wrote:
Hello!
I am writing VBA code in Access 97 to import an Excel 2000
Spreadsheet. I had hoped to do this using ODBC, but that was not to
be, for who knows what reason.

The problem is that I import the excel file into a table in Access
that I name X. Then I run an append query to import the data into
another table.

I get an error message that says it "can't append all the records" due
to "validation rule violations"

There ARE NO validation rules! If I copy and paste these rows manully,
Access accepts it with no errors.

The rows in the excel file look perfectly okay.

Any ideas??!!


You didn't specify what type of data, but remember that numberical data
in excel defaults to "double" whilst access defaults to "long integer"
for numerical data. May not be your problem, but it's worth mentioning.

If you have decimals in your spreadsheet, make sure you set the
corresponding field to "double" in your access table.

Cheers
Chris

Nov 13 '05 #5
I hadn't checked back here for a while - I have been using my screwy
version with the unsolved data violation mystery.

Thanks for posting this code and I'll check it out. I do recall when I
was wrestling with the thing earlier that I connected in a similar way
using a Access module (Access 97) that I found that Access and/or
Excel would hang and in general behave quite badly. In fact, I got a
message box error message that I took a screen shot of, printed out,
and hung up on my cube wall for some small amusement value (it seemed
absurdly dire to me):

Automation error: Catastrophic Failure

Perhaps my problem was using Access97 and Excel 2000. I used Access97
due to the bug in Access2000 sendobject when sending emails.

So, thanks again and I'll experiment with your code.
Bonnie
Nov 13 '05 #6
I tried the code and, while I was easily able to connect to the excel
file and edit/query, etc., there was the old problem of the
misinterpretation of the data types - I had some fields that had dates
in them and in the linked table the values were represented as #Num!
Oh how I hate that.
Bonnie
Nov 13 '05 #7
bo****@aol.com (Bonnie R) wrote in message news:<72**************************@posting.google. com>...
I tried the code and, while I was easily able to connect to the excel
file and edit/query, etc., there was the old problem of the
misinterpretation of the data types - I had some fields that had dates
in them and in the linked table the values were represented as #Num!
Oh how I hate that.
Bonnie


What if you create an import specification and explicitly state that a
field is a date?
Nov 13 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Little PussyCat | last post by:
Hello, I nee to write something that will transfer excel data into an SQL Server table. I have for another database application I wrote have it importing Excel spreadsheet data using cell by...
1
by: Joris Kempen | last post by:
Hi people, I know that the question has come around sometimes: How to open an Access Report using ASP and export it to for example RTF. I'm trying to implement the first method of David...
16
by: cyranoVR | last post by:
This is the approach I used to automate printing of Microsoft Access reports to PDF format i.e. unattended and without annoying "Save As..." dialogs, and - more importantly - without having to use...
2
by: WODJ | last post by:
Folks, I hope someone out there can please help me, not much hair left with this one. I'm importing excel data into a SQL server database using an Oledb connection in C#. The problemm is that...
1
by: christiekp | last post by:
I am able to import a file from excel to access manually, using the top row as my headings, successfully. however, when using the TransferSpreadsheet command in a macro, importing, excel 5-7, has...
2
pod
by: pod | last post by:
Hello I am importing Excel files data into an Access database. My script goes through one Excel file at a time, row by row, and build a SQL statement for each row to insert data into the database....
1
by: Balasubramaniam K | last post by:
I want to generate a report in ms access to show comparison of products, wherein there might be 2, 3, 4 ... vendors who submit quotations for any no. of products. The products are displayed...
5
by: Russ Slater | last post by:
Hi, I am currently getting a very odd key violation when appending values to a table. The strangest thing is that it is only happening some of the time. I have a form where people can create...
0
by: Aravind555 | last post by:
Hi, I like to update access data base from excel , based on primary key. also i like to know how to get customized report(query) in Excel where i may need to do only refresh in excel Thanks...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.