Wrox Programmer Forums
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 15th, 2005, 02:46 PM
Authorized User
 
Join Date: Jul 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default Looping thru fields

I have a table that I am trying to update with a CSV file that I have linked into my Access database. The field names are the same in both locations, but they aren't in the same order, and I can't count on the order remaining the same each time I get the new CSV file.

What I would like to do is loop thru one table and update the field of the same name in the other table.
For example (I know this won't work, its just the general idea)
Code:
Dim fld As Field
Do While Not rs1.EOF
    For Each Field in rs1.Fields
        rs2.fld = rs1.fld
    Next
Loop
I also see this working in a situation where I only want certain fields from a database. I can create an array of field names and loop thru the field names, to get just the fields I want. In my situation, I am doing an update of an Access db from a CSV file provided by a national membership organization. I may not want all the fields they provide. Since I have to check the tables both ways for this system, I would only have to create my field lists one time, and loop thru the lists to compare/update values. It saves typing out 50 field names for each loop to compare/update.

I do this with perl/mysql all the time. Can it be done in VBA?

Thanks for any insight.
 
Old February 15th, 2005, 03:14 PM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 471
Thanks: 0
Thanked 1 Time in 1 Post
Default

QJ,

Is the CSV file a complete new file or just the updates?

Kevin

 
Old February 15th, 2005, 03:33 PM
Authorized User
 
Join Date: Jul 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The CSV file is a completely new file each month. The national org doesn't just give us changes, so we have to go thru the CSV to add new members and modify existing members in the state db.

In addition, I have to go thru the CSV file to determine if members in the state db are no longer in the national db so I can mark them inactive for the state org to follow up with, and then make them active next month when they pay their dues.

I am beginning to understand why data synchronization can be such a PITA :)
 
Old February 15th, 2005, 04:19 PM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 471
Thanks: 0
Thanked 1 Time in 1 Post
Default

QJ,

Which field are you using for a Primary Key?
Does the CSV file come with field names in the first record?

Kevin

 
Old February 15th, 2005, 04:30 PM
Authorized User
 
Join Date: Jul 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The primary key field is simply called id, and yes, the CSV has the field names in the first row.
 
Old February 15th, 2005, 05:00 PM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 471
Thanks: 0
Thanked 1 Time in 1 Post
Default

QJ,
Don't know if this would work.
import the new csv file into a new table. (Always Named the same)
create a relationship between the id in your table and id in the new table. (This table will be recreated each time you import the CSV file).

since the 2 tables are now related by the id field, the field order doesn't matter.

Create an update query to update the records from the csv table.
this query should execute right after the import is completed.
Your table, will be refreshed with the new data, but only the records that are in both tables. If they are in both, make sure it sets your table to active.

Create an unmatched query to check if you have records that aren't on the CSV table and set them to inactive.

Create an unmatched query to check if there are new records on the CSV that you don't have and insert them.

Don't know.

Kevin

 
Old February 18th, 2005, 07:54 PM
Authorized User
 
Join Date: Feb 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Good thinking dartcoach. As I was reading the posts, I was starting to develop a strategy and when I read your post it was all there except better said.

Daniel
 
Old February 18th, 2005, 08:35 PM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 471
Thanks: 0
Thanked 1 Time in 1 Post
Default

Daniel,

Thanks. Wasn't sure if it was clear.

dartcoach

 
Old February 20th, 2005, 12:30 PM
Authorized User
 
Join Date: Jul 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I got pulled into a different project temporarily, and wasn't able to keep up on this thread.

I am going to try the query method you suggested, and see how it goes.

Thanks for the ideas.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping ssaranam SQL Server 2005 2 April 17th, 2008 01:40 AM
looping mrjoka Classic ASP Basics 1 September 26th, 2007 12:21 AM
Looping deepsea007 XSLT 1 June 14th, 2007 12:13 PM
Using Forum fields select fields on the fly hellosureshkumar Crystal Reports 0 December 17th, 2004 08:20 AM
Looping through form fields & retriving the values tdaustin Classic ASP Basics 8 June 10th, 2003 03:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.