Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programming a LOOP statement

Status
Not open for further replies.

dbvb

Programmer
Jan 26, 2000
2
0
0
US
Hope someone out there can help. I'm a newbie with limited VB5 experience. I'm working through what I thought would be a simple loop structure to read multiple records in an MS Access table, calculate a value from each record, cumulate these values and report the total. The problem I am having is that the loop never leaves the initial record. It iterates through each record properly but the calculations it performs is always against the 1st record in the file.<br>
<br>
I'm attaching the code (it's short!!) in hopes someone can pick out my obvious error.<br>
<br>
Any help would be appreciated.<br>
<br>
Private Sub ROI_Click()<br>
Dim dbMydb As Database<br>
Dim recinvest As Recordset<br>
Set dbMydb = OpenDatabase _<br>
(&quot;C:\My Documents\Invest2.mdb&quot;)<br>
Set recinvest = dbMydb.OpenRecordset(&quot;Invests&quot;)<br>
<br>
Dim Days As Double<br>
Dim days1 As Double<br>
Dim Gain As Single<br>
Dim gaintot As Single<br>
Dim Years As Single<br>
purch_date = recinvest(&quot;purch_date&quot;)<br>
market = recinvest(&quot;market&quot;)<br>
cost = recinvest(&quot;cost&quot;)<br>
Days = 0<br>
days1 = 0<br>
gaintot = 0<br>
With recinvest<br>
Do Until .EOF<br>
Days = Now - purch_date<br>
Gain = market - cost<br>
days1 = days1 + Days<br>
gaintot = gaintot + Gain<br>
Years = Days / 365<br>
Print gaintot<br>
.MoveNext<br>
Loop<br>
.Close<br>
End With<br>
End Sub<br>

 
I think all you need to do is put the assignment settings for purch_date, market, and cost inside the loop - it looks like they're set only at the beginning of the procedure.
 
Well first of all is &quot;purch_date&quot; a field in your table?<br>
If so you need DOT in front of it .purch_date<br>
as well as any other field in your table<br>
I don't see any DOT's in ther except on the movenxt at the end<br>
the code thinks it's a variable not a field.<br>

 
That worked. I thought it would be something simple like that. Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top