Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Software > Access Modules daovba ado > Loop Code
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 4 Topic 1452 of 1609
Post > Topic >>

Loop Code

by =?Utf-8?B?V2lzc2Ft?= <Wissam@[EMAIL PROTECTED] > Apr 10, 2008 at 10:32 PM

Hi,
I have the code below that would copy data from one table (rsSource) into 
another table (rsTarget). Is there a loop code that would enable me to do 
something like the following:

Loop for N =1 till 40
If rsSource![ResultN] <> "" Then 
rsTarget.Edit 
rsTarget![DateN] = rsSource![DateN] 
rsTarget![ResultN] = rsSource![ResultN] 
rsTarget.Update 
End IF

{This is a database on patient visits. For each visitN, I have DateN, 
CholesterolN,GlucoseN,... and ~9 other fields. I am trying to see if there
is 
a loop that would save me time from having to write all of this in the
code}.
Thanks in advance. 
************************************************* 
Dim db As DAO.Database 
Dim rsSource As DAO.Recordset 
Dim rsTarget As DAO.Recordset 
Dim lngcount as lnteger 

Set db = CurrentDb() 
Set rsSource = db.OpenRecordset("SourceTable") 
Set rsTarget = db.OpenRecordset("TargetTable") 
rsSource.MoveFirst 
lngcount = rsTarget.RecordCount 
If lngcount = 0 Then 
rsTarget.AddNew 
rsTarget![Patient#] = rsSource![Patient#] 
rsTarget.Update 
End If 
Do Until rsSource.EOF 
'Search for matching record based on primary key field which is Patient# 
rsTarget.Index = "PrimaryKey" 
rsTarget.Seek "=", rsSource![Patient#] 
If rsTarget.NoMatch Then ' .NoMatch is FALSE if Patient# found 
rsTarget.AddNew 
rsTarget![Patient#] = rsSource![Patient#] 
rsTarget.Update 
End If 
rsTarget.Index = "PrimaryKey" 
rsTarget.Seek "=", rsSource![Patient#] 
If rsSource![Result1] <> "" Then 
rsTarget.Edit 
rsTarget![Date1] = rsSource![Date1] 
rsTarget![Result1] = rsSource![Result1] 
'will also need to assign values for 9 other fields called 
Cholesterol1,Glucose1,...
rsTarget.Update 
End If 
If rsSource![Result2] <> "" Then 
rsTarget.Edit 
rsTarget![Date2] = rsSource![Date2] 
rsTarget![Result2] = rsSource![Result2] 
'will also need to assign values for 9 other fields called 
Cholesterol2,Glucose2,...
rsTarget.Update 
End If 
‘I need to enter similar If statements for Result3, Result4,…Result40,

rsSource.MoveNext 
Loop
 




 4 Posts in Topic:
Loop Code
=?Utf-8?B?V2lzc2Ft?= <  2008-04-10 22:32:00 
Re: Loop Code
"Douglas J. Steele&q  2008-04-11 10:48:44 
RE: Loop Code
=?Utf-8?B?V2lzc2Ft?= <  2008-04-12 07:51:00 
Re: Loop Code
"zhaorong" <  2008-05-06 11:18:15 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Nov 21 13:04:53 CST 2008.