473,386 Members | 1,644 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,386 software developers and data experts.

Can I create a loop to streamline this code?

Hi All,
I am using Access 2000.

I would like to streamline this code by using a variable for the column
name. I have three tables with 255 columns each that I would like to
populate with the data from one table that has 1 column. Each of the
three tables will each up with 1 record with 255 columns. This is the
code I wrote (in brief) to demonstare whate I want to do. Is there an
easier way? I don't really want to type 500 lines of code for each
table!
Thanks,
Kathy

Public Sub CopyDataToRecipeTables()

Dim rsMain As ADODB.Recordset
Dim rsTable As ADODB.Recordset

Dim strMain As String
Dim strTable As String

Set rsMain = New ADODB.Recordset
Set rsTable = New ADODB.Recordset

strMain = "Select FIELD1, RowID From RecipeData"
strTable = "Select * FROM Recipe1"

rsMain.Open strMain, CurrentProject.Connection, adOpenKeyset,
adLockOptimistic
rsTable.Open strTable, CurrentProject.Connection, adOpenDynamic,
adLockOptimistic

rsTable.AddNew
rsTable!Field1 = rsMain!Field1
rsMain.MoveNext
rsTable!Field2 = rsMain!Field1
rsMain.MoveNext
rsTable!Field3 = rsMain!Field1
rsMain.MoveNext
rsTable!Field4 = rsMain!Field1
rsTable.Update

End Sub

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #1
4 2713

"Kathy" <Kathy.merecat.com> wrote in message
news:41**********************@news.newsgroups.ws.. .
Hi All,
I am using Access 2000.

I would like to streamline this code by using a variable for the column
name. I have three tables with 255 columns each that I would like to
populate with the data from one table that has 1 column. Each of the
three tables will each up with 1 record with 255 columns. This is the
code I wrote (in brief) to demonstare whate I want to do. Is there an
easier way? I don't really want to type 500 lines of code for each
table!
Thanks,
Kathy

Public Sub CopyDataToRecipeTables()

Dim rsMain As ADODB.Recordset
Dim rsTable As ADODB.Recordset

Dim strMain As String
Dim strTable As String

Set rsMain = New ADODB.Recordset
Set rsTable = New ADODB.Recordset

strMain = "Select FIELD1, RowID From RecipeData"
strTable = "Select * FROM Recipe1"

rsMain.Open strMain, CurrentProject.Connection, adOpenKeyset,
adLockOptimistic
rsTable.Open strTable, CurrentProject.Connection, adOpenDynamic,
adLockOptimistic

rsTable.AddNew
rsTable!Field1 = rsMain!Field1
rsMain.MoveNext
rsTable!Field2 = rsMain!Field1
rsMain.MoveNext
rsTable!Field3 = rsMain!Field1
rsMain.MoveNext
rsTable!Field4 = rsMain!Field1
rsTable.Update

End Sub


Kathy, it's very hard to imagine why you would want to put the same data in
every field, but you can iterate over the fields this way:

Dim fld as Field

[your code to open both recordsets]

For Each fld in rsTable.Fields
rsTable(fld.NAME) = rsMain!Field1
Next
rsTable.Update

HTH
Nov 13 '05 #2

Thank You! That worked perfectly. I realize that I didn't include the
code that loops through the RecipeData table.
The recipe data is an imported text file 706 rows long. I must convert
that to a series of tables one row long but 255 columns wide so that the
recipe data can be viewed in a series of forms that resemble the machine
interface.

Kathy

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #3
"Randy Harris" <ra***@SpamFree.com> wrote in message
news:uJ******************@newssvr33.news.prodigy.c om...

"Kathy" <Kathy.merecat.com> wrote in message
news:41**********************@news.newsgroups.ws.. .
Hi All,
I am using Access 2000.

I would like to streamline this code by using a variable for the column
name. I have three tables with 255 columns each that I would like to
populate with the data from one table that has 1 column. Each of the
three tables will each up with 1 record with 255 columns. This is the
code I wrote (in brief) to demonstare whate I want to do. Is there an
easier way? I don't really want to type 500 lines of code for each
table!
Thanks,
Kathy

Public Sub CopyDataToRecipeTables()

Dim rsMain As ADODB.Recordset
Dim rsTable As ADODB.Recordset

Dim strMain As String
Dim strTable As String

Set rsMain = New ADODB.Recordset
Set rsTable = New ADODB.Recordset

strMain = "Select FIELD1, RowID From RecipeData"
strTable = "Select * FROM Recipe1"

rsMain.Open strMain, CurrentProject.Connection, adOpenKeyset,
adLockOptimistic
rsTable.Open strTable, CurrentProject.Connection, adOpenDynamic,
adLockOptimistic

rsTable.AddNew
rsTable!Field1 = rsMain!Field1
rsMain.MoveNext
rsTable!Field2 = rsMain!Field1
rsMain.MoveNext
rsTable!Field3 = rsMain!Field1
rsMain.MoveNext
rsTable!Field4 = rsMain!Field1
rsTable.Update

End Sub

Kathy, it's very hard to imagine why you would want to put the same data

in every field, but you can iterate over the fields this way:

Dim fld as Field

[your code to open both recordsets]

For Each fld in rsTable.Fields
rsTable(fld.NAME) = rsMain!Field1
Next
rsTable.Update

HTH

From the looks of things, it won't be the same value. Each time
rsMain!Field1 is used, it is referencing a different record, since rsMain.Mo
veNext is called each time. I think it might have helped if the recordsets
had been named "rstRead" and "rstWrite" so you can see what is going on.
This sort of routine would need some error handling since you are putting an
unknown number of records into a single record. What if there were more
than 255 records which won't fit into a single table? In fact, whatever
you're doing seems to be going against the grain of relational database
design where tables should be long and skinny, not short and fat (if that
explanation isn't too technical).


Nov 13 '05 #4

Here is my procedure with your addition.
The reason for this project is not to store the Recipe in these tables
I'm making, but to display them in forms that look like the machine
interface screens and print them as a paper reference for the machine
operators.

I agree that this is an inefficent table structure. But it is what I
thought I would need to do to get the values to display in the forms I
created that resemble the machine interface.

The operator will click a button to import the recipe, convert it from
one long table to 3 wide tables and diplay the result in six forms in a
tabbed control. They can then choose to print any of the reports for
their parer backup.

The tables will be cleared until the next time someone wants to print a
recipe.

Public Sub CopyDataToRecipeTables()

Dim rsMain As ADODB.Recordset
Dim rsTable As ADODB.Recordset

Dim strMain As String
Dim strTable As String
Dim fld As Field
Set rsMain = New ADODB.Recordset
Set rsTable = New ADODB.Recordset

strMain = "Select FIELD1, RowID From Recipe Where RowID < 256"
strTable = "Select * FROM Recipe1"

rsMain.Open strMain, CurrentProject.Connection, adOpenKeyset,
adLockOptimistic
rsTable.Open strTable, CurrentProject.Connection, adOpenDynamic,
adLockOptimistic
rsTable.AddNew
For Each fld In rsTable.Fields
rsTable(fld.Name) = rsMain!Field1
rsMain.MoveNext
Next
rsTable.Update

End Sub
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #5

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

Similar topics

7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
6
by: SamIAm | last post by:
Hi am creating a email application that needs to mail out a very large amount of emails. I have created a multithreaded c# application that using message queuing. I have created a threadpool of 5...
7
by: Roemer | last post by:
Hi all I stumbled over a new problem: I have a programm with just a class that is asynchronous listening for network connections. As soon as someone connected, a new form needs to be created....
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
10
by: SM | last post by:
Hello I'm trying to create a multi dimensional array in JavaScript, but after some reading i still can't figure out how to apply it to my model. Here it is: I have a list A and for each item...
7
by: Matt | last post by:
Hi all, I'm trying to create a system where it reads a number of records from a database and then creates a row in the GUI that contains a single field from the database and a button that has a...
4
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the...
0
by: rw2007 | last post by:
I'd like to hear what you think about this... I'm working on a new architecture for an existing software product. I want this to be a flexible - and especially extendable - architecture for future...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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.