2015-06-26 35 views
0

我在Access GUI中運行它時收到了很長的查詢定義。我不想將它保存爲最終用戶可能意外運行的Query對象,所以我想通過VBA運行它。當我嘗試使用DoCmd.RunSQL運行它時,它會引發錯誤:無法在Access中通過vba運行查詢,但定義很長

「RunSQL操作需要包含SQL語句的參數。」

當我與mydatabase.Execute嘗試,我得到:

「在Microsoft Access引擎無法找到輸入表或查詢‘’請確保它存在並且其名稱拼寫正確。」

我做了一個Debug.Print並將輸出複製到Access GUI中的查詢中,在清理了行制動後,立即窗口添加了查詢,運行正常。查詢字符串長度爲5,293個字符。任何幫助,將不勝感激。

Dim dBase As Database 
Set dBase = CurrentDb() 
Dim stringSQL_3 As String 
stringSQL_3 = _ 
"INSERT INTO Measurement_Period_History (EMPLOYEE_NUMBER, PERIOD_START, PERIOD_END, DATE_REVIEWED, AVERAGE_HOURS, PERIOD_YEAR, PERIOD_TYPE_ID, FIRST_PAY_WEEK, LAST_PAY_WEEK) " & _ 
"SELECT FinalData.EMPLOYEE_NUMBER " & _ 
    ",FinalData.START_PERIOD " & _ 
    ",FinalData.END_PERIOD " & _ 
    ",Now() AS DATE_REVIEWED " & _ 
    ",IIF(FinalData.FLSA = 2, 40.00, SUM(FinalData.HOURS)/52) AS AVERAGE_HOURS " & _ 
    ",Cint(FinalData.PERIOD_YEAR) AS PERIOD_YEAR " & _ 
    ",FinalData.PERIOD_TYPE_ID " & _ 
    ",FinalData.FIRST_PAY_WEEK " & _ 
    ",FinalData.LAST_PAY_WEEK " 

stringSQL_3 = stringSQL_3 & _ 
"FROM (" & _ 
    "SELECT MeasurementData.EMPLOYEE_NUMBER " & _ 
     ",MeasurementData.FLSA " & _ 
     ",MeasurementData.STARTING_HIRE_DATE " & _ 
     ",MeasurementData.VARIABLE_EMPLOYEE " & _ 
     ",MeasurementData.START_PERIOD " & _ 
     ",MeasurementData.END_PERIOD " & _ 
     ",MeasurementData.PERIOD_TYPE_ID " & _ 
     ",MeasurementData.PRIOR_PERIOD_START " & _ 
     ",MeasurementData.PERIOD_YEAR " & _ 
     ",MeasurementData.FIRST_PAY_WEEK " & _ 
     ",MeasurementData.LAST_PAY_WEEK " & _ 
     ",IIf(IsNull(Payroll_History.HOURS), 0, Payroll_History.HOURS) AS HOURS " 

stringSQL_3 = stringSQL_3 & _ 
    "FROM Payroll_History " & _ 
    "RIGHT JOIN (" & _ 
     "SELECT BaseData.EMPLOYEE_NUMBER " & _ 
      ",BaseData.FLSA " & _ 
      ",BaseData.STARTING_HIRE_DATE " & _ 
      ",BaseData.VARIABLE_EMPLOYEE " & _ 
      ",BaseData.START_PERIOD " & _ 
      ",BaseData.END_PERIOD " & _ 
      ",BaseData.PERIOD_TYPE_ID " & _ 
      ",BaseData.PRIOR_PERIOD_START " & _ 
      ",BaseData.PERIOD_YEAR " & _ 
      ",DateAdd('d', 3, DateAdd('ww', - 52, DateAdd('d', - 2, DateAdd('ww', DateDiff('ww', 0, DateAdd('ww', IIf(DatePart('w', " & _ 
      "BaseData.END_PERIOD) >= 5, 0, - 1), BaseData.END_PERIOD)), 0)))) AS FIRST_PAY_WEEK " & _ 
      ",DateAdd('d', - 2, DateAdd('ww', DateDiff('ww', 0, DateAdd('ww', IIf(DatePart('w', BaseData.END_PERIOD) >= 5, 0, - 1), " & _ 
      "BaseData.END_PERIOD)), 0)) AS LAST_PAY_WEEK " 

stringSQL_3 = stringSQL_3 & _ 
     "FROM (" & _ 
      "SELECT ElgEmployees.EMPLOYEE_NUMBER " & _ 
       ",ElgEmployees.FLSA " & _ 
       ",ElgEmployees.STARTING_HIRE_DATE " & _ 
       ",ElgEmployees.VARIABLE_EMPLOYEE " & _ 
       ",IIF(ElgEmployees.VARIABLE_EMPLOYEE = No, DateSerial(" & DatePickerYear & " - 1, 4, 1), " & _ 
       "IIF(PeriodInfo.PRIOR_PERIOD_START IS NOT NULL, DateSerial(" & DatePickerYear & " - 1, DatePart('m', " & _ 
       "PeriodInfo.PRIOR_PERIOD_START), 1), DateSerial(" & DatePickerYear & " - 1, DatePart('m', ElgEmployees.STARTING_HIRE_DATE), 1))) AS START_PERIOD " & _ 
       ",IIF(ElgEmployees.VARIABLE_EMPLOYEE = No, DateAdd('d', - 1, DateSerial(" & DatePickerYear & ", 4, 1)), DateAdd('d', - 1, " & _ 
       "IIF(PeriodInfo.PRIOR_PERIOD_START IS NOT NULL, DateSerial(" & DatePickerYear & ", DatePart('m', PeriodInfo.PRIOR_PERIOD_START), 1), " & _ 
       "DateSerial(" & DatePickerYear & ", DatePart('m', ElgEmployees.STARTING_HIRE_DATE), 1)))) AS END_PERIOD " & _ 
       "," & DatePickerYear & " AS PERIOD_YEAR " & _ 
       ",PeriodInfo.PERIOD_TYPE_ID " & _ 
       ",PeriodInfo.PRIOR_PERIOD_START " 

stringSQL_3 = stringSQL_3 & _ 
      "FROM (" & _ 
       "SELECT Employee.EMPLOYEE_NUMBER " & _ 
        ",Employee.FLSA " & _ 
        ",Employee.VARIABLE_EMPLOYEE " & _ 
        ",IIf(Day(Employee.CURRENT_HIRE_DATE) = 1, Employee.CURRENT_HIRE_DATE, DateSerial(DatePart('yyyy', DateAdd('m', 1, " & _ 
        "Employee.CURRENT_HIRE_DATE)), DatePart('m', DateAdd('m', 1, Employee.CURRENT_HIRE_DATE)), 1)) AS STARTING_HIRE_DATE " 

stringSQL_3 = stringSQL_3 & _ 
       "FROM Employee " & _ 
       "WHERE (" & _ 
         "((Employee.EMPLOYEE_NUMBER)" & EmployeeNumberText & ") " & _ 
         "AND ((IIf(Day(Employee.CURRENT_HIRE_DATE) = 1, Employee.CURRENT_HIRE_DATE, DateSerial(DatePart('yyyy', DateAdd('m', 1, " & _ 
         "Employee.CURRENT_HIRE_DATE)), DatePart('m', DateAdd('m', 1, Employee.CURRENT_HIRE_DATE)), 1))) < DateAdd('y', - 1, DATE())) " & _ 
         "AND ((Employee.CURRENT_TERMINATION_DATE) IS NULL) " & _ 
         ") " & _ 
       ") AS ElgEmployees " & _ 
      "INNER JOIN (" & _ 
       "SELECT Employee.EMPLOYEE_NUMBER " & _ 
        ",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, NULL, Measurement_Period_History.PERIOD_YEAR) AS PRIOR_PERIOD_YEAR " & _ 
        ",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, IIF(Employee.VARIABLE_EMPLOYEE = No, 2, 0), " & _ 
        "IIF(Employee.VARIABLE_EMPLOYEE = No, 2, IIF(Measurement_Period_History.PERIOD_END < Employee.CURRENT_HIRE_DATE, 0, " & _ 
        "IIF(Measurement_Period_History.AVERAGE_HOURS >= 30, 1, 0)))) AS PERIOD_TYPE_ID " & _ 
        ",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, NULL, IIF(Measurement_Period_History.PERIOD_END < " & _ 
        "Employee.CURRENT_HIRE_DATE, NULL, Measurement_Period_History.PERIOD_END)) AS PRIOR_PERIOD_END " & _ 
        ",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, NULL, IIF(Measurement_Period_History.PERIOD_END < " & _ 
        "Employee.CURRENT_HIRE_DATE, NULL, Measurement_Period_History.PERIOD_START)) AS PRIOR_PERIOD_START " 

stringSQL_3 = stringSQL_3 & _ 
       "FROM Measurement_Period_History " & _ 
       "RIGHT JOIN Employee ON Measurement_Period_History.EMPLOYEE_NUMBER = Employee.EMPLOYEE_NUMBER " & _ 
       "WHERE ((Employee.CURRENT_TERMINATION_DATE) Is Null) AND (Employee.EMPLOYEE_NUMBER" & EmployeeNumberText & ") " & _ 
       "GROUP BY Employee.EMPLOYEE_NUMBER " & _ 
        ",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, NULL, Measurement_Period_History.PERIOD_YEAR) " & _ 
        ",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, IIF(Employee.VARIABLE_EMPLOYEE = No, 2, 0), " & _ 
        "IIF(Employee.VARIABLE_EMPLOYEE = No, 2, IIF(Measurement_Period_History.PERIOD_END < Employee.CURRENT_HIRE_DATE, 0, " & _ 
        "IIF(Measurement_Period_History.AVERAGE_HOURS >= 30, 1, 0)))) " & _ 
        ",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, NULL, IIF(Measurement_Period_History.PERIOD_END < " & _ 
        "Employee.CURRENT_HIRE_DATE, NULL, Measurement_Period_History.PERIOD_END)) " & _ 
        ",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, NULL, IIF(Measurement_Period_History.PERIOD_END < " & _ 
        "Employee.CURRENT_HIRE_DATE, NULL, Measurement_Period_History.PERIOD_START)) " & _ 
       ") AS PeriodInfo ON ElgEmployees.EMPLOYEE_NUMBER = PeriodInfo.EMPLOYEE_NUMBER " & _ 
      ") AS BaseData " & _ 
     ") AS MeasurementData ON Payroll_History.EMPLOYEE_NUMBER = MeasurementData.EMPLOYEE_NUMBER " & _ 
    "WHERE ((Payroll_History.PAY_DATE) Is Null) " & _ 
     "OR (" & _ 
      "(Payroll_History.PAY_DATE) >= MeasurementData.FIRST_PAY_WEEK " & _ 
      "AND (Payroll_History.PAY_DATE) <= MeasurementData.LAST_PAY_WEEK " & _ 
      ") " & _ 
    ") AS FinalData " 

stringSQL_3 = stringSQL_3 & _ 
"WHERE LAST_PAY_WEEK < Now() " & _ 
"GROUP BY EMPLOYEE_NUMBER " & _ 
    ",FLSA " & _ 
    ",STARTING_HIRE_DATE " & _ 
    ",VARIABLE_EMPLOYEE " & _ 
    ",START_PERIOD " & _ 
    ",END_PERIOD " & _ 
    ",PERIOD_TYPE_ID " & _ 
    ",PRIOR_PERIOD_START " & _ 
    ",PERIOD_YEAR " & _ 
    ",FIRST_PAY_WEEK " & _ 
    ",LAST_PAY_WEEK " 


    Debug.Print stringSQL_3 
    Debug.Print Len(stringSQL_3) 
    dBase.Execute string_3 
+2

當你從'dBase.Execute string_3'中得到這個錯誤時,'string_3'中包含的字符串是什麼? – HansUp

+2

不是什麼都好,但我確信這是保存查詢存在的原因之一。有更好的方法來保護您的Access對象而不是將它們串化。 –

+0

羅伯特是對的。你讓自己難過。你應該重新思考這個概念。 – Gustav

回答

2

是不是你的SQL在stringSQL_3?其他字符串(string_3)中有什麼?這可能是這個問題,你試圖執行錯誤的字符串?

+0

對。沒有string_3。請註明'Option Explicit'。 – Gustav

+0

我不是一個Access程序員,不適用於變量定義如此鬆散的語言。我幾乎完成了這個項目,但在任何地方添加了Option Explicit。非常感謝!現在,我只需要忍受一個愚蠢的錯誤,這個錯誤將會在我的餘生中留在互聯網上。 – Mrphin

+0

@Mrphin,發生在我們所有的一點或另一個。不要流汗:) –

相關問題