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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare current and previous records with loop and update

Status
Not open for further replies.

primagic

IS-IT--Management
Jul 24, 2008
476
GB
I have the following code which is meant to loop through the records in a recordset and tell me if the previous record is the same as the current record.

I need to amend it so if the current accountnumber is the same as the previous account number then update the invoiceid column of the recordset to display the same invoice number. If it is different I need to add a new invoice number to the tbl invoices and use that number for the different account. Hope that makes sense.


Code:
Dim rs As DAO.Recordset
   Dim db As DAO.Database
   Dim qd As DAO.QueryDef
   Dim AccountNumber As String
   
   
   Set db = CurrentDb
   Set qd = db.QueryDefs!Query1
   qd.Parameters![Forms!frmDashboard!InvoiceFromDate] = [Forms]![frmDashboard]![InvoiceFromDate]
   qd.Parameters![Forms!frmDashboard!InvoiceToDate] = [Forms]![frmDashboard]![InvoiceToDate]

Set rs = qd.OpenRecordset
    
AccountNumber = rs!AccountNumber
Do While Not rs.EOF
If rs!AccountNumber = AccountNumber Then
'same Account number
MsgBox "Same account"
Else
'different Account Number
MsgBox "different account"
End If
rs.MoveNext
Loop
'Catch the last owner here
rs.Close

I have created a function that adds the new invoice. Can I call this from the above code anywhere?

Code:
Public Function AddInvoice()
   Dim rs As DAO.Recordset
   Dim db As DAO.Database
   

Set db = CurrentDb
Set rs = db.OpenRecordset("tblInvoices")
rs.AddNew
rs("InvoiceNumber") = DMax("InvoiceNumber", "tblInvoices") + 1
rs.Update
rs.Close
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top