Form Filter by Month - ms-access

I have a Problem with my filter for a form. The form contains activity of persons on specific dates during a month.
I have a combobox:
.
This combobox RowSource groups and formats dates into month name and year (mmmm jj):
The combobox is unbound and has this OnClick event:
Private Sub Kombinationsfeld479_Click()
Me.Filter = "[tbl_Taetigkeitserfassung.TaetigkeitsDatum] = "
& Format(Nz(Me!Kombinationsfeld479, Date), "\#yyyy-mm\#")
Me.FilterOn = True
Filter of the form, when selecting October 18:
[tbl_Taetigkeitserfassung.TaetigkeitsDatum] = #2018-10#
But in the End it just shows the 01.01.2018.
I know there is a format mistake somewhere.
Thanks for your help!
P.S.: Is there a possibility to add a "Show All" selection within the combobox?

Should use combobox AfterUpdate event.
Format() function results in a text string, not a date. So the # delimiter does not apply, use apostrophe. Assuming data in field TaetigkeitsDatum is a true full date value, it must be formatted to match the combobox value. Don't execute the code if no value selected in combobox so Nz() should not be needed.
Yes, the combobox RowSource can have a "Show All" choice. This will require a UNION query.
SELECT "_Show All" AS Data FROM tbl_Taetigkeitserfassung
UNION SELECT Format(TaetigkeitsDatum, "mmmm yyyy") FROM tbl_Taetigkeitserfassung;
Then code can handle the selection.
If Me.Kombinationsfeld479 = "Show All" Then
Me.FilterOn = False
ElseIf Not IsNull(Me.Kombinationsfeld479) Then
Me.Filter = "Format([TaetigkeitsDatum], 'mmmm yyyy') = '" & Me.Kombinationsfeld479 & "'"
Me.FilterOn = True
End If

Related

Filter form based on two combo box selections

I have this two combobox on my form. Each one filters the form independently. but I want to filter the form based on both selections.
I have this code to a check box after update event to filter the form with the two combo box but it doesn't work:
combo19 is the name for the first combo box and combo21 is the name for the second combobox.
Private Sub Check34_AfterUpdate()
Me.Filter = Me.Combo19 & Me.Combo21
Me.FilterOn = True
Me.Refresh
End Sub
The Filter property is a string expression consisting of a WHERE clause without the WHERE keyword. For example, the following Visual Basic code defines and applies a filter to show only customers from the USA:
Me.Filter = "Country = 'USA'"
Me.FilterOn = True
https://msdn.microsoft.com/en-us/vba/access-vba/articles/form-filter-property-access
In your case, it would look like:
Me.Filter = "field1 = '" & Me.Combo19 & "'" & " AND field2 = '" & Me.Combo21 & "'"
Where Field1 and Field2 should be replaced with the actual column names in your recordsource.
Also, your code doesn't check if Combo19 or Combo21 is empty.

MS Access RecordSource selection error

I am new to Access and have a form that displays a list of employees that is fetched from a query. It is a continuous list and I have a way to filter by employee type. I wanted to isolate the new record in the continuous form, so I added a button that changes DataEntry = True, however, when changing DataEntry = False, I ran into issues with the filtering working, and they appeared to stem around the RecordSource.
The filtering is done using a combobox that calls a simple 'Requery' 'AfterUpdate()', and the query itself gets the values as criteria from the combobox selection.
In the button to change to DataEntry = False, I have been trying to assign the RecordSource as well. Initially, it seemed to work great, but then Access crashed and now I am getting error 3701 once the button is pressed.
I have tried various different syntax to try to set the RecordSource.
Me.RecordSource = "qryName"
Form.RecordSource = "qryName
Forms!frmName.RecordSource = "qryName
and all of the above with "SELECT * FROM [qryName]" instead of a simple string.
Each gives me a 3701 error.
What am I doing wrong?
This sounds overly complicated. Just set the Filter property of the form - you can use the combobox to do that:
Me.Filter = "EmployeeType = '" & Me!ComboSelectedType.Value "'"
Me.FilterOn = True
or, if the value is numeric:
Me.Filter = "EmployeeType = " & Me!ComboSelectedType.Value ""
Me.FilterOn = True

MS Access FIlter on subform changing values of other fields

I have a datasheet subform with a combobox name Loan_ID_cbo. Whenever I update the filter via the combobox, the subform is updating the Loan ID (primary key) with the selected Laon ID from the subform, thereby changing the data on the table.
I would like to only filter this data and not allow the filter to edit the data on the table. How can I prevent this from happening?
Here is my VBA code for the After_Update event:
Private Sub Loan_ID_cbo_AfterUpdate()
Application.Echo False
Me.Filter = "MyKey = '" & Loan_ID_cbo & "'"
Me.FilterOn = True
If Loan_ID_cbo = "" Then
Me.Filter = ""
Me.FilterOn = False
End If
Application.Echo True
End Sub
If I understand correctly, your combobox is in the subform datasheet. Thus the combobox appears on each row.
It does happen probably because your combobox is bound to the field Mykey. So changing the combobox do change the value of Mykey.
You should not make a filtering combobox out of the myKey field in the subform, you should make an unbound combobox in the parent form :
In the subform, delete this combobox, and add a new textbox and bind it to Mykey.
On your mainform, create a new combobox and use this one to filter your subform. So the code would be something like :
Private Sub Loan_ID_cbo_AfterUpdate()
Application.Echo False
Me.subformname.Form.Filter = "MyKey = '" & Loan_ID_cbo & "'"
Me.subformname.Form.FilterOn = True
If Loan_ID_cbo = "" Then
Me.subformname.Form.Filter = ""
Me.subformname.Form.FilterOn = False
End If
Application.Echo True
End Sub

how to filter on load in MS access?

when double clicking on a value i need to open a form. the value gets passed to the opened form. I want the form to directly filter on that value instead of pushing on a button first. i tried filtering on change and on load but it doesn't work. when loading it doesn't know the value because it gets added after it opened the form.
this is the code for passing the value:
DoCmd.OpenForm "SubmenuRubrieken", acNormal
Forms!SubmenuRubrieken.Tv_rubrieknaam.Value = Me.Tekst14.Value
this is the code for filtering on that value in Tv_rubrieknaam:
Dim filter As String
filter = ""
If Not IsNull(Tv_rubrieknaam) Then filter = filter & " AND rubrieknaam = '" & Tv_rubrieknaam.Value & "'"
Me.filter = Right(filter, Len(filter) - 5)
Me.FilterOn = True
for some reason it doesn't trigger the filter on changing the value of Tv_rubrieknaam. how do i need to solve this?
I guess, the Form_Load() event is finished before you set the value (OpenForm is performed before value is set), so you would have to do the filtering in the OnChange() event of the Textfield or similar.
Better: pass the Me.Text14.Value with the DoCmd.OpenForm command either as a prepared whereCondition OR as OpenArgs with filtering option in the On_Load event.
A basic example:
I have a Form1 with a TextBox called Text0 on it. Text0 has a value of 2.
I have a Form2 with a Table called Table1 as Recordsource. Table1 has a column called Field1 containing numbers between 1 and 3
All I need to do is add the following code into the module of the Form1 and the moment I click on Text0 Form2 will be openend filtered down to rows with Field1 = 2
Private Sub Text0_Click()
DoCmd.OpenForm "Form2", acFormDS, , "Field1 = " & Nz(Me!Text0, 0)
End Sub

Add Criteria To VBA Code and Requery

So the following VBA code I have will requery the form based on the Combo1 value equal to January. Eventually, I'll have X number of years in the Combo value and only want to display all records based on each year.
Is it possible to add additional VBA code to use the Criteria from the query instead of having X amount of queries and requery them based on the year.
Private Sub Combo1_AfterUpdate()
If Combo1.Value = "2013" Then
[Form_Main Form].RecordSource = "Main_Form_Query"
[Form_Main Form].Requery
End If
End Sub
You can construct the query in runtime (remember: a query object in access is just an sql statement)
So, let's asume that your data is stored in a table called tblMyData that has columns called month (String, month name) and year (Numeric, Integer) (just examples). You can generate the SQL query in VBA this way:
...
Dim strSQL as String
...
strSQL = "SELECT * FROM tblMyData " & _
"WHERE month='" & ComboMonth.Value & "' " & _
" AND year=" & ComboYear.Value ";"
...
[Form_Main Form].RecordSource = strSQL
[Form_Main Form].Requery
...
Hope this helps you.
Open the form in a different way.
DoCmd.OpenForm "MyForm",,,"MyYear=2013"
Or
DoCmd.OpenForm "MyForm",,,"MyYear=" & Me.MyCombo
Assuming that MyYear in the table or query is numeric. If it is not, you can use single quotes.
The general idea is that you use a form with all records, and use the Where argument of OpenForm to specify the records that should be shown. You can do the same with reports.
I have only a vague idea about what you're trying to accomplish. It might help to show us the SQL from Main_Form_Query. Meanwhile, consider whether you can use the form's .Filter property to do what you need.
If the form's Record Source includes a text field named fiscal_year, you could filter the rows displayed based on the year selected in your combo box.
Private Sub Combo1_AfterUpdate()
' .Value is the default property, so not required after Me.Combo
Me.Filter = "fiscal_year = '" & Me.Combo1 & "'"
Me.FilterOn = True
End Sub
Yet another approach could be to reference the combo value in Main_Form_Query.
WHERE
Forms!YourForm!Combo1 Is Null
OR some_field = Forms!YourForm!Combo1
Then do Me.Requery in the combo's After Update event.