473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Error While Creating a Recordset from a QueryDef with Parameters

43
I have successfully used this type of code to create permanent queries that are much faster to access from vba code. (It seems to be much faster than using normal DAO RecordSet code with queries or tables).

It also lets you use parameters.

I first define a query in the normal query design mode and save it. Then I add PARAMETERS to the top of the SQL.

I can then access it as a QueryDef object from the code and create a recordset from it.

I am using Access 2003

My problem is that when I perform a change in both the code and in the SQL PARAMETERS section to remove a parameter, it gives the error

"Too Few Parameters. Expected 3" for example.


SQL for the query:

Expand|Select|Wrap|Line Numbers
  1. PARAMETERS lngTeamID Long, lngShiftID Long, strOther Text ( 255 );
  2. SELECT DISTINCTROW Log.ID, Log.ID2, Log.[Other]
  3.  FROM Log
  4.  
  5. WHERE Log.ID=[lngID]) AND Log.ID2=[lngID2] AND Log.[Other]=[strOther]
  6.  
  7.  
I created a permanent query that appears in the list of queries

Code in the module utilizing the QueryDef:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. Sub Test1 (ID as Strong, ID2 As String)
  4.  
  5. Dim qdf As QueryDef, prmID As Parameter, prmID2 As Parameter, prmOther As Parameter
  6.  
  7. ' Use pre-defined (compiled) QueryDefs for better performance than OpenRecordSet(strSQL)
  8. Dim rst As Recordset, strOther_text As String
  9.     Set qdf = CurrentDb.QueryDefs("qry_def_Log")
  10.  
  11.     Set prmID = qdf.Parameters!lngID
  12.     Set prmID2 = qdf.Parameters!lngID2
  13.     Set prmOTHER = qdf.Parameters("strOTHER")     
  14.     prmID = ID ' From function parameters
  15.     prmID2 = ID2
  16.     prmOTHER = "1"
  17.     Set rst = qdf.OpenRecordset(dbOpenSnapshot)
  18.  
  19.     If rst.RecordCount > 0 Then
  20.         rst.MoveFirst 
  21.  
  22.     Other statements…
  23.  
  24.     End If
  25.  
  26. Exit Sub
  27.  
It runs when I first define it and then when I go back, for example and remove parameter and code for strOther, prmOTHER it gives me the error I mentioned.

Seems like the only way I can get rid of it is to reconstruct the query again from scratch, ending up with the same code that gave me the error.

Is it compiling it interally and needs to be removed somehow and then re-defined?
Oct 31 '12 #1
9 2619
TheSmileyCoder
2,322 Expert Mod 2GB
You don't really show how you remove it. Depending on the method I believe it might be necessary with a refresh of sorts. This is not based on own experience, merely on something I recall having read.
Nov 1 '12 #2
zmbd
5,501 Expert Mod 4TB
It runs when I first define it and then when I go back, for example and remove parameter and code for strOther, prmOTHER it gives me the error I mentioned.
First... I don't quite follow you here, I think I understand; however, just to be sure... could you give an example of what are you doing?

Second... (and this may change depending on your answer to the first question): Have you closed the record set that you opened on line 17 and released the call before trying to change the parameters.
Expand|Select|Wrap|Line Numbers
  1. qdf.close
  2. Set rst = Nothing
Once you set the parameters with the call, for a given recordset (ie need two different recordsets using the same query then use two different variable names), the query will stay that way until closed and the recordset released.

Third... what is happening within the "other code...
Nov 1 '12 #3
Sedrick
43
Sorry for not being more clear on this.

The change I am making is manual, not programatic.

I am going into the query under the Access Queries menu and doing "Design" for the existing permanent query listed there.

I right click in the tables area at the top and choose "SQL". Then I go in and take out strOther as a parameter there and also remove it from the WHERE clause. So the code behind the query then becomes:

Expand|Select|Wrap|Line Numbers
  1. PARAMETERS lngTeamID Long, lngShiftID Long;
  2.  
  3. SELECT DISTINCTROW Log.ID, Log.ID2, Log.[Other] 
  4.  FROM Log 
  5.  
  6. WHERE Log.ID=[lngID]) AND Log.ID2=[lngID2] 
  7.  
I have a module called Module3. The manual change there in the sub called "Test1" would be:

Expand|Select|Wrap|Line Numbers
  1.  
  2.     Set prmID = qdf.Parameters!lngID 
  3.     Set prmID2 = qdf.Parameters!lngID2 
  4.  
  5.     ' Removed line with prmOTHER from here
  6.  
  7.     prmID = ID ' From function parameters 
  8.     prmID2 = ID2 
  9.  
  10.     ' Removed line with prmOTHER from here
  11.  
  12.     Set rst = qdf.OpenRecordset(dbOpenSnapshot
  13.  
The error will occur then when executing the "Test1" sub.

"Too Few Parameters. Expected 3"

So it is the same code that worked with three parameters before manually taking out the 3rd parameter.
Nov 1 '12 #4
TheSmileyCoder
2,322 Expert Mod 2GB
Where is your qdf defined? Is it refreshed after your manual change?
Nov 1 '12 #5
Sedrick
43
It is saved manually when I exit design mode.

I just tried something else. I deleted and created it programatically using the QueryDefs collection and the CreateQueryDef method. Still the same error.

Expand|Select|Wrap|Line Numbers
  1. Currentdb.Querydefs.Delete "qry_def_Log"
  2.  
  3. CurrentDb.CreateQueryDef("qry_def_Log", _
  4. "PARAMETERS lngTeamID Long, lngShiftID Long, strOther Text ( 255 ); SELECT DISTINCTROW Log.ID, Log.ID2, Log.[Other]  FROM Log  WHERE Log.ID=[lngID]) AND Log.ID2=[lngID2] AND Log.[Other]=[strOther] "
  5.  
  6.  
Same result :-/
Nov 1 '12 #6
Sedrick
43
Although I have not yet been able identify the cause of this I found a way to work around it.

I simply take out all the parameters from both the Query definition and the code that passes in the parameters. Run it to verify working. Then add back one parameter at a time to both the code and the query's underlying SQL.
Nov 1 '12 #7
zmbd
5,501 Expert Mod 4TB
I'm glad you found a way around things.

However, I was never really clear in what you were doing to begin with....

Why are you trying to change a stored query by hand while a VBA object is running that refers to it?

Once again, I point out... you've not shown where the record set was released in the code. You open a snapshot of the record set and then you go changing the query by hand while the recordset is still open - I'd expect something wierd.
Nov 1 '12 #8
Sedrick
43
Sorry for the misunderstanding. I was not running code and then changing things manually in the middle of the execution. I guess I did not make that clear enough.

I was executing what I had set up manually. Stopped the execution. Made a change to the query and re-executed. The recordset was released at the end of the module when the code closed it and set it to nothing.

I found another possible solution since my last post. It seems like Access did not like one of my parameter names. When I changed the name it worked with much less trouble.

Thanks for looking into this with me. Much appreciated.
Nov 2 '12 #9
TheSmileyCoder
2,322 Expert Mod 2GB
I am glad to hear you got it working, but I also find that this thread is a very good example of too little information being provided from the person asking the question. When it comes to code, its important to see how (And WHERE public/private) objects are dimensioned and set, and even in some cases (Especially for public variables) how they are cleaned up.
Nov 2 '12 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Xavier MT | last post by:
Hi, I have a problem if somebody can help I appreciate it My VS.NET seemed to work fine and all of a sudden I started to get the "error creating control message" In my aspx page I drag a...
8
by: Drew | last post by:
This is a weird error, it seems if we reboot the web server, which is also the SQL server, I get this error, ADODB.Recordset (0x800A0E7A) Provider cannot be found. It may not be properly...
6
by: owen | last post by:
Generally speaking, what does it mean when I see a "button" with red text showing this message instead of the control I've dragged onto the web form in Design View.? (But the page works fine at...
0
by: Ravi Ambros Wallau | last post by:
Hi: I've created a custom control - a grid that uses Infragistics to display some filters, the grid itself, and some buttons. Well, when using this control directly on WebForm, everything works...
4
by: Richard Hollenbeck | last post by:
I thought I was very specific in this SQL request. There is a form open with a selected record (and a corresponding "lngRecipeID" on that form. The table also has a field called "lngRecipeID". ...
1
by: Nathan Sokalski | last post by:
I have created a custom control for ASP.NET using VB.NET. My control inherits from the System.Web.UI.WebControls.CompositeControl class, and is working fine. However, the Visual Studio .NET designer...
1
by: Richard Hollenbeck | last post by:
I wonder what I'm missing? I really feel like a retard because I've been screwing with some code for a very long time. I just must be missing something very simple. In the following example,...
4
by: =?Utf-8?B?Z3JlZw==?= | last post by:
I am trying to create an instance of a 3rd party COM component, and I'm getting an error. I've opened a ticket with this vendor as well, but I want to approach it from the C# .NET side to make...
3
by: phill86 | last post by:
Hi, I am trying to run the following query in a recordset and i get the following error message Runtime error 3061 - Too few parameters. Expected 1 i am using the following code
12
by: hannoudw | last post by:
I tried to figure out what i did wrong, and i couldn't find the mistake could any one help me ?? i'm getting this message :Error 3061, Too few parameters Expected 1. achat_num and fact_num are both...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.