Link to home
Start Free TrialLog in
Avatar of SweetingA
SweetingA

asked on

Logic within recordsets

I have a nice neat piece of code which was kindly scripted for me by one of the experts which looks for blank batch numbes in a table (starting at the bottom of the table) and copies the next batch number down into the blank record (see attached code).

This works beutifully but its only a part of the solution.

I need to add a small piece of logic......

If the machine number on the blank batch number record is not equal to the machine number on the next record down then instead of copying the batch number upwards i would like find the next non blank batch number above and copy that in.

If neither machine matches (which is very rare) then i would like to put in batch number X000001

Not sure if this makes sense or not but i will inclde a small example.....

Before current code run......

DateStamp     Machine    Batch
01/01/13            M2             B12
02/01/13            M1
03/01/13            M1
03/01/13            M1             B17
03/01/13            M6             B09
04/01/13            M6        
05/01/13            M6            
05/01/13            M7             N10

Current Result..................

DateStamp     Machine    Batch
01/01/13            M2             B12
02/01/13            M1             B17   OK
03/01/13            M1             B17   OK
03/01/13            M1             B17
03/01/13            M6             B09
04/01/13            M6             B10   NOK
05/01/13            M6             B10   NOK
05/01/13            M7             B10

Desired Result....................

DateStamp     Machine    Batch
01/01/13            M2             B12
02/01/13            M1             B17   OK
03/01/13            M1             B17   OK
03/01/13            M1             B17
03/01/13            M6             B09
04/01/13            M6             B09  OK
05/01/13            M6             B09  OK
05/01/13            M7             B10

Current code

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim sqlstr As String

sqlstr = "SELECT * FROM tbl_AS400_OPCP07 ORDER BY Machine, DateStamp, Batch"

Set db = CurrentDb
Set rs = db.OpenRecordset(sqlstr)

rs.MoveLast

While rs.BOF = False
   If Not IsNull(rs!Batch) Then
     vBatch = rs!Batch.Value
     vMachine = rs!Machine.Value
   Else
     rs.Edit
       rs!Batch.Value = vBatch
       rs!Machine.Value = vMachine
     rs.Update
   End If
   rs.MovePrevious
Wend
rs.Close

I know its a little complex but your asistance would be much appreciated.

Thankyou in advance
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SweetingA
SweetingA

ASKER

Thats very clever, thankyou very much but can you explain in words what its doing as i can't quite follow it.