have a search form in Access 2010 that filters projects based on certain criteria and opens them in another form. One of the criteria is an unbound multi-select list box, txtArea. The data is stored in a table, ProjectActivity. There is another table called LookupArea that divides ProjectActivity by area as North, South, or Both (ie. Having parts in north and south area).
I also have a query, qrySelectArea:
SELECT DISTINCT Area FROM LookupArea;
This is the code I'm trying to work with:
Private Sub OpenReport_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
Set db = CurrentDb()
Set qdf = db.QueryDefs("qryLookupArea")
If Me!txtArea.ItemsSelected.Count > 0 Then
For Each varItem In Me!txtArea.ItemsSelected
strCriteria = strCriteria & "ProjectActivity.Area = " & Chr(34) _
& Me!.ItemData(varItem) & Chr(34) & "OR "
Next varItem
strCriteria = Left(strCriteria, Len(strCriteria) - 3)
Else
strCriteria = "ProjectActivity.Area Like '*'"
End If
strSQL = "SELECT * FROM ProjectActivity " & _
"WHERE " & strCriteria & ";"
qdf.SQL = strSQL
DoCmd.OpenQuery "qryLookupArea"
DoCmd.Close acForm, "Search Form"
DoCmd.Open "ReportForm", acNormal
DoCmd.Close acQuery, "qryLookupArea"
End Sub
When I click the OpenReport button, the query runs (I can see it in the background while the report form opens) but the report form shows all the projects regardless of how I filter it. I'm not super big in VBA so I'd really appreciate any suggestions.
The easiest way is when you open the form, you need to pass the selection criteria as follows (just make sure your 'strCriteria' is valid syntax (i.e. "ProjectActivity.Area Like '*'"):
DoCmd.OpenForm "ReportForm", acNormal, , strCriteria
Related
I have tried to create the event procedure, but It returns zero irrespective of my selection.
I have two tables, which are correctly joined, and below is the code that has an issue.
First, "MsgBox Me.Technology" is returning my selection value eg. Python, Java, but "MsgBox rs!ProjEmployeeID" is returning 0 all times. Help me troubleshoot the code. Thank you. I want it to return Project Employee ID like 1, 2, 3
Option Compare Database
Private Sub Technology_AfterUpdate()
MsgBox Me.Technology
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT ProjEmployeeID FROM Project WHERE Technologies = Trim('" & Forms!Form_employee_by_technologies!Technology & "')")
rs.AddNew
MsgBox rs!ProjEmployeeID
Dim strDocName As String
Dim strWhere As String
strDocName = "Technology"
strWhere = "[EmployeeID] =" & rs!ProjEmployeeID
DoCmd.OpenReport strDocName, acViewReport, , strWhere, acWindowNormal
End Sub
I am not a coder nor do it work in IT but I maintain an access database for our marketing reporting. This database has a VBA script in it that cycles through our report and creates separate excel files for each entity. I have been able to copy the exact code and change it out using new table and filter references without any issues.
I tried to do it again today and I am getting an error "Run-time error 3061 Too few parameters expected". I have been looking online and everyone seems to say that the error refers to incorrect field names or a missing quote but i can't for the life of me figure it out. I double checked the table and field names and I have tried numerous combinations of quotes with no luck. Sorry if this is a really stupid question but I am stumped.
Private Sub Command4_Click()
Dim ExportReportName As String
Dim ExportFileName As String
Dim FilterCriteriaString As String
Dim Filter As String
Dim SQLstring As String
Dim rs As DAO.Recordset
ExportReportName = "User Retest-Partners"
SQLstring = "SELECT DISTINCT [2-5 User Retest Partners].[Owner] FROM [2-5 User Retest Partners]"
Set rs = CurrentDb.OpenRecordset(SQLstring)
If Not rs.BOF And Not rs.EOF Then
rs.MoveFirst
While (Not rs.EOF)
Filter = rs.Fields("Owner")
FilterCriteria = "[2-5 User Retest Partners].[Owner] LIKE '*" & Filter & "*'"
ExportFileName = "U:\Marketing\Reporting\User Retest Needed\Database Exports\Manager Reporting\" & Filter & ".xls"
DoCmd.OpenReport "User Retest-Partners", acViewPreview, , FilterCriteria, acHidden
DoCmd.OutputTo acOutputReport, ExportReportName, acFormatXLS, ExportFileName
DoCmd.Close acReport, ExportReportName, acSaveNo
rs.MoveNext
Wend
End If
Set rs = Nothing
End Sub
This is the version of the code that runs:
Private Sub Command1_Click()
Dim ExportReportName As String
Dim ExportFileName As String
Dim FilterCriteriaString As String
Dim Filter As String
Dim SQLstring As String
Dim rs As DAO.Recordset
ExportReportName = "User Retest-Stores"
SQLstring = "SELECT DISTINCT [2-8 User Retest Stores].[Store Num] FROM [2-8 User Retest Stores]"
Set rs = CurrentDb.OpenRecordset(SQLstring)
If Not rs.BOF And Not rs.EOF Then
rs.MoveFirst
While (Not rs.EOF)
Filter = rs.Fields("Store Num")
FilterCriteria = "[2-8 User Retest Stores].[Clinic (Default)] LIKE '*" & Filter & "*'"
ExportFileName = "U:\Marketing\Reporting\User Retest Needed\Database Exports\Store Reporting\" & Filter & ".xls"
DoCmd.OpenReport "User Retest-Stores", acViewPreview, , FilterCriteria, acHidden
DoCmd.OutputTo acOutputReport, ExportReportName, acFormatXLS, ExportFileName
DoCmd.Close acReport, ExportReportName, acSaveNo
rs.MoveNext
Wend
End If
Set rs = Nothing
End Sub
I have a form with one combo boxes and ok button. When a value from combo box is selected and clicked "OK", it opens a query based on the selected value.
That is fine, but it closes the form and then opens the query. I have to again click on the form tab to select another value and run query.
Is it possible, query runs in another window while form window is still open?
For combo box
I have a code in row source like
select distinct format(columndate, 'mm-dd-yyyy') from table1
For OK Button,
I have a code as below :
Private Sub Submit_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQL As String
Set db = CurrentDb
Set qdf = db.QueryDefs("query")
strSQL = "SELECT columndate," & _
"sum(qty1)," & _
"sum(qty2)," & _
"sum(qty3)," & _
"sum(qy4)" & _
"FROM table1 " & _
"WHERE table1.column_date = '" & Me.datefield.value & "' " & _
"group by table1.[columndate];"
qdf.sql = strSQL
DoCmd.Restore
DoCmd.OpenQuery ("query")
DoCmd.Close acForm, "Me.Form3"
Set qdf = Nothing
Set db = Nothing
Debug.Print strSQL
End Sub
I have one more question on this. The date field in fact in the format "dd-mm-yyyy" in the table, but in query it shows blank result as long as I change the format to "mm-dd-yyyy" in row sources as in the first query here
Your OK-button click handler would have to look like this
Private Sub btnOk_Click()
DoCmd.OpenQuery(Me!cboQuery, acViewNormal, acReadOnly)
End Sub
Also make sure that the Cancel property of your button is set to No.
Here is my problem:
I am working with MS-Access 2010. I have a form with a listbox reserved for employee names and employee IDs with 2 command buttons below. One button displays all of my employees. The other opens a modal form with filter criteria. In this model form i have another listbox with different HR categories (with multi-select enabled).
What I am attempting to do is: open my modal form, select one or more of these HR categories in my listbox, click a command button that then closes the modal form and updates the employee listbox based on the certain criteria i have selected.
Thanks in advance for help with this. This is driving me a little nuts :-)
Here is my updated code below.
Private Sub cmd_ExecuteEmpFilter_Click()
Dim varItem As String
Dim i As Variant
Dim strSql As String
'Building the criteria string from selected items in the list box
varItem = ""
For Each i In Me!lst_HRFilter.ItemsSelected
If varItem <> "" Then
varItem = varItem & " , "
End If
varItem = varItem & Me!lst_HRFilter.ItemData(i) & ""
Next i
'Filter the form using selected items inside the list box
txt_HRCat = varItem
strSql = "SELECT DISTINCT q.LastName, q.FirstName, q.EmployeeID " & _
"FROM qry_MasterEmployeeFilter As q " & _
"WHERE q.HomeTeamID=" & Me.txt_TeamID2 & _
"AND q.ReportingGroupID IN (" & varItem & ") " & _
"ORDER BY q.[LastName], q.[FirstName], q.[EmployeeID];"
Debug.Print strSql
Forms!frm_Employee_Updater.lst_AllEmps.RowSource = strSql
DoCmd.Close acForm, "frm_EmployeeFilter", acSaveYes
End Sub
you need to requery the main form after you have changed its recordsource.
Forms!frm_Employee_Updater.lst_AllEmps.Requery
I have a form (Cobind_frmMain) that allows the user to create a pool of titles that are attached to it. So there is a top level Pool Name (TopLvlPoolName) and on a subform, the titles are added to it. What I need is to issue a Report for each of the titles. I have the report and queries all set up. Right now, the report will show all the titles in one file. The titles are in a field called "CatCode".
What I need is the following:
1. Save each title as a PDF and save it to our server.
2. Open email and attach the PDF.
3. Repeat until all titles are done.
EDIT: This is what I have so far for code and the error message I still get is: "Too Few Parameters" on the Set Recordset line. I'm trying to set the parameter in the strSQL line. I want the PartPoolName (in Cobind_qryReport, a query) to equal the TopLvlPoolName on the open form. The SQL for Cobind_qryReport is listed below:
Private Sub btn_Run_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Set db = CurrentDb
strSQL = "Select * FROM Cobind_qryReport WHERE PartPoolName = " & Me.TopLvlPoolName
Set rs = db.OpenRecordset(strSQL)
On Error GoTo Err_PO_Click
If MsgBox("Do you wish to issue the cobind invites?", vbYesNo + vbQuestion, "Confirmation Required") = vbYes Then
rs.MoveFirst
Do While Recordset.EOF = False
DoCmd.OutputTo acOutputReport, "Cobind_rptMain", acFormatPDF, "K:\OB MS Admin\Postage\CoBind Opportunities\Sent Invites\" & [CatCode] & "_" & [PartPoolName] & "Cobind Invite_" & Format(Now(), "mmddyy") & ".pdf"
DoCmd.SendObject acSendReport, "Cobind_rptMain", acFormatPDF, , , , [CatCode] & "_" & [PartPoolName] & " Cobind Invite", "Please find the cobind invite attached. Response is needed by " & [RSVP] & ". Thank you.", True
Recordset.MoveNext
Loop
End If
Exit_PO_Click:
MsgBox ("It didn't work")
Exit Sub
Err_PO_Click:
MsgBox Err.Description
Resume Exit_PO_Click
End Sub
Cobind_qryReport SQL:
SELECT tblEvents.EventTitle, Cobind_tblPartic.CatCode, Cobind_tblPartic.CodeQty, Cobind_tblPartic.PartPoolName, Cobind_tblTopLvl.RSVP, Cobind_tblPartic.ID
FROM Cobind_tblTopLvl, Cobind_tblPartic INNER JOIN tblEvents ON Cobind_tblPartic.CatCode = tblEvents.EventCode
GROUP BY tblEvents.EventTitle, Cobind_tblPartic.CatCode, Cobind_tblPartic.CodeQty, Cobind_tblPartic.PartPoolName, Cobind_tblTopLvl.RSVP, Cobind_tblPartic.ID
ORDER BY Cobind_tblPartic.ID;
Thank you again for all your help!
You're query Cobind_qryReport has a parameter that you need to set. if you want to know the parameter name try the following code
Dim qdf As QueryDef
Set qdf = CurrentDb.QueryDefs("Cobind_qryReport")
If qdf.Parameters.Count > 0 Then
MsgBox (qdf.Parameters(0).Name)
End If
Update
Since you know you've got a parameter doing select * from Cobind_qryReport it might just be easier to set the parameter and then use the qdf to open the recordset e.g.
Dim rs as DAO.Recordset
Dim qdf As QueryDef
Set qdf = CurrentDb.QueryDefs("Cobind_qryReport")
qdf.Parameters(0).Value = 7832
Set foo = qdf.OpenRecordset()
Note: you can use the parameter name in the place of the ordinal when setting the parametervalue
e.g. qdf.Parameters("Foo").value = 7832