Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Stan Denman is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    50

    Import from CSV

    Years ago I used an access book that provided for a very complicated contact management schema that has been biting me in the backside ever since. The main contacts I need to manage are clients, but there are also various 3rd parties like doctors. The address management was a many to many and like I said, too complex, and too unwieldy. In my MS access DB I had the bright idea of "subclassing" contacts.

    I have a Contacts Table and a Client Table. Contacts is everyone, client is, well, clients. Because the address management "broke down" I shifted to simply housing address and other data for clients in the client table.

    I had a hard drive crash this week and lost many MS DB client records. I use the MS DB to populate Acrobat forms for client signature. Those signed client document are stored on the Adobe cloud. You can click on a file in the above dashboard and get "download form field data" which gives you a csv file of this form's data (name, DOB, Address, etc) which can help me to repopulate my MS database. I have managed to merge these separate csv files and this is the source of data I want to use. But my client table has a vestige of the folly of following a complete MS Access book!

    In my Clients table I have a foreign field, ContactID, as primary. That field, ContactID is autonumber, primary in table Contacts. So when I import my data from the csv file I have


    1. Client a record in table Contacts
    2. Create a record in table Clients with the ContactID just created in table Contacts
    3. Add the rest of the data in the csv file to that created record in table Clients.

    Any help would be most appreciated.

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    What code are you using and what help are you asking for?

    I suspect you want to find the id of the new contact record and if you are are you are using sql to populate the contact table you should be able to use @@identity

  3. #3
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Some more info along the same lines, showing your attempted code would help getting more pertinent answers:
    https://stackoverflow.com/questions/...g-foreign-keys
    https://stackoverflow.com/questions/...y-relationship
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  4. #4
    Stan Denman is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    50
    Thanks for your responses. I don't get it sorry. Have no idea what @@identity is. MS Access much too complicated for me. Wish I had started with something else years ago! I'll just undo the complicated "subclassing".

  5. #5
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    It is not that complicated; you don't say how you import the csv (built in Import button on the ribbon vs custom code), but basically the way this usually works is you import the entire csv into a temporary Access table with all the fields that exist in the csv file. Then you us an append query to add the required fields to the Contacts table. And finally you use a second append query to add the rest of the fields from the temporary table to the Clients table using Dmax("ContactID","Contacts") as the foreign key for the newly added record in the Contacts. Last step is to empty the temporary table and import a new csv....
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  6. #6
    Stan Denman is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    50
    OK. Thanks.

  7. #7
    madpiet is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    154
    Depends on how your PDFs are structured. If they have tables, I'd use Excel to import the data, because it can read PDF files... well, PowerQuery can.

  8. #8
    Stan Denman is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    50
    So I have added records to ContactID from the temporary table made from the csv file. Now I want to add records to the client table. I want add the data based upon the criteria of matching the last name. So I would create a record in table Client for Joe Blow with the right SSN and the right ClientID. Not sure how to do this. This is my append query. I don't understand how to get the right ClientID in there


    Click image for larger version. 

Name:	Screenshot (1).jpg 
Views:	16 
Size:	77.2 KB 
ID:	51418.

  9. #9
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    I think you mean the "right ContactID". If you process this one csv\client at the time (and the ContactID in Contacts is set up as an incrementing autonumber) you would simply add a calculated field to your append query: ContactID:Dmax("ContactID","Contacts") (as the mas will be the one you just added) and append that the foreign key in Clients.
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  10. #10
    Stan Denman is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    50
    I have already added 10 records to the Contacts table. ContactID was created automatically. The name of the person is also in table Contact. Now I simply want to create records in the client table with data from the temporary table putting in the right contact id based upon matching the last name from contact table and the temporary table. This is what chatGPT thinks I should do:

    INSERT INTO Clients (ContactID, SSN, ClientMailingAddr, City, State, Zip)SELECT C.ContactID, T.ClientSSN, T.ClientMailingAddr1, T.ClientMailingAddrCity, T.ClientMailingAddrState, T.ClientAddrZip
    FROM TempData AS T
    INNER JOIN Contacts AS C ON T.LastName = C.LastName;

  11. #11
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Yes, if you have multiple records you want to process you can join on LastName; but in case you have duplicate last names you could also add a second join on another field (FirstName or DateOdBirth). And I would suggest you change the join type to (left) OUTTER JOIN where the LastName in Clients Is Null (look at how the Access wizard creates a "unmatched query" between Contacts and Clients on LastName) to avoid creating duplicates if you run this query multiple times.

    If you can upload a sample file with just the three tables (tempData,COntacts,Clients) and the queries you already have it would help a lot.
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  12. #12
    Stan Denman is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    50
    Thanks for your offer, but my data includes SSNs. I have this code and when I try to run it it says it will append 15 records. Pretty amazing when TempData table only has 10 records! I just hate Access:

    INSERT INTO tblClients ( ContactID, SSN, MailingAddr1, City, State, Zip )
    SELECT tlbCont.ContactID, TempData.ClientSSN, TempData.ClientMailingAddr1, TempData.ClientMailingAddrCity, TempData.ClientMailingAddrState, TempData.ClientMailingAddrZip
    FROM TempData INNER JOIN tblCont AS tlbCont ON TempData.ClientLastName = tlbCont.LastName;

  13. #13
    madpiet is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    154
    SSNs in Access is an epic fail. Zero data security. Hard pass on that one.

  14. #14
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    That is because you probably have duplicates in the tblCont for the LastNames, hence my earlier suggestions to add another field to the join to avoid ambiguity and make the join an outer join to avoid adding the same records multiple times.
    Uploading a sample database with "dummy" records will help us help you....
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  15. #15
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    You don't upload the real data, just a couple of dummy records in each table with fake SSN, names, addresses, etc. to allow us to look at and write the queries...
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Import Linked Outlook Tasks Folder Doesn't Import All Data
    By Bkper087 in forum Import/Export Data
    Replies: 0
    Last Post: 11-19-2019, 12:06 AM
  2. Replies: 6
    Last Post: 09-08-2019, 03:18 PM
  3. Replies: 1
    Last Post: 07-09-2017, 11:14 PM
  4. Replies: 2
    Last Post: 02-28-2017, 01:34 AM
  5. Replies: 13
    Last Post: 01-25-2016, 01:36 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums