Reputation: 11
I've been asked by one of the departments in our School to create a database in which they can store and filter data with ease.
It's been a long time since i've used Access and I can't seem to remember or find a way to create a working Macro that Applies a filter to a form it opens.
I have a table set up called Main_Data with a Column called 'Class'. That is the data I am wanting to filter and be shown when the form is opened. The form that is being opened and filtered is 'Reading Student Entry Form'.
I am using the form builder at the minute and have it set up as follows:
Form Name: Reading Student Entry Form
View: Form
Filter Name:
Where Condition: [Main_Data]![Class]='class1'
Data Mode: Edit
Window Mode: Normal
Ideally when it runs I would like it so that the user can enter what class they want to apply to the filter as this method seems as though I am going to have to create several different macro's for each class and then assign them to command buttons.
Any helps is greatly appreciated!
Upvotes: 1
Views: 4569
Reputation: 8741
Like this ?
Copy this code in your private module of the form:
Private Sub Form_Open(Cancel As Integer)
'
Dim strClass
strClass = InputBox("Please type the class name:", "Class Input", "class1")
'
' not to open the form as no class given:
'
if(IsEmpty(strClass)) then
Cancel = True
Exit Sub
end if
'
' filter by class name:
'
Me.Filter = "Class='" & strClass & "'"
Me.FilterOn = True
'
End Sub
I suggest you also rename your form:
Form Name: Reading Student Entry Form => frmStudentEntry
Avoiding space in form names is always a good pratice.
Upvotes: 1