Microsoft Access 2010: ComboBox text extraction / search - ms-access

I have a text search in my form, which uses the following code to filter my table of staff data:
Private Sub Command71_Click()
DoCmd.ApplyFilter "", _
"[Forename] Like '*" & [Forms]![StaffTotalQuery]![StaffTotalSearchText] & "*'" & _
"Or [Surname] Like '*" & [Forms]![StaffTotalQuery]![StaffTotalSearchText] & "*'" & _
"Or [ResearchArea]. Like '*" & [Forms]![StaffTotalQuery]![StaffTotalSearchText] & "*'" & _
"Or [Skills] Like '*" & [Forms]![StaffTotalQuery]![StaffTotalSearchText] & "*'" & _
"Or [EndDate] Like '*" & [Forms]![StaffTotalQuery]![StaffTotalSearchText] & "*'"
End Sub
The search works for all fields apart from [ResearchArea].
Both ResearchArea and Skills are ComboBoxes on my split form.
Both ResearchArea and Skills are fields in my Staff table.
New Skills can be added in the split form, but new ResearchArea's can only be added in the table.
The drop-down box on the Skills ComboBox contains repeated entries( e.g. if my listed skills on five staff are: "", "", "", "Accounting", "Accounting", then these options will appear in the drop-box), and blanks. I'd like it only to show unique entries, but also for me to be able to create new ones in this split form.
I'd also like to be able to search all staff's ResearchArea's, which I could do if they were text (like "Forename").

You have a dot to remove on this line:
"Or [ResearchArea]. Like '*" & [Forms]![StaffTotalQuery]![StaffTotalSearchText] & "*'" & _
Aside, you cannot search dates - in a meaningful way - as you do.

Related

MS ACCESS - How to filter a subform datasheet by a main form's textbox like a searchbox

I have a main form1 (formview) and a subform2 (datasheetview) based on the same query like a splitform.
In the main form1 i have a textbox that i want to use like a searchbox for the subform2.
This searchbox has to filter the subform2 datasheet by searching in 3 different fields (name, type, number) and when finding a record that matches the searchbox's value, it has to filter the datasheet by this value.
I tried with macro Applyfilter and VBA but didn't succed..
Can someone help me?
Consider:
Private Sub tbxSearch_AfterUpdate()
Me.ctrDS.Form.Filter = "Member_name LIKE '*" & Me.tbxSearch & "*' OR TypeOfBusiness LIKE '*" & Me.tbxSearch & "*' OR Member_ContactNumber='" & Me.tbxSearch & "'"
Me.FilterOn = True
End Sub
Or
Private Sub tbxSearch_AfterUpdate()
With Me.ctrDS.Form.RecordsetClone
.FindFirst "Member_name LIKE '*" & Me.tbxSearch & "*' OR TypeOfBusiness LIKE '*" & Me.tbxSearch & "*' OR Member_ContactNumber='" & Me.tbxSearch & "'"
If Not .NoMatch Then Me.ctrDS.Form.Bookmark = .Bookmark
End With
End Sub

Use Access filter with own keyword search together

I'm new in access and vba. Now I have a problem with my first project. I have created a table and a form with a keyword search. My Keyword search works fine but if i use also the standard filter from access together, i only can use the filter from one column because all other filter values are not available. I have make an msgbox to see the Output and access puts me an underscore before the table name.
Public Sub btnSearch_Click()
Dim sql As String
sqlQuery = " brands.name LIKE '*" & Me.txtKeywords & "*' " _
& " OR brands.ID LIKE '*" & Me.txtKeywords & "*' "
sql = "SELECT brands.* " _
& " FROM brands " _
& " WHERE " & sqlQuery
Me.sfrmBrands.Form.RecordSource = sql
Me.sfrmBrands.Form.Requery
End Sub
and the output
MsgBox (Me.sfrmBrands.Form.Filter)
Output: ([_brands].[name]="Test")
But i need ([brands].[name]="Test")
It should read:
sqlQuery = " brands.name LIKE '*" & Me.txtKeywords & "*' " & _
" OR brands.ID LIKE '*" & Me.txtKeywords & "*' "
Also, all you do is to set the RecordSource of the subform leaving its filter setting untouched, so your messagebox will just display this filter.
To set the filter:
Dim sql As String
Dim Filter As String
sql = "SELECT * FROM brands"
Filter = "[name] LIKE '*" & Me.txtKeywords & "*' " & _
"OR [ID] LIKE '*" & Me.txtKeywords & "*'"
Me.sfrmBrands.Form.RecordSource = sql
Me.sfrmBrands.Form.Filter = Filter
Me.sfrmBrands.Form.FilterOn = True
Edit: Filter before filter:
Me.sfrmBrands.Form.RecordSource = sql
' and perhaps:
Me.sfrmBrands.Form.FilterOn = True
I have found the problem but i have not a solution for that.
if i search by keywords the
Me.sfrmBrands.Form.RecordSource = sql
changes the table name from the query name to the subform name
is there an other solution for
Me.sfrmBrands.Form.RecordSource = sql

Access VBA - Apply Filter - Multiple OR conditions

I converted my working macro to VBA script (also working) in Microsoft Access 2010:
DoCmd.ApplyFilter "", "[Forename] Like ""*"" & [Forms]![StaffTotalQuery]![StaffTotalSearchText] & ""*""", ""
I used the same formatting (I believe) to try and extend this filter to work with multiple fields, though it doesn't work:
DoCmd.ApplyFilter "", "[Forename] Like ""*"" & [Forms]![StaffTotalQuery]![StaffTotalSearchText] & ""*"" Or [Surname] Like ""*"" & [Forms]![StaffTotalQuery]![StaffTotalSearchText] & ""*""", ""
This is what I mean by doesn't work: If my data is:
[forename] [surname]
alex bobs
chris dean
then typing, for example "alex", or "a", doesn't filter the results at all. On the other hand, the code with just one filter does narrow down the data.
You could try an alternative method of applying a filter:
me.filter = "[forename] like '*" & Me.StaffTotalSearchText & "*'" & _
" OR [surname] like '*" & Me.StaffTotalSearchText & "*'"
me.filter =true
EDIT
As #Andre has commented, I have used single quotes (apostrophes) to encapsulate my strings. Your vba should work with the following change:
DoCmd.ApplyFilter "", "[Forename] Like '*" & _
[Forms]![StaffTotalQuery]![StaffTotalSearchText] & "*'" & _
" Or [Surname] Like '*" & [Forms]![StaffTotalQuery]![StaffTotalSearchText] & "*'"

Multiple text filters in Microsoft Access 2010

I would like to filter through my data with two text boxes, StaffTotalSearchText1 and StaffTotalSearchText2.
I understand that only the most recent DoCmd.ApplyFilter command appies, and that means I don't know how to apply two text filters.
Edit: Solution found - see Johnny's RecordSource code
One way of applying a filter is to do it do the loaded data (RecordSource) - still allowing a "DoCmd.ApplyFilter" to be used.
I've created a "lock filter 1" button. When enabled, the filter applies; when disabled, there is no filter.
Johhny's answer has the correct code to filter the loaded data upon loading it.
Just use both values in filter and the same code in both filter events:
DoCmd.ApplyFilter "", _
"[Forename] Like '*" & me![StaffTotalSearchText1] & "*'" & _
" Or [Surname] Like '*" & me![StaffTotalSearchText1] & "*'" & _
" Or [Forename] Like '*" & me[StaffTotalSearchText2] & "*'" & _
" Or [Surname] Like '*" & me[StaffTotalSearchText2] & "*'"
Edit: applying the second filter after the first:
If Not IsNull(Me![StaffTotalSearchText1]) And Len(Me![StaffTotalSearchText1] & "") <> "" Then
strFilter = "[Forename] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
" Or [Surname] Like '*" & Me![StaffTotalSearchText1] & "*'"
If Not IsNull(Me![StaffTotalSearchText2]) And Len(Me![StaffTotalSearchText2] & "") <> "" Then
strFilter = "(" & strFilter & ") AND " & _
"( [Forename] Like '*" & Me![StaffTotalSearchText2] & "*'" & _
" Or [Surname] Like '*" & Me![StaffTotalSearchText2] & "*')"
End If
End If
DoCmd.ApplyFilter "", strFilter
If you want to use RecordSource you can use the same filter but must add the SQL for select command:
strFilter = ""
If Not IsNull(Me![StaffTotalSearchText1]) And Len(Me![StaffTotalSearchText1] & "") <> "" Then
strFilter = "[Forename] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
" Or [Surname] Like '*" & Me![StaffTotalSearchText1] & "*'"
If Not IsNull(Me![StaffTotalSearchText2]) And Len(Me![StaffTotalSearchText2] & "") <> "" Then
strFilter = "(" & strFilter & ") AND " & _
"( [Forename] Like '*" & Me![StaffTotalSearchText2] & "*'" & _
" Or [Surname] Like '*" & Me![StaffTotalSearchText2] & "*')"
End If
End If
If strFilter <> "" Then
strFilter = "SELECT * FROM StaffTable WHERE " & strFilter
Else
strFilter = "[StaffTable]"
End If
Me.RecordSource = strFilter
Try this:
Me.RecordSource = "SELECT * FROM StaffTable " & _
"WHERE [Forename] Like '*" & [Forms]![StaffTotalQuery]![StaffTotalSearchText1] & "*' " & _
"Or [Surname] Like '*" & me![StaffTotalSearchText1] & "*' " & _
"Or [ProfessionalID] Like '*" & me![StaffTotalSearchText1] & "*'"
Assuming you're pointing to the correct objects (I'm guessing Forename is on a different form? It's the only one you're referencing by form name), this should work.

DoCmd multiple where clauses

I have form where user can search the database by worker ID , name/surname, job.
I want to make possible all possible variations e.g. ID+name/surname+job or ID+job, or name/surname etc.
I have done all except combinations with ID's. Searching only by ID works, but combinations don't as I do not know what is the correct syntax. I have tried many, many ways to do this, but with no result.
Here is searching only by ID:
DoCmd.OpenForm "DarbiniekiSearchResults", acFormDS
Forms!DarbiniekiSearchResults.RecordSource = "" _
& "SELECT * " _
& "FROM Darbinieks " _
& "WHERE ID_darbinieks=" & Forms!SearchDarbinieki!darbIDsearch
And here is what I am trying to do: ID+job:
DoCmd.OpenForm "DarbiniekiSearchResults", acFormDS
Forms!DarbiniekiSearchResults.RecordSource = "" _
& "SELECT * " _
& "FROM Darbinieks " _
& "WHERE ID_darbinieks= & Forms!SearchDarbinieki!darbIDsearch OR amats_darb= '" & Forms!SearchDarbinieki!darbAmatsSearch & "'"
And the error is: http://i.stack.imgur.com/NA2bi.png