I am updating a legacy Product Management Database to include Sales Order and Invoicing functionality.
I am struggling on how best to create the invoice detail records (converting SO detail records to INVC detail records) and could use some advice on approach and/or specific code to include in my onclick event.
Background
I created a header and a detail table for Sales Orders and a form to manage a given Sales Order (Frm_SalesOrder). It includes a pop-up sub-form (AddForm_SODetail) linked by SalesOrderID so the user can add/edit/save the SO detail (select part number, add quantity, confirm price, etc.). Similiarly, I created a header and a detail table for Invoices with a form to manage a given Invoice (Frm_INVC) and a sub-form to manage the detail (SubForm_INVCDetail).
On my SO form, I have added a "Ship & Invoice" button (Button_CreateINVOICE) with an On-Click Event to open the Frm_Invoice in add-mode and pre-populate various Invoice Header values (PK, FK and other invoice header constants that pull from the given Sales Order), thereby linking this new Invoice Header record to the open Sales Order. Where I have existing INVC detail records, they display correctly. SO, THIS ALL WORKS AS INTENDED!
Now, I am trying to add functionality to the Button_CreateINVOICE so that it also creates the required Invoice Detail records
> I want to take the SalesOrder Detail records tied to the Invoice's SalesOrder and create each of them as an Invoice Detail record, but only if if there are items still to be shipped on a given line (Tbl_SODetail.SOd_QtyOPEN > 0)
My first thought was to build an Append Query with the relevant fields and then execute that query within the Button_CreateINVOICE onclick event. However, I'm not sure how to add the FK (InvoiceID) to these new records. So, I am wondering if it's better to enter VBA code directly into the OnClick to bascially match the Append Query and update the InvoiceID, using the already defined variable (NextInvID) in the on-click procedure. I looked at the SQL of the append query with the INSERT / SELECT / FROM / WHERE statements, but wasn't successful at getting my syntax correct.
Does this approach make sense? Any suggestion on how to write the code? I'd be grateful for any advice.
GOAL:
A. Grab the associated SO_Detail records for the SalesOrderID selected in the current form
WHERE [Tbl_SODetail].[SOd_SalesOrderIDREF] = [Frm_SalesOrder].[SalesOrderID])
AND [Tbl_SODetail].[SOd_QtyOPEN] > 0.
B. Populate several fields
InvoiceIDREF = NextInvID (this is a variable created earlier in the code to populate the Invoice Header's PK [InvoiceID])
INVCd_SKU = [Tbl_SODetail].[SOd_SKU]
INVCd_PricePP = [Tbl_SODetail].[SOd_PricePP]
A few other comments:
> My SO/Invoice forms are tied to queries (i.e. SO_HeaderQuery, SO_DetailQuery, etc.) and not the actual tables so that I can pull in other associated data.
> I have separate SO and Invoice tables because I create several invoices against a given Sales Order.
> I know I could name my PK/FK the same between tables, but I didn't.
My tables/fields
Tbl_SOHeader
PK = SalesOrderID
Tbl_SODetail
PK = SOd_LineID
FK = SOd_SalesOrderIDREF (links to Sales Order Header)
SOd_SKU
SOd_PricePP
Tbl_INVCHeader
PK = InvoiceID
FK = SalesOrderIDREF (links to Sales Order Header)
Tbl_INVCDetail
PK = INVCd_LineID
FK = InvoiceIDREF (links to Invoice Header)
FK = SOd_LineIDREF (link to Sales Order Detail)
INVCd_SKU
INVCd_PricePP
I am struggling on how best to create the invoice detail records (converting SO detail records to INVC detail records) and could use some advice on approach and/or specific code to include in my onclick event.
Background
I created a header and a detail table for Sales Orders and a form to manage a given Sales Order (Frm_SalesOrder). It includes a pop-up sub-form (AddForm_SODetail) linked by SalesOrderID so the user can add/edit/save the SO detail (select part number, add quantity, confirm price, etc.). Similiarly, I created a header and a detail table for Invoices with a form to manage a given Invoice (Frm_INVC) and a sub-form to manage the detail (SubForm_INVCDetail).
On my SO form, I have added a "Ship & Invoice" button (Button_CreateINVOICE) with an On-Click Event to open the Frm_Invoice in add-mode and pre-populate various Invoice Header values (PK, FK and other invoice header constants that pull from the given Sales Order), thereby linking this new Invoice Header record to the open Sales Order. Where I have existing INVC detail records, they display correctly. SO, THIS ALL WORKS AS INTENDED!
Now, I am trying to add functionality to the Button_CreateINVOICE so that it also creates the required Invoice Detail records
> I want to take the SalesOrder Detail records tied to the Invoice's SalesOrder and create each of them as an Invoice Detail record, but only if if there are items still to be shipped on a given line (Tbl_SODetail.SOd_QtyOPEN > 0)
My first thought was to build an Append Query with the relevant fields and then execute that query within the Button_CreateINVOICE onclick event. However, I'm not sure how to add the FK (InvoiceID) to these new records. So, I am wondering if it's better to enter VBA code directly into the OnClick to bascially match the Append Query and update the InvoiceID, using the already defined variable (NextInvID) in the on-click procedure. I looked at the SQL of the append query with the INSERT / SELECT / FROM / WHERE statements, but wasn't successful at getting my syntax correct.
Does this approach make sense? Any suggestion on how to write the code? I'd be grateful for any advice.
GOAL:
A. Grab the associated SO_Detail records for the SalesOrderID selected in the current form
WHERE [Tbl_SODetail].[SOd_SalesOrderIDREF] = [Frm_SalesOrder].[SalesOrderID])
AND [Tbl_SODetail].[SOd_QtyOPEN] > 0.
B. Populate several fields
InvoiceIDREF = NextInvID (this is a variable created earlier in the code to populate the Invoice Header's PK [InvoiceID])
INVCd_SKU = [Tbl_SODetail].[SOd_SKU]
INVCd_PricePP = [Tbl_SODetail].[SOd_PricePP]
A few other comments:
> My SO/Invoice forms are tied to queries (i.e. SO_HeaderQuery, SO_DetailQuery, etc.) and not the actual tables so that I can pull in other associated data.
> I have separate SO and Invoice tables because I create several invoices against a given Sales Order.
> I know I could name my PK/FK the same between tables, but I didn't.
My tables/fields
Tbl_SOHeader
PK = SalesOrderID
Tbl_SODetail
PK = SOd_LineID
FK = SOd_SalesOrderIDREF (links to Sales Order Header)
SOd_SKU
SOd_PricePP
Tbl_INVCHeader
PK = InvoiceID
FK = SalesOrderIDREF (links to Sales Order Header)
Tbl_INVCDetail
PK = INVCd_LineID
FK = InvoiceIDREF (links to Invoice Header)
FK = SOd_LineIDREF (link to Sales Order Detail)
INVCd_SKU
INVCd_PricePP