I have a detailed macro that runs on invoices then queries a SQL database to identify whether any previous invoices have been issued to the client this year and inserts a file containing a table with various fields from the database to list those invoices
and the amounts from them. I have copied the code from a similar query that functions properly to insert a different file and modified the SQL table and field names to get the data I need. The file gets inserted but none of the field information is updated
and I can't identify why. This is the code I have, which is in a separate module of the macro. The initial codes are in the invoice document. If I include the line rsInvoiceLines.Close, then I get an error, although it functions correctly in the other macro:
Public Sub InvoiceStatement()
Dim rsInvoiceCount, rsInvoiceLines As Recordset
Dim strJobNo, strDate As String
Dim intNumLines As Integer
'Get the job number and date from the doc
strJobNo = GetValue("[JobNo]")
If Len(strJobNo) < 1 Then
Exit Sub
End If
strDate = GetValue("[Date]")
If Len(strDate) < 1 Then
Exit Sub
End If
'Find the invoice statement marker and remove it
With Selection.Find
.Text = "[InvoiceStatement]"
.Forward = True
.Wrap = wdFindContinue
End With
If Selection.Find.Execute = True Then
Selection.Delete Unit:=wdCharacter, Count:=1
Else
Exit Sub
End If
'See if there are any invoice transactions. If not, bail out
Set rsInvoiceCount = modDB.Query("select NUM=IsNull(Count(*), 0) from INV where JOB_NO='" & strJobNo & "'")
intNumLines = rsInvoiceCount.Fields("NUM")
rsInvoiceCount.Close
If intNumLines < 1 Then
Exit Sub
End If
'Insert a new page and the invoice statement file
Selection.InsertBreak Type:=wdPageBreak
Selection.InsertFile FileName:=ModDBMacros.FirmPath & "Invoice_stmt.doc", range:="", _
ConfirmConversions:=False, Link:=False, Attachment:=False
Set rsInvoiceLines = modDB.Query("select LN.DATE, HEAD=case HD.TYPE when 'INV' then HD.INV_NO, LN.FEES_AMT, LN.PARTS_AMT, HD.DISC_AMT, HD.DATE_PAID from INVDET as LN, INV as HD where LN.INV_AUDIT_NO = HD.AUDIT_NO and HD.JOB_NO ='" & strJobNo & "' and HD.DATE
> '2013.12.31' order by HD.DATE")
Selection.GoTo what:=wdGoToBookmark, Name:="Inv_Stmt_Start"
Do While Not rsInvoiceLines.EOF
Selection.TypeText (rsInvoiceLines.Fields("DATE"))
Selection.MoveRight Unit:=wdCell
If Len(rsInvoiceLines.Fields("HEAD")) > 0 Then
Selection.TypeText (rsInvoiceLines.Fields("HEAD"))
Selection.TypeText vbCrLf
End If
Selection.TypeText (rsInvoiceLines.Fields("REF_NO"))
Selection.MoveRight Unit:=wdCell
Selection.TypeText (Format(rsInvoiceLines.Fields("FEES_AMT"), "#,###,##0.00"))
Selection.MoveRight Unit:=wdCell
Selection.TypeText (Format(rsInvoiceLines.Fields("PARTS_AMT"), "#,###,##0.00"))
Selection.MoveRight Unit:=wdCell
Selection.TypeText (Format(rsInvoiceLines.Fields("DISC_AMT"), "#,###,##0.00"))
Selection.MoveRight Unit:=wdCell
Selection.TypeText (rsInvoiceLines.Fields("DATE_PAID"))
Selection.MoveRight Unit:=wdCell
rsInvoiceLines.MoveNext
If Not rsInvoiceLines.EOF Then
Selection.MoveRight Unit:=wdCell
End If
Loop
'rsInvoiceLines.Close
End Sub