Sub ExportInvoiceLines()
Dim rs As DAO.Recordset, db As DAO.Database, strSQL As String, strTxnID As String
Set db = CurrentDb
'********** INSERT INVOICE LINE **********
'Open the recordset that contains the InvoiceLine data you are inserting
Set rs = db.OpenRecordset("tblInvoiceLine_RL")
With rs
.MoveFirst
strTxnID = rs!TxnID
'iterate through the table fields
For x = 0 To rs.Fields.Count - 1
'do not use null values or customfield values
If IsNull(rs.Fields(x).Value) = False And _
InStr(1, rs.Fields(x).name, "customfield", vbTextCompare) = 0 And _
rs.Fields(x).name <> "InvoiceLineTxnLineID" Then
Debug.Print rs.Fields(x).name & ": " & rs.Fields(x).Type
'Debug.Print rs.Fields(x).name & ": " & rs.Fields(x).FieldSize
'add fields to the field string for the sql
strFields = strFields & Chr(34) & rs.Fields(x).name & Chr(34) & ", "
'add values to the value string
If rs.Fields(x).Type = 20 Then
'do not enclose number values in quotes
strValues = strValues & Nz(rs.Fields(x).Value, "") & ","
ElseIf rs.Fields(x).Type = 8 Then
strValues = strValues & "{d '" & Year(rs.Fields(x).Value) & "-" & Right("00" & Month(rs.Fields(x).Value), 2) & "-" & Right("00" & Day(rs.Fields(x).Value), 2) & "'}" & ","
Else
'enclose text values in quotes
strValues = strValues & "'" & Replace(rs.Fields(x).Value, "'", "`") & "',"
End If
End If
Next x
End With
rs.Close
'add the SaveToCache field and value
strFields = strFields & Chr(34) & "FQSaveToCache" & Chr(34)
strValues = strValues & "0"
'add the beginning of the SQL plus the fields and values and TxnID of the invoice
strSQL = "INSERT INTO INVOICELINE (" & strFields & ") VALUES (" & strValues & ") "
Dim qd As DAO.QueryDef, q As String
q = "qryTemp"
'call a function to delete existing queries or manually delete it if it already exists
Call fncDeleteQuery(q)
'create a QODBC query
Set qd = db.CreateQueryDef(q)
qd.Connect = "ODBC;DSN=QuickBooks Data;SERVER=QODBC"
qd.ReturnsRecords = False
qd.sql = strSQL
Debug.Print "Insert Query:"
Debug.Print qd.sql
Debug.Print
qd.Execute
'********** UPDATE CUSTOM FIELDS **********
Dim strInvoiceLineTxnLineID As String
'change the query to return records this time
qd.ReturnsRecords = True
qd.ODBCTimeout = 30
'Retrieve the InvoiceLines from the Invoice using the TxnID
qd.sql = "select txnid,InvoiceLineTxnLineID FROM INVOICELINE WHERE TXNID='" & strTxnID & "'"
'get the InvoiceLineTxnLineID from the last record in the query
Set rs = db.OpenRecordset(q)
rs.MoveLast
strInvoiceLineTxnLineID = rs!InvoiceLineTxnLineID
'change the recordset to the table again to get the values you want to update
Set rs = db.OpenRecordset("tblInvoiceLine_RL")
rs.MoveFirst
'change the query to update this time
qd.ReturnsRecords = False
qd.sql = "update invoiceline set CustomFieldInvoiceLineCUSTOM2='" & rs!CustomFieldInvoiceLineCUSTOM2 & "'" & _
" where txnid='" & strTxnID & "' and invoicelinetxnlineid='" & strInvoiceLineTxnLineID & "'"
Debug.Print "Update Query:"
Debug.Print qd.sql
Debug.Print
qd.Execute
'************* CONFIRM THE INSERT ************
'declare a boolean to hold the value of True or False for confirmation
Dim blnConfirmed As Boolean
'set the boolean's value to true and it will only change if values do not match
blnConfirmed = True
'change the query to return records
170 qd.ReturnsRecords = True
180 qd.ODBCTimeout = 30
'select the fields you need that match the fields in your form's table and put them into a confirmation table
190 qd.sql = "select InvoiceLineTxnLineID,InvoiceLineItemRefFullName," & _
"InvoiceLineQuantity,CustomFieldInvoiceLineCUSTOM2 FROM INVOICELINE WHERE TXNID='" & strTxnID & _
"' and invoicelinetxnlineid='" & strInvoiceLineTxnLineID & "'"
Debug.Print "Confirmation Query:"
Debug.Print qd.sql
Debug.Print
'create a table from the returned records
DoCmd.SetWarnings False
DoCmd.RunSQL "SELECT * INTO tblConfirmInvoiceLines_RL FROM " & q
DoCmd.SetWarnings True
'rs is already set to the form's recordsource table: "tblInvoiceLine_RL"
'create rs2 recordset and set it to the confirmation table you created above
Dim rs2 As DAO.Recordset, y As Integer
Set rs2 = db.OpenRecordset("tblConfirmInvoiceLines_RL")
'move to the last record because that will be that last inserted invoiceline
rs2.MoveLast
Debug.Print "Compare Values"
'iterate through the fields of each recordset
For x = 0 To rs.Fields.Count - 1
For y = 0 To rs2.Fields.Count - 1
'if the field names match, compare the values but do not compare invoicelinetxnlineid
If rs.Fields(x).name = rs2.Fields(y).name And rs.Fields(x).name <> "invoicelinetxnlineid" Then
If rs.Fields(x).Value <> rs2.Fields(y).Value Then
Debug.Print rs.Fields(x).name & ": " & rs.Fields(x).Value & " <> " & rs2.Fields(y).Value
blnConfirmed = False
Else
Debug.Print rs.Fields(x).name & ": " & rs.Fields(x).Value & " = " & rs2.Fields(y).Value
End If
End If 'rs.Fields(x).name = rs2.Fields(y).name
Next y
Next x
'let user know confirmation results
If blnConfirmed = False Then
MsgBox "Insert incomplete."
Else
MsgBox "Insert confirmed."
End If
Set qd = Nothing
rs.Close
rs2.Close
Set rs = Nothing
Set rs2 = Nothing
Set db = Nothing
End Sub
Function fncInvoiceLineTable()
On Error GoTo fncInvoiceLineTable_err
Dim db As DAO.Database, rs As DAO.Recordset, qd As DAO.QueryDef, q As String, qbDate As String
'delete this query if it already exists
Set db = CurrentDb
qbDate = "{d '" & Year(Now) & "-" & Right("00" & Month(Now), 2) & "-" & Right("00" & Day(Now), 2) & "'}"
q = "qryTemp"
Set qd = db.CreateQueryDef(q)
qd.Connect = "ODBC;DSN=QuickBooks Data;SERVER=QODBC"
qd.ReturnsRecords = True
qd.ODBCTimeout = 60
qd.sql = "select TxnID,TxnDate,RefNumber,InvoiceLineQuantity,CustomFieldInvoiceLineOther1,CustomFieldInvoiceLineOther2," & _
"CustomFieldInvoiceLineCustom2,InvoiceLineItemRefFullName from InvoiceLine where " & _
" RefNumber='TestInvoice' and txnDate=" & qbDate
Debug.Print qd.sql
DoCmd.SetWarnings False
DoCmd.RunSQL "select * into tblInvoiceLine_RL from " & q
fncInvoiceLineTable_exit:
DoCmd.SetWarnings True
Set qd = Nothing
Set db = Nothing
DoCmd.OpenTable "tblInvoiceLine_RL"
Exit Function
fncInvoiceLineTable_err:
Debug.Print Err.Number & ": " & Err.Description
If Err.Description = "Object 'qryTemp' already exists." Then
DoCmd.DeleteObject acQuery, "qryTemp"
Resume
End If
End Function
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)