将sql查询的结果存储到变量access vba中

时间:2017-03-10 15:26:51

标签: mysql vba access

我有sql查询,它根据三个标准返回一些字段。

  1. LOC字段有一个字符串" Alipore"
  2. 日期大于等于变量sdate,变量sdate从表单上的文本框中获取值。
  3. 日期小于等于变量edate,它从表单上的文本框中获取值。
  4. 但我收到错误"两个参数2"错误。

    Sub Test()
        Dim rs As DAO.Recordset
        Dim sqlMax As String
        Dim result As Integer
        Dim startd, endd As String
    
        startd = Me.txtsdate.Value
    
        endd = Me.txtedate.Value
    
        sqlMax = "SELECT Sum(Salesdata.FOOD) AS SumOfFOOD, Sum(Salesdata.LIQUORS) AS SumOfLIQUORS, Sum(Salesdata.SMARTPORTION) AS SumOfSMARTPORTION, Sum(Salesdata.[SP TAKEAWAY]) AS [SumOfSP TAKEAWAY]," _
         & "Sum(Salesdata.TAKEAWAY) AS SumOfTAKEAWAY, Sum(Salesdata.TAX_KKCESS02) AS SumOfTAX_KKCESS02, Sum(Salesdata.TAX_SBC020) AS SumOfTAX_SBC020, Sum(Salesdata.TAX_SERVICECHARGE) AS SumOfTAX_SERVICECHARGE," _
         & "Sum(Salesdata.TAX_VAT145) AS SumOfTAX_VAT145, Sum(Salesdata.AMEX) AS SumOfAMEX, Sum(Salesdata.CASH) AS SumOfCASH, Sum(Salesdata.MASTERCARD) AS SumOfMASTERCARD, Sum(Salesdata.VISA) AS SumOfVISA, Sum(Salesdata.OTHERS) AS SumOfOTHERS," _
         & "Sum(Salesdata.Vcloud) AS SumOfVcloud, Sum(Salesdata.MANAGERAC) AS SumOfMANAGERAC FROM Salesdata" _
         & "WHERE (((Salesdata.Loc) = 'Alipore'))" _
         & "HAVING (((Salesdata.DATE)>=" & startd & " And (Salesdata.DATE)<=" & endd & "));"
    

    设置rs = CurrentDb.OpenRecordset(sqlMax,dbOpenDynaset)

        Set rs = CurrentDb.OpenRecordset(sqlMax)
    
        result = rs.Fields(0)
    
        MsgBox result
    
        Set rs = Nothing
    
        rs.Close
    End Sub
    

1 个答案:

答案 0 :(得分:0)

天啊,我找到了它,它很简单。之前需要一个空间,以便它在访问中运行,但是当我将其复制到VBA并重新构建sql语句时。

应该是

&安培; &#34; WHERE(((Salesdata.Loc)=&#39; Alipore&#39;))&#34; _

而不是

&安培; &#34; WHERE(((Salesdata.Loc)=&#39; Alipore&#39;))&#34; _