Skip to Content
0
Apr 05, 2010 at 12:03 PM

Service Credit Note repeats multiple lines on following credit notes

13 Views

I'm trying to add service credit notes with multiple lines and I'm having a problem with lines repeating on following credit notes. If I add a credit note with 3 lines and the next credit note has only one line, lines 2 and 3 from the previous credit note repeat on the next credit note. It continues repeating lines 2 and 3 until a credit note is entered with more than 3 lines. I can't find what I am doing wrong and appreciate any and all help. I'm working with Business One version 2007 SP00 PL49 and I wrote the following code.

Function AddARCreditnotes() As Boolean

On Error GoTo errAddArcreditnote

AddARCreditnotes = False

Dim db As DAO.Database

Dim rst As DAO.Recordset

Dim oARCN As SAPbobsCOM.Documents

Dim oARCNLines As SAPbobsCOM.Document_Lines

Dim lngCurDocNo As Long, x As Long

Dim strErrDocs As String

Dim lngDocKey As Long

If InitializeCompany() = False Then

MsgBox "Cannot connect to SBO", vbCritical

Exit Function

End If

Set oARCN = oCoSource.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oCreditNotes)

Set db = CurrentDb()

Set rst = db.OpenRecordset("qinvB1CreditNoteImport")

If rst.BOF And rst.EOF Then

GoTo exitAddARcreditnote

End If

strErrDocs = ""

With oARCN

rst.MoveFirst

Do Until rst.EOF

If .GetByKey(rst!DocNum) = False Then

.CardCode = rst!CardCode

lngCurDocNo = rst!DocNum

If Not IsNull(lngCurDocNo) Then .NumAtCard = lngCurDocNo

.DocDate = rst!DocDate

.DocDueDate = rst!DocDate

.Doctype = dDocument_Service

.DiscountPercent = rst!Discount

x = 0

Do Until lngCurDocNo <> rst!DocNum

Set oARCNLines = .Lines

Debug.Print lngCurDocNo & "-" & rst!CardCode

oARCNLines.SetCurrentLine (x)

oARCNLines.AccountCode = "2170"

oARCNLines.TaxCode = IIf(rst!intTaxStatus = 1, "TAXON", "TAXNO")

oARCNLines.LineTotal = rst!LineTotal

oARCNLines.ItemDescription = "Original Invoice Number " & rst!U_OrigInvNo

rst.MoveNext

If Not rst.EOF Then

If lngCurDocNo = rst!DocNum Then

x = x + 1

.Lines.Add

End If

End If

Loop

.Add

Call oCoSource.GetLastError(errcode, B1ERRMSG)

If (0 <> errcode) Then

Debug.Print ("Found error:" + Str(errcode) + "," + B1ERRMSG) & " - " & lngCurDocNo

GoTo exitAddARcreditnote

End If

Else

rst.MoveNext

End If

'GoTo exitAddARcreditnote

Loop

End With

AddARCreditnotes = True

exitAddARcreditnote:

On Error Resume Next

rst.Close

db.Close

Set rst = Nothing

Set db = Nothing

oARCN.Close

Set oARCN = Nothing

CloseCompany

Exit Function

errAddArcreditnote:

MsgBox Err.Number & Err.Description

Call oCoSource.GetLastError(errcode, B1ERRMSG)

If (0 <> errcode) Then

Debug.Print ("Found error:" + Str(errcode) + "," + B1ERRMSG) & " - " & lngCurDocNo

End If

strErrDocs = strErrDocs & "-" & CStr(lngCurDocNo)

Resume exitAddARcreditnote

End Function

Thank-you for your help.