Access vba code not exporting to EXCEL “Too few parameters. Expected 1.”

荒凉一梦 提交于 2020-04-18 05:43:10

问题


I have some code I previous got from one of my other Access Databases, which works fine. It intended purpose was to copy the results that were put into a make table into a blank excel sheet then format the columns.

I however copied the code and tweaked it a little. What I am trying to do with the copied code is still export my results into a blank excel sheet, however, this time I am not coping from a make table but from a select query. I am not sure if it is possible but there is no other way for me to make this into a make table, it has to be an select query. Is there any way someone can help? The code I have now is:

Option Compare Database
Public Function Export_EXCEL()


Dim dbs As DAO.Database
Dim Response As Integer
Dim strSQL As String
Dim Query1 As String
Dim LTotal As String
  Dim Excel_App As Excel.Application 'Creates Blank Excel File
  Dim strTable As String ' Table in access
    Dim queryDelete As String 'SQL to delete records in local table
    Dim strAssigned As DAO.Recordset

'-------------------------------------------------------------------------------
strTable = "Select * From cso_sup_SETUP" 'Access Query I am trying to copy

    Set Excel_App = CreateObject("Excel.Application")
        Set dbs = CurrentDb
'-------------------------------------------------------------------------------
Dim rs As DAO.Recordset
Set rs = dbs.OpenRecordset(strTable)

    Excel_App.Visible = True

Dim wkb As Excel.Workbook
  Set wkb = Excel_App.Workbooks.Add

Dim rg As Excel.Range
Dim i As Long
' Add the headings
For i = 0 To rs.Fields.Count - 1
    wkb.Sheets(1).Cells(1, i + 1).Value = rs.Fields(i).Name
Next i

Set rg = wkb.Sheets(1).Cells(2, 1)
rg.CopyFromRecordset rs

' make pretty
rg.CurrentRegion.EntireColumn.AutoFit


Set rs = Nothing
Set wkb = Nothing
Set dbs = Nothing

End Function

This is the modified code below:

Option Compare Database
Public Function Export_EXCEL()


Dim dbs As DAO.Database
Dim Response As Integer
Dim strSQL As String
Dim Query1 As String
Dim LTotal As String
  Dim Excel_App As Excel.Application 'Creates Blank Excel File
  Dim strTable As String ' Table in access
   Dim queryDelete As String 'SQL to delete records in local table
   Dim strAssigned As DAO.Recordset

 Dim rs As DAO.Recordset
 Dim db As DAO.Database
 Dim prm As DAO.Parameter
 Dim qdf As DAO.QueryDef

 '-------------------------------------------------------------------------------
 strTable = "Select * From cso_sup_SETUP" 'Access Query I am trying to copy

Set Excel_App = New Excel.Application
    Set dbs = CurrentDb
 '-------------------------------------------------------------------------------

Set rs = QuerDef.OpenRecordset(strTable)

Excel_App.Visible = True

Dim wkb As Excel.Workbook
  Set wkb = Excel_App.Workbooks.Add

Dim rg As Excel.Range
Dim i As Long
 ' Add the headings
For i = 0 To rs.Fields.Count - 1
   wkb.Sheets(1).Cells(1, i + 1).Value = rs.Fields(i).Name
 Next i

Set rg = wkb.Sheets(1).Cells(2, 1)
rg.CopyFromRecordset rs

' make pretty
rg.CurrentRegion.EntireColumn.AutoFit


 Set rs = Nothing
 Set wkb = Nothing
 Set dbs = Nothing

 End Function

回答1:


This error has one of two causes:

Cause 1 - You have specified a field name in query cso_sup_SETUP that does not exist in the underlying table. In this case, Access treats the non-existent field as a "parameter", and throws a Too few parameters error if you try to open it in code.

Cause 2 - You have defined a Parameter in query cso_sup_SETUP, but have not supplied a value in code.

Fixes:

Fix 1 - Remove the non-existent field, or correct the spelling (look for Expr1)

Fix 2 - Supply a parameter value in code per HansUp's suggestion.




回答2:


You have an Access QueryDef (a saved query) named "cso_sup_SETUP". That query references a control on a form ... something like Forms!MyForm!MyTextbox. When you open that query in the Access query designer, it can retrieve the text box value the query needs. However when you attempt to use the query with the DAO OpenRecordset method, it is unable to retrieve the text box value and treats Forms!MyForm!MyTextbox as a parameter for which you have not supplied a value.

You can load your Recordset by setting an object variable to the QueryDef and then supplying the form control's value as a Parameter before calling the QueryDef.OpenRecordset method.

Note your problem does not actually have anything to do with Excel --- it's a purely Access problem. So here is Access VBA code which should give you the Recordset I think you need. Suggest you create a new procedure in your code module to test this. Once you have it working, either add the Excel bits from the original procedure or adapt the original to use this.

Also strongly suggest you add Option Explicit to your module's Declarations section. Put it on a new line immediately below Option Compare Database

Dim rs As DAO.Recordset
Dim db As DAO.Database
Dim prm As DAO.Parameter
Dim qdf As DAO.QueryDef

Set db = CurrentDb
Set qdf = db.QueryDefs("cso_sup_SETUP")
For Each prm In qdf.Parameters
    prm.value = Eval(prm.Name)
Next
Set rs = qdf.OpenRecordset(dbOpenDynaset)


来源:https://stackoverflow.com/questions/60764756/access-vba-code-not-exporting-to-excel-too-few-parameters-expected-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!