Run-time Error '3061' Too Few Parameters. Expected 4. (1 Viewer)

ccondran08

Registered User.
Local time
Tomorrow, 06:44
Joined
Feb 27, 2014
Messages
58
Hi, looking for some help here please. I have a form that has a textbox in what I've called the "settings" tab of a control tab. The user can click on a button and edit the text box and type in an Activity Code that is then saved and used in a query to retrieve records that the user has selected (eg. [Forms]![frm_Main_Menu]![txtActivity1]).

The code below is used to export the query and paste it into a spreadsheet that is located in the directory of "TRACpath". During the execution of the code I get an Run-time error "3061" which appears and then the VBA editor appears and is highlighted on the following line ;

Set rs_Milestones = qdf_Milestones.OpenRecordset()

Can anyone please shed some light on this so I can get this finished...:banghead:







Dim XL As Excel.Application
Dim xlWB As Workbook
Dim wbTarget As Workbook
Dim TRACpath As String


TRACpath = [Forms]![frm_Main_Menu].[txtTRAC_Path]



Dim qdf_Milestones As QueryDef
Dim rs_Milestones As Recordset

Set qdf_Milestones = CurrentDb.QueryDefs("qry_P3_Milestone_Gaps")
Set rs_Milestones = qdf_Milestones.OpenRecordset()

Set XL = CreateObject("Excel.Application")

Set wbTarget = XL.Workbooks.Open(TRACpath)


wbTarget.Worksheets("Data").Cells(2, 186).CopyFromRecordset rs_Milestones

wbTarget.Save
wbTarget.Close

Set wbTarget = Nothing
Set XL = Nothing
Set qdf_Milestones = Nothing
 

Minty

AWF VIP
Local time
Today, 23:44
Joined
Jul 26, 2013
Messages
10,368
Is your query a crosstab query by any chance? If it is you have to define the parameters in the query design.
 

ccondran08

Registered User.
Local time
Tomorrow, 06:44
Joined
Feb 27, 2014
Messages
58
No, this isn't a cross tab query.
 

Minty

AWF VIP
Local time
Today, 23:44
Joined
Jul 26, 2013
Messages
10,368
Could you post up the query - without it we are guessing.
 

ccondran08

Registered User.
Local time
Tomorrow, 06:44
Joined
Feb 27, 2014
Messages
58
This is a very simple query, it works fine when I open the query. The problem is when the code tries to grab the query and send it to the spreadsheet I am missing something that tells the query that there is a textbox on the form that is used to filter the query. I believe the answer is in the below link but for some reason I am missing something.


https://support.microsoft.com/en-us/kb/209203


SELECT tbl_P6_Data.[Activity Name], tbl_P6_Data.Finish, tbl_P6_Data.[Activity ID]
FROM tbl_P6_Data
WHERE ((tbl_P6_Data.[Activity ID])=Forms!frm_Main_Menu!txtActivity1);
 

ccondran08

Registered User.
Local time
Tomorrow, 06:44
Joined
Feb 27, 2014
Messages
58
Thanks sneuberg, the link you provided had my answer and you've saved my day. The full code is ;

Dim XL As Excel.Application
Dim xlApp As Excel.Application
Dim xlWB As Workbook
Dim wbTarget As Workbook
Dim TRACpath As String

Dim qdf_Milestones
Dim rs_Milestones


TRACpath = [Forms]![frm_Main_Menu].[txtTRAC_Path]


'Punch ETC Transfer
Set qdf_Milestones = CurrentDb.QueryDefs("qry_P3_Milestone_Gaps")

qdf_Milestones("Forms!frm_Main_Menu!txtActivity1") = Forms!frm_Main_Menu!txtActivity1
qdf_Milestones("Forms!frm_Main_Menu!txtActivity2") = Forms!frm_Main_Menu!txtActivity2
qdf_Milestones("Forms!frm_Main_Menu!txtActivity3") = Forms!frm_Main_Menu!txtActivity3
qdf_Milestones("Forms!frm_Main_Menu!txtActivity4") = Forms!frm_Main_Menu!txtActivity4


Set rs_Milestones = qdf_Milestones.OpenRecordset()

Set XL = CreateObject("Excel.Application")

Set wbTarget = XL.Workbooks.Open(TRACpath)


wbTarget.Worksheets("Data").Cells(2, 186).CopyFromRecordset rs_Milestones

wbTarget.Save
wbTarget.Close

Set wbTarget = Nothing
Set XL = Nothing
Set qdf_Milestones = Nothing
 

Users who are viewing this thread

Top Bottom