I am trying to have a query written in the Microsoft Access Query designer see the selection my VBA code is making in a table.
Essentially I have the following VBA loop, it goes down the customer column in the table TBL_ForQB and executes the query "QB_Make_Model" if the customer is different a different query runs and the loop stops.
Main Problem:
How can I make the "QB_Make_Model" see the customer the VBA code is on, currently with my code it just inserts info from all the customer.
QB_Make_Model Query:
INSERT INTO InvoiceLine ( InvoiceLineDesc, FQSaveToCache )
SELECT DISTINCT t.Make_Model, 1
FROM TBL_ForQB AS t;
VBA Loop:
Dim dbs As Database
Set dbs = CurrentDb
Dim rst As Recordset
Set rst = dbs.OpenRecordset("TBL_ForQB")
Dim cust As String
Stop
rst.MoveFirst
Dim fcust As String
fcust = rst!Customer
MsgBox rst!Customer
Do Until rst.EOF
cust = rst!Customer
If cust = fcust Then
DoCmd.SetWarnings False
MsgBox "Same Customer"
DoCmd.OpenQuery "QB_Make_Model"
Else
DoCmd.OpenQuery "QB_Invoice_PK"
MsgBox "Diffeent Customer, I have Stopped and Sent to QB!"
'fcust = rst!Customer
'DoCmd.OpenQuery "QB_Make_Model_PK"
'DoCmd.OpenQuery "QB_Invoice_PK"
End If
rst.MoveNext
Loop
NEW ISSUE
@Parfait answer seemed to work correctly, the issue now is this:
Say there are two of the same Customer records in the table "TBL_ForQB" using the new parameter the query "QB_Make_Model" matches the customer name and VBA executes it. The problem is it sends all the rows in the Make_Model column of the "TBL_ForQB" that match the customer name. And does it as many times as there are Customers with the same name.
For ex:
I have a customer named "Customer1" with 2 records each of which include one Make_Model record (each) in the table "TBL_ForQB". The new VBA code will run and execute the Query "QB_Make_Model", which will match the Customer parameter but insert both records of "QB_Make_Model" for every time there is a customer record.
See the issue?