Parent Child
Invoice InvoiceLine
Estimate EstimateLine
CreditMemo CreditMemoLine
Constant #2: The unique identifier for these parent tables is TxnID.
Invoice: TxnID InvoiceLine: InvoiceLineTxnLineID
Estimate: TxnID EstimateLine: EstimateLineTxnLineID
CreditMemo: TxnID CreditMemoLine: CreditMemoLineTxnLineID
I can see the smoke wafting out your ears from here as your brain jumps ahead to the possibilities this offers. Slow down so we can lay out how to apply this to your database.
InvoiceLine EstimateLine CreditMemoLine
----------- ------------ --------------
TxnID TxnID TxnID
TxnDate TxnDate TxnDate
RefNumber RefNumber RefNumber
InvoiceLineTxnLineID EstimateLineTxnLineID CreditMemoLineTxnLineID
InvoiceLineMemo EstimateLineMemo CreditMemoLineMemo
InvoiceLineRate EstimateLineRate CreditMemoLineRate
To see all available field names for a specific table, type
sp_columns tablename into the VB Demo that comes with QODBC. Replace
tablename with the table of your choice.
Sub Import(sTableName As String, QBTableName As String, dtFrom As Date, dtTo As Date)
On Error GoTo Import_err
'*** create sql
100 Dim sSQL As String
110 sSQL = "select " & sQBFields(QBTableName) & " from " & QBTableName & " where txndate>=" & fQBDate(dtFrom) & " and txndate<=" & fQBDate(dtTo)
'*** create pass-thru query
200 Dim db As DAO.Database, qd As DAO.QueryDef
210 Set db = CurrentDb
220 Set qd = db.CreateQueryDef("qryTemp")
230 qd.Connect = "ODBC;DSN=QuickBooks Data;SERVER=QODBC"
240 qd.ReturnsRecords = True
250 qd.ODBCTimeout = 60
260 qd.SQL = sSQL
270 Set qd = Nothing
280 Set db = Nothing
'*** import into table
300 DoCmd.RunSQL "select '" & QBTableName & "' as QBTableName ," & sDBfields(QBTableName) & " into " & sTableName & " from qryTemp"
Exit Sub
Import_err:
If InStr(1, err.Description, "Object 'qryTemp'", vbTextCompare) > 0 Then
DoCmd.DeleteObject acQuery, "qryTemp"
Resume
End If
Debug.Print "Sub Import", Erl, err.Number, err.Description
End Sub
Function fQBDate(myDate As Date) As String
fQBDate = "{d '" & Year(myDate) & "-" & Right("00" & Month(myDate), 2) & "-" & Right("00" & Day(myDate), 2) & "'}"
End Function
Function sQBFields(QBTableName As String) As String
On Error GoTo sQBFields_err
'*** open the Fields table for the QBTable you're importing as a recordset
100 Dim db As DAO.Database, RS As DAO.Recordset, i As Integer
110 Set db = CurrentDb
120 Set RS = db.OpenRecordset("SELECT COLUMNNAME FROM FIELDS_" & QBTableName)
130 If RS.RecordCount > 0 Then
140 RS.MoveLast
150 RS.MoveFirst
160 Do While Not RS.EOF
170 sQBFields = sQBFields & RS!COLUMNNAME & ","
180 RS.MoveNext
190 Loop
200 End If
210 RS.Close
220 Set RS = Nothing
230 Set db = Nothing
240 If Nz(sQBFields, "") <> "" Then sQBFields = Left(sQBFields, Len(sQBFields) - 1)
Exit Function
sQBFields_err:
Debug.Print "Function sQBFields", Erl, err.Number, err.Description
End Function
Function sDBfields(QBTableName As String) As String
On Error GoTo sDBfields_err
'*** open the Fields table for the QBTable you imported
100 Dim db As DAO.Database, RS As DAO.Recordset, i As Integer
110 Set db = CurrentDb
120 Set RS = db.OpenRecordset("SELECT COLUMNNAME FROM FIELDS_" & QBTableName)
130 If RS.RecordCount > 0 Then
140 RS.MoveLast
150 RS.MoveFirst
160 Do While Not RS.EOF
170 sDBfields = sDBfields & RS!COLUMNNAME
'*** if the imported table is NOT InvoiceLine, replace the pertinent field names with "InvoiceLine"
'*** for example, if you imported the CreditMemoLine table, replace all the fields containing the
'*** string "CreditMemo" (QBTableName) with "InvoiceLine"
180 If QBTableName <> "InvoiceLine" And InStr(1, RS!COLUMNNAME, "line", vbTextCompare) > 0 Then
190 sDBfields = sDBfields & " as " & Replace(RS!COLUMNNAME, QBTableName, "InvoiceLine")
200 End If
210 sDBfields = sDBfields & ","
220 RS.MoveNext
230 Loop
240 End If
250 RS.Close
260 Set RS = Nothing
270 Set db = Nothing
280 If Nz(sDBfields, "") <> "" Then sDBfields = Left(sDBfields, Len(sDBfields) - 1)
Exit Function
sDBfields_err:
Debug.Print "Function sDBfields", Erl, err.Number, err.Description
End Function
Now you only need one form or report to for all three types: EstimateLines, InvoiceLines and CreditMemoLines. You can also import all three types into separate tables and put them into one big table using a union query. Each transaction will denote what type it is in the field: QBTableName.
Call import("tblTemp","creditmemoline","1/1/15","2/2/15")
Once you understand the principle, you'll want to use it for other transaction types and create procedures that export from a single form as well.
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)