我有VBA运行连接到具有日期范围的表单的查询。我试图编写一个IF语句,其中包含两个开始日期和结束日期的文本框。我在下面的IF语句中出现语法错误你能帮忙吗? :
Option Compare Database
Function ImportPurchases()
Dim qdfPassThrough As DAO.QueryDef
Dim MyDB As Database
Dim strConnect As String
Set db = CurrentDb()
DoCmd.SetWarnings False
Set qdfPassThrough = db.QueryDefs("Netezza_abc_purch_track")
If Not IsNull((Forms![DateRange].StartDate)) And Not IsNull((Forms![DateRange].EndDate)) Then
strSQL = "SELECT A.STORE_NBR,A.ITEM_NBR, A.NDC_NBR, C.BUSINESS_UNIT_NBR, C.INV_CUST_NAME ,C.BUSINESS_UNIT_NAME, Sum(SHIPPED_QTY)as Allocated_Qty, Sum(INV_LINE_AMT) as Extended_Cost FROM FCT_DLY_INVOICE_DETAIL A, FCT_DLY_INVOICE_HEADER B, DIM_INVOICE_CUSTOMER C WHERE A.INV_HDR_SK = B.INV_HDR_SK AND B.DIM_INV_CUST_SK = C.DIM_INV_CUST_SK AND A.STORE_NBR=B.STORE_NBR AND A.INV_DT BETWEEN (" & Forms![DateRange].StartDate & ")and (" & Forms![DateRange].EndDate & ")AND A.SUPPLIER_NBR NOT IN ('50000181', '20000775', '50000809', '50000950') AND A.SRC_SYS_CD = 'ABC' AND C.INV_CUST_NAME NOT LIKE '%340B%' AND C.BUSINESS_UNIT_NAME NOT LIKE '%340B%' Group by 1,2,3,4,5,6"
Else
strSQL = "ERROR"
End If
qdfPassThrough.SQL = strSQL
strConnect = "ODBC;DSN=NZSQL;Database=PRD_EDW_RXPURCHASING_DB;Server"
qdfPassThrough.Connect = strConnect
qdfPassThrough.Close
DoCmd.SetWarnings True
End Function
答案 0 :(得分:2)
If
关键字不能在同一语句中使用两次。删除第二个。
If Not IsNull(...) And Not IsNull(...) Then
strSQL = ...
Else
strSQL = ...
End If
(准确的错误信息是什么?)
(我想知道为什么你的VBA会丢失空格?编辑会清理所有这些内容,或者抛出对话框。我想我很好奇。)
截至上午9:00,您的代码版本具有此比较条款:
isnull >= (Forms![DateRange].StartDate)
我想你想要这样的东西:
IsNull((Forms![DateRange].StartDate))
作为一般建议,我建议您使用测试或试用思维模式制作一些最小的代码,学习构建模块,然后根据实际需要进行操作。
在每个模块的顶部键入Option Explicit
。然后使用Debug > Compile Database
。这将显示代码中的严重错误,并可能导致您解决语法错误。
答案 1 :(得分:0)
很可能你(也)需要为你的日期格式化正确的字符串表达式:
... (A.INV_DT Between '" & Format(Forms![DateRange]!StartDate, "yyyy\/mm\/dd") & "' And '" & Format(Forms![DateRange]!EndDate, "yyyy\/mm\/dd") & "') And A.SUPPLIER_NBR ...
答案 2 :(得分:0)
我最终得到了这个:
选项明确 Private Sub Command9_Click() ' JB函数ImportPurchases()将此更新为私有子,函数用于模块,并更新名称以反映按钮名称加_click() Dim db作为DAO.Database Dim qdf作为QueryDef
Dim qdfPassThrough As DAO.QueryDef
Dim MyDB As Database
Dim strConnect As String
'JB Dim SQLstr As String, you had this reversed in the sql :)
Dim strSQL As String
Dim DateStart As Date
Dim DateEnd As Date
Dim DateStart_string As String
Dim DateEnd_string As String
'Set db and querydef variables
Set db = CurrentDb
Set qdf = db.QueryDefs("Netezza_abc_purch_track")
'Initialize variables with values from the form
DateStart = Forms!DateRange!DateStart.Value
DateStart_string = Format(DateStart, "yyyy-mm-dd")
DateEnd = Forms!DateRange!DateEnd.Value
DateEnd_string = Format(DateEnd, "yyyy-mm-dd")
strSQL = "SELECT A.STORE_NBR,A.ITEM_NBR, A.NDC_NBR, C.BUSINESS_UNIT_NBR, C.INV_CUST_NAME ,C.BUSINESS_UNIT_NAME, Sum(SHIPPED_QTY)as Allocated_Qty, Sum(INV_LINE_AMT) as Extended_Cost " & _
"FROM FCT_DLY_INVOICE_DETAIL A, FCT_DLY_INVOICE_HEADER B, DIM_INVOICE_CUSTOMER C WHERE A.INV_HDR_SK = B.INV_HDR_SK AND B.DIM_INV_CUST_SK = C.DIM_INV_CUST_SK AND A.STORE_NBR=B.STORE_NBR " & _
"AND A.INV_DT BETWEEN ('" & DateStart_string & "') and ('" & DateEnd_string & "') AND A.SUPPLIER_NBR NOT IN ('50000181', '20000775', '50000809', '50000950') " & _
"AND A.SRC_SYS_CD = 'ABC' AND C.INV_CUST_NAME NOT LIKE '%340B%' AND C.BUSINESS_UNIT_NAME NOT LIKE '%340B%' Group by 1,2,3,4,5,6"
qdf.SQL = strSQL
Set db = Nothing
Set qdf = Nothing
'DoCmd.RunMacro "Macro1"
DoCmd.OpenQuery "Netezza_abc_purch_track"
End Sub