473,385 Members | 1,821 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,385 software developers and data experts.

how do you open a recordset with underlying parameters?

119 100+
Hi There,

I am trying to execute a query using openRecordset on a query called 'AccountValues'. The 'AccountValues' query relies on another query, 'Prices', which in turn relies on a form for two parameters.

The information flow is:

2 parameters -> Prices -> AccountBalances -> {my query}

Ideally, I would be able to perform the following:

Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT Account, SubAccount, Sector, Symbol, Sum(MarketValueStart) AS SumOfMarketValueStart, Sum(MarketValueEnd) AS SumOfMarketValueEnd FROM AccountValues GROUP BY Account, SubAccount, Sector, Symbol;"
  2. Set CustData = qdf.OpenRecordset(strSQL)
However, when I run this I get the following error message:

"Too few parameters. Expected 2."

I understand that this is because the 'Prices' query requires these parameters. Now, I have been able to open 'AccountValues' with the correct parameters using the following code.

Expand|Select|Wrap|Line Numbers
  1.     Dim CustDB As dataBase, CustData, CustTotals As Recordset
  2.     Dim qdf As QueryDef, prm As Parameter
  3.  
  4.     Set CustDB = DBEngine.Workspaces(0).Databases(0)
  5.     Set qdf = CustDB.QueryDefs("AccountValues")
  6.  
  7.     For Each prm In qdf.Parameters
  8.         prm.value = Eval(prm.Name)
  9.     Next prm
  10.  
  11.     Set CustData = qdf.OpenRecordset(dbOpenDynaset)
However, this is only opens the 'AccountValues' query with the correct parameters, not the QUERY ON AccountValues.

Does anyone know how I might be able to go one step further and execute a query on the AccountValues query?

Thanks in advance for your help.
Jun 7 '07 #1
5 5710
ADezii
8,834 Expert 8TB
Hi There,

I am trying to execute a query using openRecordset on a query called 'AccountValues'. The 'AccountValues' query relies on another query, 'Prices', which in turn relies on a form for two parameters.

The information flow is:

2 parameters -> Prices -> AccountBalances -> {my query}

Ideally, I would be able to perform the following:

Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT Account, SubAccount, Sector, Symbol, Sum(MarketValueStart) AS SumOfMarketValueStart, Sum(MarketValueEnd) AS SumOfMarketValueEnd FROM AccountValues GROUP BY Account, SubAccount, Sector, Symbol;"
  2. Set CustData = qdf.OpenRecordset(strSQL)
However, when I run this I get the following error message:

"Too few parameters. Expected 2."

I understand that this is because the 'Prices' query requires these parameters. Now, I have been able to open 'AccountValues' with the correct parameters using the following code.

Expand|Select|Wrap|Line Numbers
  1.     Dim CustDB As dataBase, CustData, CustTotals As Recordset
  2.     Dim qdf As QueryDef, prm As Parameter
  3.  
  4.     Set CustDB = DBEngine.Workspaces(0).Databases(0)
  5.     Set qdf = CustDB.QueryDefs("AccountValues")
  6.  
  7.     For Each prm In qdf.Parameters
  8.         prm.value = Eval(prm.Name)
  9.     Next prm
  10.  
  11.     Set CustData = qdf.OpenRecordset(dbOpenDynaset)
However, this is only opens the 'AccountValues' query with the correct parameters, not the QUERY ON AccountValues.

Does anyone know how I might be able to go one step further and execute a query on the AccountValues query?

Thanks in advance for your help.
How about applying a Filter to your Recordset based on a Parameterized Query?
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database, MyRS As DAO.Recordset, qdf As DAO.QueryDef
  2.  
  3. Set MyDB = CurrentDb()
  4. Set qdf = MyDB.QueryDefs("qryEmployee")
  5.  
  6. 'Evaluate Parameters
  7. For Each prm In qdf.Parameters
  8.   prm.Value = Eval(prm.Name)
  9. Next prm
  10.  
  11. 'Open Recordset on Paramter Query
  12. Set MyRS = qdf.OpenRecordset(dbOpenDynaset)
  13. MyRS.MoveLast: MyRS.MoveFirst
  14.   MsgBox "Recordset Count before Filtering = " & MyRS.RecordCount
  15.  
  16. 'Let's apply a Filter to the Original Recordset
  17. MyRS.Filter = "[Platoon]='A'"
  18.  
  19. 'Create a New 'Filtered' Recordset based on the Original
  20. Dim rstFiltered As DAO.Recordset
  21. Set rstFiltered = MyRS.OpenRecordset()
  22. rstFiltered.MoveLast: rstFiltered.MoveFirst
  23.   MsgBox "Recordset Count after Filtering = " & rstFiltered.RecordCount
  24.  
  25. MyRS.Close
  26. rstFiltered.Close
Just a couple of things to remember when Filtering a Recordset:
  1. The new Filtering doesn't take effect until you create a new Recordset based on the old one.
  2. The Filter property doesn't apply to Table-Type Recordsets.
  3. The new Filtering will never retrieve additional Rows from the original Source Tables. It will Filter only Rows that are in the base Recordset you are filtering.
NOTE: Let me know how you make out.
Jun 8 '07 #2
maxamis4
295 Expert 100+
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT Account, SubAccount, Sector, Symbol, Sum(MarketValueStart) AS SumOfMarketValueStart, Sum(MarketValueEnd) AS SumOfMarketValueEnd FROM AccountValues GROUP BY Account, SubAccount, Sector, Symbol"
  2. Set CustData = qdf.OpenRecordset(strSQL)
you don't need the semicoln
Jun 8 '07 #3
billelev
119 100+
ADezii,

Thanks for your suggestion. It's not so much that I need to filter the initial recordset, but rather group and sum the data (as per the Select statement at the top of the post).

Any ideas how I could achieve that?
Jun 8 '07 #4
maxamis4
295 Expert 100+
My best suggestion is to create your query in Access, under the queries tab. Once you get the query you want working, then look at the query in sql view and copy and paste that into your recordset. This should help you trouble shoot it better. Sorry I can't help you more but I would need to see the results displayed to understand where the syntax is wrong. But like I said create your sql view under quieres in access and when you get it to display how you like copy it to your recordset.


good luck
Jun 9 '07 #5
billelev
119 100+
This is the query I would like to execute:

Expand|Select|Wrap|Line Numbers
  1. SELECT Account, SubAccount, Sector, Symbol, Sum(MarketValueStart) AS SumOfMarketValueStart, Sum(MarketValueEnd) AS SumOfMarketValueEnd
  2. FROM AccountValues
  3. GROUP BY Account, SubAccount, Sector, Symbol;
However, AccountValues is a query that takes inputs from a form. I cannot, therefore, open a recordSet with the above query without being asked to enter parameters.

I can, however, open a recordSet of 'AccountValues' using CustDB.QueryDefs("AccountValues"), but this will not allow me to GROUP the data, which is the whole point of me trying to do this.

It is not a question of how to write an SQL select statement. I know exactly which statement to use. I just don't know how to use the above statement AND feed in the correct parameters using VBA.
Jun 11 '07 #6

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

Similar topics

2
by: Robert Mark Bram | last post by:
Hi All! My ASP page below receives the following error: Error Type: Microsoft OLE DB Provider for ODBC Drivers (0x80040E10) Too few parameters. Expected 2. /polyprint/test.asp, line 31 ...
4
by: Nuno | last post by:
Is there any SQL Error? Or I have to use Select case in VB code to control SQL instead. Thank you for any ans. Nuno
1
by: John | last post by:
(acc2002) I have a command button (cmdView) on a form (frmOverview)that needs to open a form (frmFilmProjectAgreements) and its two subforms (control names: fsubAgreements and fsubPayments)...
4
by: Max Harvey | last post by:
Hi, I have looked at the example called "Open Parameter queries from code" from the site http://www.mvps.org/access/queries/qry0003.htm I made up a test which I though looked pretty close...
5
by: Ryan Hubbard | last post by:
Is it possible to get the recordset from an open query window? So you run the query. The window is open. Can vba retrieve this data?
8
by: lauren quantrell | last post by:
When I open an Access form I can have no recordset specified, then in the form's OnOpen event I can do something like: Me.paramaters = "@SomeColumn = 22)" Me.recordsource = "dbo.sproc123" But I...
2
by: allyn44 | last post by:
Hello, I have built a serch form for users to edit records. I only want them to pull up the record they need, and I want to check for nulls. There should not be dupes becasue the underlying...
20
by: fniles | last post by:
I am using VB.NET 2003, SQL 2000, and SqlDataReader. As I read data from tblA, I want to populate tblB. I use SQLDataReader for both tables. I do not use thread. When I ExecuteReader on tblB, I...
2
by: wallconor | last post by:
Hi, I am having a problem using Dreamweaver CS3 standard recordset paging behavior. It doesn’t seem to work when I pass parameter values from a FORM on my search page, to the recordset on my...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.