loop not completing correctly 
Author Message
 loop not completing correctly

I have written the following module (first attempt at one
of these) - based around a lot of assistance gained from
previous replies to this newsgroup.  But I am encountering
a problem on the loop section, the temporary table I am
practising with has 4 records it updates the first and
second correctly, but then repeats the number it enters
into the second one on the third and fourth records.  I am
obviously missing something - can anyone possibly help.

Oh I know the error bit is wrong - thats the next thing I
have to work on.

Thanks in advance.

Joyce

Public Function UpdateCheques()
On Error GoTo UpdateCheques_Err

Dim db As database
Dim rs1, rs2 As DAO.Recordset
Dim NextAvailableCounter As Long

Set db = CurrentDb
Set rs1 = db.OpenRecordset("Temp Cheque List")
Set rs2 = db.OpenRecordset("CounterTable")

'Get the next counter.
NextAvailableCounter = rs2!NextAvailableCounter

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''
'Open the table and get the current value of
NextAvailableNumber,
'increment the value by 1, and save the value back into
the table
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''
Do Until rs1.EOF
    rs1.Edit
    rs2.Edit
    rs1!ChequeNo = rs2!NextAvailableCounter
    rs1.Update
    rs2!NextAvailableCounter = NextAvailableCounter + 1
    rs2.Update
    rs1.MoveNext

Loop

rs1.Close
rs2.Close
db.Close

'This is only a temporary error routine cos it was going
into,
'a perpetual loop and I haven't figured out how to write
one ...yet

UpdateCheques_Err:
MsgBox "Error " & Err & ": " & Error$
End

End Function



Tue, 03 May 2005 00:57:17 GMT  
 loop not completing correctly

Quote:

>I have written the following module (first attempt at one
>of these) - based around a lot of assistance gained from
>previous replies to this newsgroup.  But I am encountering
>a problem on the loop section, the temporary table I am
>practising with has 4 records it updates the first and
>second correctly, but then repeats the number it enters
>into the second one on the third and fourth records.  I am
>obviously missing something - can anyone possibly help.

>Oh I know the error bit is wrong - thats the next thing I
>have to work on.

Be sure to add the Exit Sub statement befor the error
handler.

Quote:
>Public Function UpdateCheques()
>On Error GoTo UpdateCheques_Err

>Dim db As database
>Dim rs1, rs2 As DAO.Recordset
>Dim NextAvailableCounter As Long

>Set db = CurrentDb
>Set rs1 = db.OpenRecordset("Temp Cheque List")
>Set rs2 = db.OpenRecordset("CounterTable")

>'Get the next counter.
>NextAvailableCounter = rs2!NextAvailableCounter

>'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
>''''''''
>'Open the table and get the current value of
>NextAvailableNumber,
>'increment the value by 1, and save the value back into
>the table
>'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
>''''''''
>Do Until rs1.EOF
>    rs1.Edit
>    rs2.Edit
>    rs1!ChequeNo = rs2!NextAvailableCounter
>    rs1.Update
>    rs2!NextAvailableCounter = NextAvailableCounter + 1
>    rs2.Update
>    rs1.MoveNext

>Loop

You never update the value of the NextAvailableCounter
variable.  Try this:
                . . .
        NextAvailableCounter = NextAvailableCounter + 1
        rs2!NextAvailableCounter = NextAvailableCounter
                . . .

Maybe this confusion is caused by your use of a variable
with the same name as a field in the table?  A common
convention is to name the variable lngNextAvailableCounter.

--
Marsh
MVP [MS Access]



Tue, 03 May 2005 02:19:44 GMT  
 loop not completing correctly
Here is a rewrite.  

You didn't dim rs1 as a DAO.Recordset.  
You aren't incrementing nextAvailableCounter

Public Function UpdateCheques()
On Error GoTo UpdateCheques_Err

Dim db As database
Dim rs1 as DAO.Recordset  '<<------------
Dim rs2 As DAO.Recordset
Dim NextAvailableCounter As Long

Set db = CurrentDb
Set rs1 = db.OpenRecordset("Temp Cheque List")
Set rs2 = db.OpenRecordset("CounterTable")

'Get the next counter.
NextAvailableCounter = rs2!NextAvailableCounter

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Open the table and get the current value of
'NextAvailableNumber,
'increment the value by 1, and save the value back into
'the table
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Do Until rs1.EOF
    rs1.Edit
        rs1!ChequeNo = rs2!NextAvailableCounter
    rs1.Update

    rs2.Edit
        NextAvailableCounter = NextAvailableCounter + 1 '<<-------
        rs2!NextAvailableCounter = NextAvailableCounter '<<-------
    rs2.Update

    rs1.MoveNext
Loop

UpdateCheques_Exit:
  rs1.Close
  rs2.Close
  db.Close
  Exit Function

'This is only a temporary error routine cos it was going
'a perpetual loop and I haven't figured out how to write
one ...yet

UpdateCheques_Err:
   MsgBox "Error " & Err & ": " & Error$
   Resume UpDateCheques_Exit

End Function

Quote:

> I have written the following module (first attempt at one
> of these) - based around a lot of assistance gained from
> previous replies to this newsgroup.  But I am encountering
> a problem on the loop section, the temporary table I am
> practising with has 4 records it updates the first and
> second correctly, but then repeats the number it enters
> into the second one on the third and fourth records.  I am
> obviously missing something - can anyone possibly help.

> Oh I know the error bit is wrong - thats the next thing I
> have to work on.

> Thanks in advance.

> Joyce

> Public Function UpdateCheques()
> On Error GoTo UpdateCheques_Err

> Dim db As database
> Dim rs1, rs2 As DAO.Recordset
> Dim NextAvailableCounter As Long

> Set db = CurrentDb
> Set rs1 = db.OpenRecordset("Temp Cheque List")
> Set rs2 = db.OpenRecordset("CounterTable")

> 'Get the next counter.
> NextAvailableCounter = rs2!NextAvailableCounter

> '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> ''''''''
> 'Open the table and get the current value of
> NextAvailableNumber,
> 'increment the value by 1, and save the value back into
> the table
> '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> ''''''''
> Do Until rs1.EOF
>     rs1.Edit
>     rs2.Edit
>     rs1!ChequeNo = rs2!NextAvailableCounter
>     rs1.Update
>     rs2!NextAvailableCounter = NextAvailableCounter + 1
>     rs2.Update
>     rs1.MoveNext

> Loop

> rs1.Close
> rs2.Close
> db.Close

> 'This is only a temporary error routine cos it was going
> into,
> 'a perpetual loop and I haven't figured out how to write
> one ...yet

> UpdateCheques_Err:
> MsgBox "Error " & Err & ": " & Error$
> End

> End Function



Tue, 03 May 2005 03:54:18 GMT  
 loop not completing correctly
Great.  I always hope that understanding the solution comes out of these
messages.  The old teach a man (woman) to fish and ....

Thanks for the feedback.  It is one of the things that encourages us to donate
our time to these groups.

Quote:

> Thank you so much for your assistance with both your help
> and Marshall's I have not only fixed it - but more
> importantly understand why it wouldn't work.

> Joyce



Tue, 03 May 2005 20:58:56 GMT  
 loop not completing correctly

Quote:

>Thank you so much for your assistance with both your help
>and Marshall's I have not only fixed it - but more
>importantly understand why it wouldn't work.

What John said.

--
Marsh
MVP [MS Access]



Tue, 03 May 2005 22:19:13 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. list box not looping correctly

2. Creating ADO recordset w/o database .Append not working correctly IIS5 win2000 Prof not .Net

3. Loop until function is complete...

4. Autonumber not transferring correctly anymore...

5. help not wirking correctly

6. Step into - F8 not working correctly in Access 2000

7. Make-Table query not sorting correctly

8. TransferText method in function does not working correctly

9. Parameterized Update query not working correctly in Access 97

10. Using Hyperlinks - Not working correctly, help !

11. .EntryID not reporting correctly after a .Move

12. MS Word not showing Tem[lates correctly?

 

 
Powered by phpBB® Forum Software