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: -
'create the recordset
-
Set db = CurrentDb()
-
Set qdf = db.QueryDefs(stDocName)
-
-
'Query Parameters
-
qdf.Parameters("Region") = Me.Territory
-
qdf.Parameters("State") = Me.State
-
qdf.Parameters("Brand") = Me.Brand
-
-
Set rs = qdf.OpenRecordset()
-
If rs.RecordCount = 0 Then Exit Sub
8 4383 @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: - Forms![<Your Form Name>]![<Your Field Name>] 'in 1 or more Criteria Rows
- Dim rs As DAO.Recordset
-
Dim qdf As DAO.QueryDef
-
Dim db As DAO.Database
-
Dim prm As DAO.Parameter
-
Dim stDocName As String
-
-
stDocName = "<Query Name here>"
-
-
Set db = CurrentDb()
-
Set qdf = db.QueryDefs(stDocName)
-
-
'Resolve the Parameters
-
For Each prm In qdf.Parameters
-
prm.Value = Eval(prm.Name)
-
Next
-
-
Set rs = qdf.OpenRecordset(dbOpenDynaset)
-
-
rs.MoveLast: rs.MoveFirst
-
-
If rs.RecordCount = 0 Then Exit Sub
-
-
With rs
-
Do Until .EOF
-
'process Records within Loop
-
.MoveNext
-
Loop
-
End With
-
-
rs.Close
-
Set rs = Nothing
[quote=ADezii;3479511] -
'Resolve the Parameters
-
For Each prm In qdf.Parameters
-
prm.Value = Eval(prm.Name)
-
Next
-
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?
I am still getting the error. Maybe it is from another part?
It is ugly. I am sorry.
This is the query "output." - 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]
-
FROM [zz Profit and Loss Flow Final]
-
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 & "*"))
-
GROUP BY [zz Profit and Loss Flow Final].[Year-Month];
-
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. - Option Compare Database
-
Dim db As DAO.Database
-
Dim qdf As DAO.QueryDef
-
Dim rs As DAO.Recordset
-
Dim prm As DAO.Parameter
-
Private Sub Run_Click()
-
On Error GoTo Err_Run_Click
-
Dim stTerritory As String
-
Dim stState As String
-
Dim stBrand As String
-
Dim dFirstMonth As Date
-
Dim stDocName As String
-
Dim Suc As Boolean
-
stDocName = "output"
-
dFirstMonth = Me.firstmonth
-
-
If Me.Territory <> "" Then
-
stTerritory = Me.Territory
-
Else
-
stTerritory = "All"
-
End If
-
If Me.State <> "" Then
-
stState = Me.State
-
Else
-
stState = "All"
-
End If
-
If Me.Brand <> "" Then
-
stBrand = Me.Brand
-
Else
-
stBrand = "All"
-
End If
-
-
'create the recordset
-
Set db = CurrentDb()
-
Set qdf = db.QueryDefs(stDocName)
-
'Query Parameters
-
qdf.Parameters("Region") = Me.Territory
-
qdf.Parameters("State") = Me.State
-
qdf.Parameters("Brand") = Me.Brand
-
-
'Resolve the Parameters
-
For Each prm In qdf.Parameters
-
prm.Value = Eval(prm.Name)
-
Next
-
-
Set rs = qdf.OpenRecordset(dbDynaset)
-
rs.MoveLast: rs.MoveFirst
-
-
If rs.RecordCount = 0 Then Exit Sub
-
-
'DoCmd.OpenQuery stDocName, acNormal, acReadOnly
-
Exit_Run_Click:
-
Suc = PrintSheet(stTerritory, stState, stBrand, dFirstMonth)
-
-
Exit Sub
-
Err_Run_Click:
-
MsgBox Err.Description
-
Resume Exit_Run_Click
-
End Sub
-
-
Public Function PrintSheet(Territory As String, State As String, Brand As String, firstmonth As Date)
-
-
Dim xlApp As Excel.Application
-
Dim xlWB1 As Excel.Workbook
-
Dim currentmonth As Date
-
Dim x As Integer
-
Dim y As Integer
-
-
Set xlApp = New Excel.Application
-
xlApp.Visible = True
-
Set xlWB1 = xlApp.Workbooks.Open("Y:\Documents\Freelance\Cape Classics\v3\Profit loss Flow.xls")
-
Set xlSheet = xlWB1.Worksheets("Sheet1")
-
-
'print the limit titles to the data
-
xlSheet.Cells(1, 2).Value = Territory
-
xlSheet.Cells(2, 2).Value = Brand
-
xlSheet.Cells(3, 2).Value = State
-
xlSheet.Cells(1, 10).Value = firstmonth
-
xlSheet.Cells(2, 10).Value = DateAdd("m", 12, firstmonth) '!!!!!!Return last month from query
-
-
'print Query Data
-
currentmonth = firstmonth
-
y = 5
-
x = 3
-
If rs.Fields("Year-Month").Value = currentmonth Then
-
xlSheet.Cells(y, x).Value = currentmonth
-
x = x + 1
-
End If
-
'Do While Not .EOF Or x <> 15
-
'process Records within Loop
-
-
' .MoveNext
-
'Loop
-
-
'Save
-
xlApp.ActiveWorkbook.SaveAs ("Y:\Documents\Freelance\Cape Classics\v3\Profit loss Flow_new.xls")
-
'Clean Up
-
xlWB1.Close
-
Set xlWB1 = Nothing
-
xlApp.Quit
-
Set xlApp = Nothing
-
-
PrintSheet = True
-
-
rs.Close
-
Set rs = Nothing
-
End Function
-
[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: - For Each prm In qdf.Parameters
-
prm.Value = Eval(Forms!sort.territory) 'Parameter 1
-
prm.Value = Eval(Forms!sort.state) 'Parameter 2
-
prm.Value = Eval(Forms!sort.brand) 'Parameter 3
-
Next
I commented out - 'Query Parameters
-
qdf.Parameters("Region") = Me.Territory
-
qdf.Parameters("State") = Me.State
-
qdf.Parameters("Brand") = Me.Brand
-
and am now getting a type mismatch. Any ideas why?
@kirschey - Your Parameters are all wrong, for instance, the State Parameter would be:
- Run the following code within the Context of the Form to see your Parameter Names, and their corresponding Values:
- Dim qdf As DAO.QueryDef
-
Dim db As DAO.Database
-
Dim prm As DAO.Parameter
-
Dim stDocName As String
-
-
stDocName = "<Query Name Here>"
-
-
Set db = CurrentDb()
-
Set qdf = db.QueryDefs(stDocName)
-
-
'Resolve the Parameters
-
For Each prm In qdf.Parameters
-
prm.Value = Eval(prm.Name)
-
Msgbox prm.Name & " ==> " & prm.Value
-
Next
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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") '...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: 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,...
|
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...
|
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...
| |