Question : SQL Update Statement Help Needed - in an Access ADP

I have a table in a MS Access ADP application

I need help with a sql update statement

there is a table named tblInvoices
and a table named tblSalesOrders

when I generate Invoices - I append 1 invoice record for each customer that may  have any number of uninvoiced sales orders

I want to update the sales order table with the corresponding invoice no, where the invoice no in tblsalesorders = 0


step 1 - Invoices are generated - (uninvoiced sales order nos are appended to tblInvoices)
i am appending 1 invoice recordswhere a customer has 1 or more sales orders that are not invoiced


so a customer will have 1 invoice that may be associated with 1 or more sales orders..


tblInvoices -  invoiceID is autoincrementing
 cust id            InvoiceID      batchID
  12                  345             100    
  15                  346             100          


tblSalesorders  (still has no invoice associated)

Salesorder    custid     InvoiceNo  invoicetype
1000              12               0                 1
1001               15               0                1
1002               15               0                1


I want to update the data in tblSalesorders as follows....
update the sales order table where invoice type = 1 and invoice no = 0
with the new invoice no from the current batch

the batch id is generated from a batch table
i want tthis in the end....

tblSalesOrders
Salesorder    custid     InvoiceNo  invoicetype
1000              12               345                 1
1001               15               346                1
1002               15               346                1


I am using a command button in an access form to run the sql like this..

but the update below crashes.... something about <= and an expression???
I would rather write a new update stmt...

 strUpdate = "UPDATE dbo.tblShipments SET dbo.tblShipments.InvoiceNumber = (SELECT tblBI.InvoiceID FROM dbo.tblInvoiceBatch_Invoices tblBI WHERE (dbo.tblShipments.CustomerID = tblBI.CustomerID) AND (tblBI.InvoiceType = 1) AND (tblBI.InvoiceBatchID = " & Me.txtInvoiceBatchID & "))WHERE dbo.tblShipments.shipmentID IN (SELECT tblShipments.shipmentID from dbo.tblShipments INNER JOIN tblInvoiceBatch_Invoices tblBI1 ON tblShipments.InvoiceNumber = 0 AND (dbo.tblShipments.CustomerID = tblBI1.CustomerID)  AND (tblBI1.InvoiceType = 1))"

   
    ''MsgBox strUpdate
    DoCmd.RunSQL strUpdate





Thanks! - AB

Answer : SQL Update Statement Help Needed - in an Access ADP

At a core level - how about

strUpdate =   "UPDATE  tblSalesorders " & _
                    "SET  tblSalesorders.InvoiceNo= I.InvoiceID " & _
                    "FROM  " & _
                    "    tblSalesorders S " & _
                    "        INNER JOIN  " & _
                    "    tblInvoices I " & _
                    "        ON " & _
                    "            S.custid = I.custid " & _
                    "WHERE InvoiceNo = 0 AND invoicetype = 1 "

Random Solutions  
 
programming4us programming4us