Excel表到Access查询连接,[Microsoft] [ODBC Microsoft Access Drive]参数太少。预期1

时间:2017-03-28 11:41:42

标签: sql excel ms-access odbc

我正在尝试在Excel中创建一个表,该表从Access Query获取数据。我无法在Data-> From Access下找到此查询。我正在使用Data->来自其他来源 - >来自数据连接向导 - > ODBC DSN。在最后一步它抛出错误[Microsoft] [ODBC Microsoft Access Drive]太少的参数。预期1.

此刻我不会发布完整查询,很长

我将发布子查询部分(带有一些格式),已经抛出此错误。有人可以看看并找出问题所在。

我在Access中正常工作的所有查询。但我需要将结果导出到Excel,因为整个报告VBA工具都在那里。 (我知道我可以创建SELECT INTO并创建表格,但更新并不那么优雅和简单)谢谢大家的时间。祝你有愉快的一天

SELECT 
Employees.PersNo, 
Employees.Employee_name, 
Employees.Reporting_Month, 
Employees.Gender_Key, 
Employees.Start_Date, 
Employees.Business_Unit, 
Employees.Position_ID, 
Employees.Position, 
Employees.Local_Band, 
Employees.PS_Group, 
Employees.Wage_Amount, 
val(Employees.Bonus) AS [Bonus_%], 
val([Employees].[Commissions_(%)]) AS [Commisions_%], 
Employees.Wage_type, Employees.Wkhrs, 
Q1.Business_Unit, 
Q1.Position_ID, 
Q1.Position, 
Q1.Local_Band, 
Q1.PS_Group, 
Q1.Wage_Amount, 
[Q1].[Bonus_%], 
[Q1].[Commisions_%], 
Employees.Wage_type, 
Employees.Wkhrs,
Employees.Evid_Status



FROM Employees LEFT JOIN (SELECT 
Dateadd("m",1,[Employees.Reporting_Month]) AS Reporting_Month, 
Employees.PersNo, 
Employees.Local_Band, 
Employees.PS_Group, 
Employees.Wage_Amount, 
val(Employees.Bonus) AS [Bonus_%], 
val([Employees].[Commissions_(%)]) AS [Commisions_%], 
Employees.Wage_type, Employees.Wkhrs, 
Employees.Business_Unit, 
Employees.Position_ID, 
Employees.Position,
Employees.Evid_Status
FROM Employees WHERE Employees.Evid_Status=1 )  AS Q1 
ON (Employees.Reporting_Month = [Q1].[Reporting_Month]) AND (Employees.PersNo = [Q1].[PersNo])
WHERE Employees.Evid_Status=1;

3 个答案:

答案 0 :(得分:0)

您可以使用Excel查询Access,就像您在下面的链接中看到的那样。

http://codepen.io/anon/pen/evxyNb

另外,请考虑使用参数查询从Access到Excel进行导出。

.col-50 {
  width: 50%;
  float: left;
  position: relative;
  min-height: 1px;
  padding-left: 0.75rem;
  padding-right: 0.75rem;
}

或者......从Access to Excel中的记录集中写入数据。

Dim dbs As DAO.Database
Dim qdfTemp As DAO.QueryDef
Dim strSQL As String, strQDF As String
Set dbs = CurrentDb

' Replace NameOfTableOrQuery with the real name of the table or query,
' replace NameOfForm with the real name of the form, and replace
' ADateControlOnForm and AnotherDateControlOnForm with the real names
' of the controls on that form
strSQL = "SELECT NameOfTableOrQuery.* FROM NameOfTableOrQuery " & _
      "WHERE NameOfTableOrQuery.FieldName >= " & _
      Format(Forms!NameOfForm!ADateControlOnForm.Value,"\#mm\/dd\/yyyy\#") & _
      " And NameOfTableOrQuery.FieldName <=" & _
      Format(Forms!NameOfForm!AnotherDateControlOnForm.Value,"\#mm\/dd\/yyyy\#") & "';"

strQDF = "_TempQuery_"
Set qdfTemp = dbs.CreateQueryDef(strQDF, strSQL)
qdfTemp.Close
Set qdfTemp = Nothing

' Replace C:\MyFolderName\MyFileName.xls with the real path and filename for the
' EXCEL file that is to contain the exported data
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
      strQDF,"C:\MyFolderName\MyFileName.xls"

dbs.QueryDefs.Delete strQDF
dbs.Close
Set dbs = Nothing

或者,只需将数据从Access导入Excel即可。

Dim lngColumn As Long
Dim xlx As Object, xlw As Object, xls As Object, xlc As Object
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim blnEXCEL As Boolean, blnHeaderRow As Boolean

blnEXCEL = False

' Replace True with False if you do not want the first row of
' the worksheet to be a header row (the names of the fields
' from the recordset)
blnHeaderRow = True

' Establish an EXCEL application object
On Error Resume Next
Set xlx = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
      Set xlx = CreateObject("Excel.Application")
      blnEXCEL = True
End If
Err.Clear
On Error GoTo 0

' Change True to False if you do not want the workbook to be
' visible when the code is running
xlx.Visible = True

' Replace C:\Filename.xls with the actual path and filename
' of the EXCEL file into which you will write the data
Set xlw = xlx.Workbooks.Open("C:\Filename.xls")

' Replace WorksheetName with the actual name of the worksheet
' in the EXCEL file
' (note that the worksheet must already be in the EXCEL file)
Set xls = xlw.Worksheets("WorksheetName")

' Replace A1 with the cell reference into which the first data value
' is to be written
Set xlc = xls.Range("A1") ' this is the first cell into which data go

Set dbs = CurrentDb()

' Replace QueryOrTableName with the real name of the table or query
' whose data are to be written into the worksheet
Set rst = dbs.OpenRecordset("QueryOrTableName", dbOpenDynaset, dbReadOnly)

If rst.EOF = False And rst.BOF = False Then

      rst.MoveFirst

      If blnHeaderRow = True Then
            For lngColumn = 0 To rst.Fields.Count - 1
                  xlc.Offset(0, lngColumn).Value = rst.Fields(lngColumn).Name
            Next lngColumn
            Set xlc = xlc.Offset(1,0)
      End If

      ' write data to worksheet
      Do While rst.EOF = False
            For lngColumn = 0 To rst.Fields.Count - 1
                  xlc.Offset(0, lngColumn).Value = rst.Fields(lngColumn).Value
            Next lngColumn
            rst.MoveNext
            Set xlc = xlc.Offset(1,0)
      Loop

End If

rst.Close
Set rst = Nothing

dbs.Close
Set dbs = Nothing

' Close the EXCEL file while saving the file, and clean up the EXCEL objects
Set xlc = Nothing
Set xls = Nothing
xlw.Close True   ' close the EXCEL file and save the new data
Set xlw = Nothing
If blnEXCEL = True Then xlx.Quit
Set xlx = Nothing

答案 1 :(得分:0)

因为 Position 是MS Accces中的reserved word,所以只需使用反引号或方括号来转义外部查询和子查询中的单词。

有趣的是,虽然表别名限定符适用于MSAccess.exe GUI程序中的保留字,但是来自Excel的外部ODBC调用可能会失败,而不会转义这些保留字:

SELECT
...
Employees.[Position], 
...

SELECT
...
Employees.`Position`, 
...

答案 2 :(得分:0)

具有相同的错误-链接Excel和Access。 将双引号更改为单引号后,错误“参数太少。预期1”已解决。正确的代码示例。

AND all_clean.lastapp='Dial'