473,387 Members | 1,721 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.

Primary Key Issues

17
Hello all,

I previously posted under this heading: Importing Data from One Excel Sheet into Multiple Access Tables Simultaneously, but the nature of the problem has changed so I decided to make a new discussion. Thanks to one of the members here I was able to successfully import the data into the tables from the excel spreadsheet and I greatly appreciate his help. However, I've run into a new problem. The first table assigns the unique key to the data, and I can't get this autonumber key to go to the other tables. Here is what I need to happen:

Table 1:
Data: 1, X, Y
2, X, Y
3, X, Y

Table 2:
Data: 1, A, B
2, A, B

Table 3:
Data: 3, C, D

Instead, what is happening is it imports that just the correct data into Table 1, but because of the relationship I have in the second table, with the ID's being linked, it won't import into the second table and instead I get an error message.

I need the tables to be set up this way because on the reporting level I might need to join tables together like this:

Report: 1, X, Y, A, B
3, X, Y, C, D

So access needs to be able to see what data matches up with what data in each table.

I've tried to set up code so when it transfers the data from the spreadsheet to the tables it automatically gives the same ID to each one, but I haven't been successful in getting it to work. I also thought of having the data go into a big staging table and then being broken down, but my admins here at work weren't crazy about that idea because they want to keep the database as small as possible.

I'm pretty new to the access scene and could really use some help. Thank you in advance for your time!
Jun 22 '07 #1
2 1626
rewalk
17
Here is the code that I've made so far which succesfully imports the files. Special thanks to members ont his network that have helped me fix some quirks in this code in the last discussion I made:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Done_Click()
  2.  
  3. Dim File As String
  4. Dim LastDataColumn As String, LastDataRow As String
  5. Dim LastDataCell As String
  6.  
  7. 'Set File name
  8. File = "C:\Sample Data.xls"
  9.  
  10. 'Open the file
  11. Workbooks.Open Filename:=File
  12.  
  13. 'Unprotect the file
  14. ActiveWorkbook.Unprotect
  15. ActiveSheet.Unprotect
  16.  
  17. 'Obtain the last cell that is used on the sheet
  18. LastDataColumn = Chr(Selection.SpecialCells(xlLastCell).Column + 64)
  19. LastDataRow = Selection.SpecialCells(xlLastCell).Row
  20. LastDataCell = LastDataColumn & LastDataRow
  21.  
  22. 'Import the data
  23. DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "ExcelImport", File, True, "A1:M" & LastDataRow
  24. DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "ExcelImport2", File, True, "N1:" & LastDataCell
  25.  
  26. 'Clear contents of template
  27. Worksheets("Sheet1").range("A2:" & LastDataCell).ClearContents
  28.  
  29. 'Protect the file
  30. ActiveWorkbook.Protect
  31. ActiveSheet.Protect
  32.  
  33. 'Save the file
  34. ActiveWorkbook.Save
  35.  
  36. 'Close the file
  37. ActiveWorkbook.Close
  38.  
  39. End Sub
  40.  
Jun 22 '07 #2
Lysander
344 Expert 100+
Instead, what is happening is it imports that just the correct data into Table 1, but because of the relationship I have in the second table, with the ID's being linked, it won't import into the second table and instead I get an error message.

I need the tables to be set up this way because on the reporting level I might need to join tables together like this:

Report: 1, X, Y, A, B
3, X, Y, C, D

So access needs to be able to see what data matches up with what data in each table.
As I understand it, you are importing an excel spreadsheet and splitting the rows in the spreadsheet across two access tables and you want to maintain the links across each row.

You are importing A-M into one table, and N into another. Can you change the spreadsheet so that column N becomes column P and then in Column N put in the row numbers, i.e. 1,2,3,to the last row. Then import A-N and N-P into your tables. Column N would become the primary key in each table and you can then link them.

The only other thing I can think of is the temporay table that your admins dont like. If the data was deleted after the import, and the dbs compacted, size would only be an issue during the import.
Jul 5 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Eric | last post by:
Let me start by saying that I'm still very new at PHP/MySql and will take any advice. :-) The issues is that every once in a while users get an error when they use this code. I believe the...
14
by: Sanjay Minni | last post by:
What is the datatype to be used for Primary Key columns for most optimised access given that - There will be a single column primary key only - The values will only be integers (but as...
0
by: sfh | last post by:
Greetings all, I have a question concerning primary key types. In the past, I have always created tables with a primary key as an "int" such as: CREATE TABLE color_id ( color_id int(10)...
1
by: sfh | last post by:
Greetings all, (I had posted this in MS SQL on accident, my apologies :( ) I have a question concerning primary key types. In the past, I have always created tables with a primary key as...
1
by: gilman | last post by:
Hello there, we have configured a perfect DB2 HADR pair. After some days, we had following problem: After a reboot standby becomes standby, that's ok. Pimary becomes primary, i tought db2 could...
7
by: sea | last post by:
Is it a good idea to programatically create a primary key? For example in a table called names, I have the following fields, (1) firstname (2)lastname (3) ID - will it be ok to create a primary...
4
by: misscrf | last post by:
Ok I have 2 issues. 1) I have a main candidate form with 2 subforms on a tab control: http://www.geocities.com/misscrf/images/contactcontinouscheckbox.jpg I have been encouraged to add these...
3
by: Jeff Amiel | last post by:
7.4.2 running on FreeBSD 5.4.2 I have a table (called "draft" ) that has a bigserial as the primary key. "check_id bigserial NOT NULL" I do NOT have an additional index on that column. I...
3
by: Chirag | last post by:
I want to create a table with member id(primary key for Students,faculty and staff ) and now i want to create issues with foreign key as member id but in references i could not able to pass on...
6
by: sachin | last post by:
Hi, I am facing some strange issue in DB2 UDB 9.5.1 I have created a database on DPF implemented environment and I tried to execute following commands Db2 create table test ( name char(10)...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.