473,402 Members | 2,046 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,402 software developers and data experts.

Create RecordSet from Form Parameters

I am trying to use a form data to modify a query and store it as a recordset.

I have it working properly just opening the query, but I cannot seem to get the QueryDef parameters working the way I would like. (The Me.Territory,Me.State, and Me.Brand is the form data).

I get the error "Item not found in this collection." Currently.

The code that doesn't work:
Expand|Select|Wrap|Line Numbers
  1.     'create the recordset
  2.     Set db = CurrentDb()
  3.     Set qdf = db.QueryDefs(stDocName)
  4.  
  5.     'Query Parameters
  6.     qdf.Parameters("Region") = Me.Territory
  7.     qdf.Parameters("State") = Me.State
  8.     qdf.Parameters("Brand") = Me.Brand
  9.  
  10.     Set rs = qdf.OpenRecordset()
  11.     If rs.RecordCount = 0 Then Exit Sub
Apr 8 '09 #1
8 4383
ADezii
8,834 Expert 8TB
@kirschey
I created a Generic Routine which will resolve your Query Parameters based on the appropriate Form Values. This logic will work on any Query that has referenced Form Values as criteria, namely:
Expand|Select|Wrap|Line Numbers
  1. Forms![<Your Form Name>]![<Your Field Name>]    'in 1 or more Criteria Rows
Expand|Select|Wrap|Line Numbers
  1. Dim rs As DAO.Recordset
  2. Dim qdf As DAO.QueryDef
  3. Dim db As DAO.Database
  4. Dim prm As DAO.Parameter
  5. Dim stDocName As String
  6.  
  7. stDocName = "<Query Name here>"
  8.  
  9. Set db = CurrentDb()
  10. Set qdf = db.QueryDefs(stDocName)
  11.  
  12. 'Resolve the Parameters
  13. For Each prm In qdf.Parameters
  14.   prm.Value = Eval(prm.Name)
  15. Next
  16.  
  17. Set rs = qdf.OpenRecordset(dbOpenDynaset)
  18.  
  19. rs.MoveLast: rs.MoveFirst
  20.  
  21. If rs.RecordCount = 0 Then Exit Sub
  22.  
  23. With rs
  24.   Do Until .EOF
  25.     'process Records within Loop
  26.     .MoveNext
  27.   Loop
  28. End With
  29.  
  30. rs.Close
  31. Set rs = Nothing
Apr 8 '09 #2
[quote=ADezii;3479511]
Expand|Select|Wrap|Line Numbers
  1. 'Resolve the Parameters
  2. For Each prm In qdf.Parameters
  3.   prm.Value = Eval(prm.Name)
  4. Next
  5.  
Thanks for the help. I still don't get how I would adapt my form inputs into the For loop. Would I use an array?
Apr 9 '09 #3
I am still getting the error. Maybe it is from another part?
Apr 9 '09 #4
ADezii
8,834 Expert 8TB
@kirschey
Post the SQL for the Query.
Apr 9 '09 #5
It is ugly. I am sorry.


This is the query "output."
Expand|Select|Wrap|Line Numbers
  1. SELECT [zz Profit and Loss Flow Final].[Year-Month], Sum([zz Profit and Loss Flow Final].[Cases Shipped]) AS [Cases Shipped], Sum([zz Profit and Loss Flow Final].[Cases Depl]) AS [Cases Depl], Sum([zz Profit and Loss Flow Final].[Cases Depl Budget]) AS [Cases Depl Budget], Sum([zz Profit and Loss Flow Final].[Gross Profit]) AS [Gross Profit], Sum([zz Profit and Loss Flow Final].SPAs) AS SPAs, Sum([zz Profit and Loss Flow Final].Samples) AS Samples, Sum([zz Profit and Loss Flow Final].[Other Selling Exp]) AS OtherSellingExp, Sum([zz Profit and Loss Flow Final].Salaries) AS Salaries, Sum([zz Profit and Loss Flow Final].[Travel & Enter]) AS [Travel & Enter], Sum([zz Profit and Loss Flow Final].[Primary Budget]) AS [OfPrimary Budget], Sum([zz Profit and Loss Flow Final].[T&E Bud]) AS [T&E Bud], Sum([zz Profit and Loss Flow Final].[Add'l Budget]) AS [Add'l Budget], Sum([zz Profit and Loss Flow Final].[Sal Budget]) AS [Sal Budget]
  2. FROM [zz Profit and Loss Flow Final]
  3. WHERE ((([zz Profit and Loss Flow Final].Region) Like "*" & Forms!sort.territory & "*") And (([zz Profit and Loss Flow Final].State) Like "*" & Forms!sort.state & "*") And (([zz Profit and Loss Flow Final].Brand) Like "*" & Forms!sort.brand & "*"))
  4. GROUP BY [zz Profit and Loss Flow Final].[Year-Month];
  5.  
Here is the vb code I am working on to dump the query into a specially formatted excel spreadsheet one cell at a time. It is the record set that is giving me some trouble at the moment because I am not doing something properly in the parameters section.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Dim db As DAO.Database
  3. Dim qdf As DAO.QueryDef
  4. Dim rs As DAO.Recordset
  5. Dim prm As DAO.Parameter
  6. Private Sub Run_Click()
  7. On Error GoTo Err_Run_Click
  8.     Dim stTerritory As String
  9.     Dim stState As String
  10.     Dim stBrand As String
  11.     Dim dFirstMonth As Date
  12.     Dim stDocName As String
  13.     Dim Suc As Boolean
  14.     stDocName = "output"
  15.     dFirstMonth = Me.firstmonth
  16.  
  17.     If Me.Territory <> "" Then
  18.         stTerritory = Me.Territory
  19.     Else
  20.         stTerritory = "All"
  21.     End If
  22.     If Me.State <> "" Then
  23.         stState = Me.State
  24.     Else
  25.         stState = "All"
  26.     End If
  27.     If Me.Brand <> "" Then
  28.         stBrand = Me.Brand
  29.     Else
  30.         stBrand = "All"
  31.     End If
  32.  
  33.     'create the recordset
  34.     Set db = CurrentDb()
  35.     Set qdf = db.QueryDefs(stDocName)
  36.     'Query Parameters
  37.     qdf.Parameters("Region") = Me.Territory
  38.     qdf.Parameters("State") = Me.State
  39.     qdf.Parameters("Brand") = Me.Brand
  40.  
  41.     'Resolve the Parameters
  42.     For Each prm In qdf.Parameters
  43.        prm.Value = Eval(prm.Name)
  44.     Next
  45.  
  46.     Set rs = qdf.OpenRecordset(dbDynaset)
  47.     rs.MoveLast: rs.MoveFirst
  48.  
  49.     If rs.RecordCount = 0 Then Exit Sub
  50.  
  51.     'DoCmd.OpenQuery stDocName, acNormal, acReadOnly
  52. Exit_Run_Click:
  53.     Suc = PrintSheet(stTerritory, stState, stBrand, dFirstMonth)
  54.  
  55.     Exit Sub
  56. Err_Run_Click:
  57.     MsgBox Err.Description
  58.     Resume Exit_Run_Click
  59. End Sub
  60.  
  61. Public Function PrintSheet(Territory As String, State As String, Brand As String, firstmonth As Date)
  62.  
  63.     Dim xlApp As Excel.Application
  64.     Dim xlWB1 As Excel.Workbook
  65.     Dim currentmonth As Date
  66.     Dim x As Integer
  67.     Dim y As Integer
  68.  
  69.     Set xlApp = New Excel.Application
  70.     xlApp.Visible = True
  71.     Set xlWB1 = xlApp.Workbooks.Open("Y:\Documents\Freelance\Cape Classics\v3\Profit loss Flow.xls")
  72.     Set xlSheet = xlWB1.Worksheets("Sheet1")
  73.  
  74.     'print the limit titles to the data
  75.     xlSheet.Cells(1, 2).Value = Territory
  76.     xlSheet.Cells(2, 2).Value = Brand
  77.     xlSheet.Cells(3, 2).Value = State
  78.     xlSheet.Cells(1, 10).Value = firstmonth
  79.     xlSheet.Cells(2, 10).Value = DateAdd("m", 12, firstmonth)  '!!!!!!Return last month from query
  80.  
  81.     'print Query Data
  82.     currentmonth = firstmonth
  83.     y = 5
  84.     x = 3
  85.     If rs.Fields("Year-Month").Value = currentmonth Then
  86.             xlSheet.Cells(y, x).Value = currentmonth
  87.             x = x + 1
  88.     End If
  89.     'Do While Not .EOF Or x <> 15
  90.          'process Records within Loop
  91.  
  92.      '    .MoveNext
  93.     'Loop
  94.  
  95.     'Save
  96.     xlApp.ActiveWorkbook.SaveAs ("Y:\Documents\Freelance\Cape Classics\v3\Profit loss Flow_new.xls")
  97.     'Clean Up
  98.     xlWB1.Close
  99.     Set xlWB1 = Nothing
  100.     xlApp.Quit
  101.     Set xlApp = Nothing
  102.  
  103.     PrintSheet = True
  104.  
  105.     rs.Close
  106.     Set rs = Nothing
  107. End Function
  108.  
Apr 9 '09 #6
ADezii
8,834 Expert 8TB
[quote=kirschey;3479523] @ADezii
I double checked the code using Wildcards as in your case, and it still functions normally.
I still don't get how I would adapt my form inputs into the For loop.
You need not adapt anything, since the Loop evaluates to:
Expand|Select|Wrap|Line Numbers
  1. For Each prm In qdf.Parameters
  2.   prm.Value = Eval(Forms!sort.territory)              'Parameter 1
  3.   prm.Value = Eval(Forms!sort.state)                  'Parameter 2
  4.   prm.Value = Eval(Forms!sort.brand)                 'Parameter 3
  5. Next
Apr 9 '09 #7
I commented out
Expand|Select|Wrap|Line Numbers
  1.      'Query Parameters
  2.      qdf.Parameters("Region") = Me.Territory
  3.      qdf.Parameters("State") = Me.State
  4.      qdf.Parameters("Brand") = Me.Brand
  5.  
and am now getting a type mismatch. Any ideas why?
Apr 10 '09 #8
ADezii
8,834 Expert 8TB
@kirschey
  1. Your Parameters are all wrong, for instance, the State Parameter would be:
    Expand|Select|Wrap|Line Numbers
    1. Forms!sort.state
  2. Run the following code within the Context of the Form to see your Parameter Names, and their corresponding Values:
    Expand|Select|Wrap|Line Numbers
    1. Dim qdf As DAO.QueryDef
    2. Dim db As DAO.Database
    3. Dim prm As DAO.Parameter
    4. Dim stDocName As String
    5.  
    6. stDocName = "<Query Name Here>"
    7.  
    8. Set db = CurrentDb()
    9. Set qdf = db.QueryDefs(stDocName)
    10.  
    11. 'Resolve the Parameters
    12. For Each prm In qdf.Parameters
    13.   prm.Value = Eval(prm.Name)
    14.   Msgbox prm.Name & " ==> " & prm.Value
    15. Next
Apr 11 '09 #9

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

Similar topics

2
by: Reggie | last post by:
Hi and TIA! I have a query that uses 2 values from my form as criteria for 2 fields in the query. I can select the options and open the query. I can base a form on the query and launch it from...
14
by: deko | last post by:
For some reason this does not seem to be working... Am I missing something basic? Dim rst As DAO.Recordset Dim db As DAO.Database Set db = CurrentDb Set rst = db.OpenRecordset("qryEmailS") '...
7
by: Peter Bailey | last post by:
I have a querystring built up in vba and I want to open a recordset based on the sql and pass the date value to a textbox or label for use elsewhere on the form. The recordset isnt working as it...
2
by: Corey | last post by:
I am missing something here. I have a pop up form (loads from the "main form")that displays multiple command buttons. When a user selects a particular button, the recordset from the main form...
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...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
0
by: beebelbrox | last post by:
Hi, I am new VB programming in Access and I am requesting help with the following code. This code is attached to a form that will display a specific recordset based in information passed to the...
3
by: Kassimu | last post by:
Hi there, I have a table with thousands of record entries, usually the user searches this table through SearchForm resulting into some recordset. What I need to do on this recordset is to...
12
by: =?Utf-8?B?R1ROMTcwNzc3?= | last post by:
Hi Guys, me again!!!! I've got a little recordset problem, basically I'm trying to build a jobs by email feature, so I have the following - Jobs by Email table which includes - category...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.