SQL通过Excel VBA选择查询 - 定义参数,查询查询

时间:2015-01-06 12:12:49

标签: sql excel vba excel-vba

我需要将3个参数放入从Excel VBA到SQL的Select查询中 - 其中一个可以使用变量替换。但是此查询是另一个查询的查询,并且参数保存在这两个其他查询中。如果在Access中运行此查询,我只是提示所有三个人手动输入它们 - “开始”,“结束”和“AdvisorName”。

运行代码将提示“没有给出一个或多个必需参数的值” - 但是,此查询中只有1个参数,其他2个参数保存在此查询内的其他两个查询中 - “Q_SoloFocus_Advisor_QuestionsYes”和“Q_SoloFocus_Advisor_QuestionsNo”。

这三个参数称为“开始”(开始日期范围),“结束”(结束日期范围)和“AdvisorName”(在此查询中保存)。

Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim Command As New ADODB.Command
Dim strSQL As String

Set cnn = New ADODB.Connection
cnn.Open ConnectionString:=Cnct
Set rst = New ADODB.Recordset

strSQL = "SELECT Q_SoloFocus_Advisor_QuestionsAll.Advisor, 
          Q_SoloFocus_Advisor_QuestionsAll.KeyID,
          Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text, 
          Q_SoloFocus_Advisor_QuestionsAll.CountOfAnswer AS [All], 
          Q_SoloFocus_Advisor_QuestionsNo.CountOfAnswer AS [No], 
          Q_SoloFocus_Advisor_QuestionsYes.CountOfAnswer
          AS Yes," & _"Format(Q_SoloFocus_Advisor_QuestionsYes.CountOfAnswer/
          Q_SoloFocus_Advisor_QuestionsAll.CountOfAnswer,'0.0%') 
          AS Result" & _" 
 FROM    (Q_SoloFocus_Advisor_QuestionsAll 
         LEFT JOIN Q_SoloFocus_Advisor_QuestionsNo 
 ON      (Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text = 
                Q_SoloFocus_Advisor_QuestionsNo.SubQ_Text)
 AND     (Q_SoloFocus_Advisor_QuestionsAll.KeyID = 
                Q_SoloFocus_Advisor_QuestionsNo.KeyID)
 AND     (Q_SoloFocus_Advisor_QuestionsAll.Advisor = 
                Q_SoloFocus_Advisor_QuestionsNo.Advisor))
 LEFT JOIN Q_SoloFocus_Advisor_QuestionsYes 
 ON     (Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text =
                Q_SoloFocus_Advisor_QuestionsYes.SubQ_Text) 
 AND    (Q_SoloFocus_Advisor_QuestionsAll.Advisor =
                Q_SoloFocus_Advisor_QuestionsYes.Advisor)
 AND    (Q_SoloFocus_Advisor_QuestionsAll.KeyID =
                Q_SoloFocus_Advisor_QuestionsYes.KeyID)" & _
        " WHERE (((Q_SoloFocus_Advisor_QuestionsAll.Advisor)=[AdvisorName]));"

'----------------------------------------------------------------------------------

'----------------------------------------------------------------------------------

rst.Open strSQL, cnn, adOpenStatic

rst.MoveFirst

其他两个查询的SQL类似,一个具有“否”,另一个不需要参数的“否”是:

SELECT     tbl_Surveys.Advisor, tbl_QuestionRef.KeyID, tbl_QuestionRef.CallSkill, 
           tbl_QuestionRef.CallReason, tbl_QuestionRef.MainQ_Text
           tbl_QuestionRef.SubQ_Text,
           Count(tbl_SurveyAnswers.Answer) AS CountOfAnswer
FROM       tbl_QuestionRef 
INNER JOIN (tbl_SurveyAnswers 
           INNER JOIN tbl_Surveys
           ON tbl_SurveyAnswers.SurveyLink = tbl_Surveys.ID) 
ON         tbl_QuestionRef.KeyID = tbl_SurveyAnswers.QuestionRef
WHERE      (((tbl_SurveyAnswers.Answer)<>"No" 
           And (tbl_SurveyAnswers.Answer)<>"N/A" 
           And (tbl_SurveyAnswers.Answer)<>"0") 
           AND ((tbl_Surveys.CallDate)>=[Start] 
           And (tbl_Surveys.CallDate)<=[End]))
GROUP BY   tbl_Surveys.Advisor, tbl_QuestionRef.KeyID, tbl_QuestionRef.CallSkill, 
           tbl_QuestionRef.CallReason, 
           tbl_QuestionRef.MainQ_Text, tbl_QuestionRef.SubQ_Text;

正如您所看到的,“开始”和“结束”参数位于第二个SQL查询中......任何想法如何将这两个参数放入第一个SQL函数中?我无法将“开始”和“结束”置于“全部”查询中,因为它会删除“计数”部分......

1 个答案:

答案 0 :(得分:0)

假设保存并命名了另外两个查询,而不是调用命名查询,将两个查询中的SQL作为派生表合并到主查询中。通过这样做,查询将提示所有参数。有关说明,请参阅JOIN条款。

strSQL = "SELECT Q_SoloFocus_Advisor_QuestionsAll.Advisor, "
          Q_SoloFocus_Advisor_QuestionsAll.KeyID,
          Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text,
          Q_SoloFocus_Advisor_QuestionsAll.CountOfAnswer AS [All],
          Q_SoloFocus_Advisor_QuestionsNo.CountOfAnswer AS [No],
          Q_SoloFocus_Advisor_QuestionsYes.CountOfAnswer
          AS Yes," & _"Format(Q_SoloFocus_Advisor_QuestionsYes.CountOfAnswer/
          Q_SoloFocus_Advisor_QuestionsAll.CountOfAnswer,'0.0%')
          AS Result" & _"
 FROM    (Q_SoloFocus_Advisor_QuestionsAll
         LEFT JOIN (YOUR FIRST SAVE QUERY SQL) AS Q_SoloFocus_Advisor_QuestionsNo
 ON      (Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text =
                Q_SoloFocus_Advisor_QuestionsNo.SubQ_Text)
 AND     (Q_SoloFocus_Advisor_QuestionsAll.KeyID =
                Q_SoloFocus_Advisor_QuestionsNo.KeyID)
 AND     (Q_SoloFocus_Advisor_QuestionsAll.Advisor =
                Q_SoloFocus_Advisor_QuestionsNo.Advisor))
 LEFT JOIN (YOUR SECOND QUWERY SQL) AS Q_SoloFocus_Advisor_QuestionsYes
 ON     (Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text =
                Q_SoloFocus_Advisor_QuestionsYes.SubQ_Text)
 AND    (Q_SoloFocus_Advisor_QuestionsAll.Advisor =
                Q_SoloFocus_Advisor_QuestionsYes.Advisor)
 AND    (Q_SoloFocus_Advisor_QuestionsAll.KeyID =
                Q_SoloFocus_Advisor_QuestionsYes.KeyID)" & _
        " WHERE (((Q_SoloFocus_Advisor_QuestionsAll.Advisor)=[AdvisorName]));"