Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 15th, 2005, 02:46 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
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.
Reply With Quote
  #2 (permalink)  
Old February 15th, 2005, 03:14 PM
Friend of Wrox
 
Join Date: Jan 2005
Location: Kansas City, Missouri, USA.
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

Reply With Quote
  #3 (permalink)  
Old February 15th, 2005, 03:33 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
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 :)
Reply With Quote
  #4 (permalink)  
Old February 15th, 2005, 04:19 PM
Friend of Wrox
 
Join Date: Jan 2005
Location: Kansas City, Missouri, USA.
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

Reply With Quote
  #5 (permalink)  
Old February 15th, 2005, 04:30 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
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.
Reply With Quote
  #6 (permalink)  
Old February 15th, 2005, 05:00 PM
Friend of Wrox
 
Join Date: Jan 2005
Location: Kansas City, Missouri, USA.
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

Reply With Quote
  #7 (permalink)  
Old February 18th, 2005, 07:54 PM
Authorized User
 
Join Date: Feb 2005
Location: Frankston South, Victoria, Australia.
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
Reply With Quote
  #8 (permalink)  
Old February 18th, 2005, 08:35 PM
Friend of Wrox
 
Join Date: Jan 2005
Location: Kansas City, Missouri, USA.
Posts: 471
Thanks: 0
Thanked 1 Time in 1 Post
Default

Daniel,

Thanks. Wasn't sure if it was clear.

dartcoach

Reply With Quote
  #9 (permalink)  
Old February 20th, 2005, 12:30 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 10:45 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.