如何获得此SQL查询以查看VBA选择?

时间:2019-01-29 14:38:46

标签: sql vba ms-access

我试图用Microsoft Access查询设计器编写查询,以查看我的VBA代码在表中所做的选择。

基本上,我有以下VBA循环,它在表TBL_ForQB的customer列中向下移动,如果客户不同,则执行查询“ QB_Make_Model”,然后运行另一个查询,并且循环停止。

主要问题:

如何使“ QB_Make_Model”看到VBA代码所在的客户,目前与我的代码一起,它只是插入所有客户的信息。

QB_Make_Model查询:

INSERT INTO InvoiceLine ( InvoiceLineDesc, FQSaveToCache )
SELECT DISTINCT t.Make_Model, 1
FROM TBL_ForQB AS t;

VBA循环:

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

新问题

@Parfait答案似乎正常工作,现在的问题是:

说“ TBL_ForQB”表中有两个相同的客户记录,使用新参数,查询“ QB_Make_Model”与客户名称匹配,然后VBA执行该记录。问题是它会发送“ TBL_ForQB”的Make_Model列中与客户名称匹配的所有行。并执行的次数与具有相同名称的客户一样多。

例如:

我有一个名为“ Customer1”的客户,该客户有2条记录,每条记录在“ TBL_ForQB”表中都包含一个Make_Model记录(每个)。新的VBA代码将运行并执行查询“ QB_Make_Model”,该查询将与Customer参数相匹配,但每次有客户记录时都插入“ QB_Make_Model”的两个记录。

看到问题了吗?

3 个答案:

答案 0 :(得分:2)

您可以尝试修改查询以接受参数,然后使用DoCmd.SetParameter传递该参数。

带有参数的修改查询:

PARAMETERS pCustomer CHAR(255);
INSERT INTO InvoiceLine ( InvoiceLineDesc, FQSaveToCache )
SELECT DISTINCT t.Make_Model, 1
FROM TBL_ForQB AS t
WHERE Customer = pCustomer;

修改后的VBA以传递参数:

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.SetParameter "pCustomer", cust
        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

您可以在this answer中了解有关在VBA中使用参数的更多信息。我建议使用DAO方法,而不是依赖DoCmd语句的方法。

答案 1 :(得分:0)

由于您无法将客户作为参数传递给DoCmd.OpenQuery,因此您将需要另一种方法,例如在DataSheet View中打开报表或表单

Dim filter AS String

filter = "Customer=""" & fcust & """" 'Assuming String type
DoCmd.OpenForm "frmMakeModel", View:=acFormDS, WhereCondition:=filter, DataMode:=acFormReadOnly

或者如果CustomerLong id:

filter = "Customer=" & fcust 'Assuming Long type

然后,表单或报表的记录源必须是一个适当的查询,该查询在SELECT列表中包含一个Customer列。

答案 2 :(得分:0)

还考虑QueryDefs为客户名称参数传递给操作查询,无需DoCmd调用包括方法DoCmd.SetWarnings

<强> SQL (使用PARAMETERS与用于客户添加的列)

PARAMETERS C_Param TEXT(255), P_Param LONG;
INSERT INTO InvoiceLine (InvoiceLineDesc, FQSaveToCache )
SELECT DISTINCT t.Make_Model, 1
FROM TBL_ForQB AS t
WHERE Customer = C_Param
AND ProjectID = P_Param;

此外,可能要维持的客户柱(理想其ID)在 InvoiceLine 表可以由相同的参数来填充。

VBA

Dim dbs As Database
Dim qdef As QueryDef                  ' NEW OBJECT
Dim rst As Recordset
Dim cust As String, fcust As String

Set dbs = CurrentDb
Set qdef = CurrentDb.QueryDefs("QB_Make_Model")
Set rst = dbs.OpenRecordset("TBL_ForQB")

rst.MoveFirst    
fcust = rst!Customer
MsgBox rst!Customer

Do Until rst.EOF
    cust = rst!Customer
    If cust = fcust Then        
        MsgBox "Same Customer"
        qdef!C_Param = cust 
        qdef!C_Param = rst!ProjectID 
        qdef.Execute dbFailOnError
    Else
        dbs.Execute "QB_Invoice_PK"
        MsgBox "Different Customer, I have Stopped and Sent to QB!"
    End If
    rst.MoveNext
Loop

rst.Close 
Set rst = Nothing: Set qdef = Nothing: Set dbs = Nothing