How to sort filtered ListBox - vba

I have created search box for a ListBox in form. Search function works as intended, but I would like results to show in ascending order based on first column. I am having trouble understanding how to do that. My code:
Private Sub searchTB_Change()
Dim strSource As String, strSearch As String
'code for searching records
strSearch = Replace(searchTB.Text, "'", "''")
strSource = "SELECT [ID], [VP_veiklioji], [VP_invented_name], [Pareisk_pav], [Par_gavimo_data], [Finished] " _
& "FROM TerPar_Oldsys " _
& "WHERE [ID] LIKE '*" & strSearch & "*' " _
& "Or [VP_veiklioji] LIKE '*" & strSearch & "*' " _
& "Or [VP_invented_name] LIKE '*" & strSearch & "*' " _
& "Or [Pareisk_pav] LIKE '*" & strSearch & "*' " _
& "Or [Par_gavimo_data] LIKE '*" & strSearch & "*' " _
& "Or [Finished] LIKE '*" & strSearch & "*' " _
'up to this part everything works
'line below do not work (it gets whole code in red in debugger) and I do not know how to fix it
& "ORDER BY" "[ID]" ASC,"
'bottom two lines work too, I have tryed DoCmd.SetOrderBy but do not understand how to make it work either
Me.oldListBox.ColumnWidths = "1.5 cm; 3 cm; 4 cm; 4 cm; 2 cm; 0.6 cm"
Me.oldListBox.RowSource = strSource
End Sub
EDIT: In my opinion it is not duplicate, since I am aiming at totally different architecture which turns out needed only quotes removal as Gustav suggested.

Remove the quotes and the comma:
& "ORDER BY [ID] ASC"

Related

Use one filter value to search and output several fields

Using Visual Basic.
Having searched and searched for an answer, my filter only selects from the 'Recipe' field. I'd like to input 'egg' into my txtSearch textbox and have my button give all recipes that have 'egg' in the recipe or as text in an ingredient. Currently only outputs 2 results: egg nog and eggs benedict. There are 15 recipes with egg in that I'd like to display, too.
Private Sub Search_Button_Click()
On Error GoTo Search_Button_Click_Err
Dim strSQL As String
strSQL = "[Cocktail] like '*" & [Forms]![Find]![txtSearch] & "*'" & _
" Or [Ing1] like '*" & [Forms]![Find]![txtSearch] & "*'" & _
" Or [Ing2] like '*" & [Forms]![Find]![txtSearch] & "*'" & _
" Or [Ing3] like '*" & [Forms]![Find]![txtSearch] & "*'" & _
" Or [Ing4] like '*" & [Forms]![Find]![txtSearch] & "*'" & _
" Or [Ing5] like '*" & [Forms]![Find]![txtSearch] & "*'" & _
" Or [Ing6] like '*" & [Forms]![Find]![txtSearch] & "*'" & _
" Or [Ing7] like '*" & [Forms]![Find]![txtSearch] & "*'"
If Len(strSQL) > 255 Then
MsgBox "ApplyFilter string length exceeds 255 characters"
Else
DoCmd.ApplyFilter "", strSQL
End If
Search_Button_Click_Exit:
Exit Sub
Search_Button_Click_Err:
MsgBox Error$
Resume Search_Button_Click_Exit
End Sub
an option is to concatenate all of the SQL fields that may contain what you're looking for then have that result in the where xxx like ....
Also some SQL environments use a % as a wildcard.

Issues filtering a listbox using variables

I'm unsure whether or not this is the best way to accomplish what I'm attempting to accomplish, but I'm currently attempting to alter the active filters on a listbox using multiple text boxes.
I have one main search box that filters an initial list box, this will also filter a few of my other list boxes that also populate relevant data.
When I look into my other list boxes (not my main one) I have data that I would like to filter further. So, I go into another textbox and input whatever it is I'm attempting to sort out, when I click the second "search button" this is where I get my error. It's a syntax error, but I don't fully understand the syntax behind it.
I'd like for my initial filter to be kept, in addition to my new criteria for searching.
Here's the code behind my initial search button (which this works)
Private Sub Command37_click()
Dim sql As String
Dim sql2 As String
sql = "SELECT People.LName, People.[Phone #], People.State " _
& "FROM People" _
& "WHERE SystemLocation LIKE '*" & Me.SearchTxt1 & "*' " _
& "ORDER by People.LName "
me.List35.RowSource = sql
me.list35.requery
sql2 = "SELECT Orders.Item, Orders.Price, Orders.Department " _
& "FROM People INNER JOIN Orders " _
& "ON People.SystemLocation = Orders.Department " _
& "WHERE Department LIKE '*" & Me.SearchTxt1 & "*' " _
& "ORDER by Orders.Department"
Me.List41.RowSource = sql2
Me.List41.Requery
Here's the code that I was trying to use with the secondary filter
Dim sql2 As String
sql2 = "SELECT Orders.Item, Orders.Price, Orders.Department " _
& "FROM People INNER JOIN Orders " _
& "ON People.SystemLocation = Orders.Department " _
& WHERE (People.SystemLocation LIKE '*" & Me.SearchTxt1 & "*') & (Orders.Item LIKE '*" & Me.SearchTxt2 & "*' " _
& "ORDER by Orders.Department"
Me.List41.RowSource = sql2
Me.List41.Requery
Ideally I'd like to be able to create a "search engine" of sorts.
The syntax error is surrounding your where clause.
Firstly, you are missing a double-quote here:
& WHERE (People.SystemLocation LIKE '*" & Me.SearchTxt1 & "*') & (Orders.Item LIKE '*" & Me.SearchTxt2 & "*' " _
^----- Double quote missing
But the main problem is that you are using the ampersand concatenation operator (&) to represent a logical and statement:
"*') & (Orders.Item LIKE '*"
^----- This should be AND
Instead, you should use the and operator, e.g.:
& "WHERE (People.SystemLocation LIKE '*" & Me.SearchTxt1 & "*') AND (Orders.Item LIKE '*" & Me.SearchTxt2 & "*' " _
However, in this case, the concatenation of form control values isn't required (and opens the potential for SQL injection) since you can directly reference a form value from the Row Source of the list box.
As such, the code can become:
Me.List41.RowSource = _
"SELECT Orders.Item, Orders.Price, Orders.Department " & _
"FROM People INNER JOIN Orders ON People.SystemLocation = Orders.Department " & _
"WHERE " & _
" People.SystemLocation LIKE '*' & Forms![YourForm]!SearchTxt1 & '*' AND " & _
" Orders.Item LIKE '*' & Forms![YourForm]!SearchTxt2 & '*' " & _
"ORDER by Orders.Department"
Me.List41.Requery
Here, change [YourForm] to the name of your form.

access docmd.openform text complex criteria

Here is the code that is not working for me:
DoCmd.OpenForm "frm8SerialsByModel", acFormDS,,"[Serial Number] like '" & Me.txtserialap & "' or like '*" & Me.txtserialap & "' or like '" & Me.txtserialap & "*' or like '*" & Me.txtserialap & "*'"
Regardless the following works perfectly in query criteria:
Like [Forms]![frm8Serials]![txtserialap] Or
Like "*" & [Forms]![frm8Serials]![txtserialap] Or
Like [Forms]![frm8Serials]![txtserialap] & "*" Or
Like "*" & [Forms]![frm8Serials]![txtserialap] & "*"
So I know that the question was most probably asked before, so I would like to ask you to recommend an article with a proper explanation of punctuation on writing string criteria containing different types of data?
Thank you in advance.
You need to treat the WhereCondition argument in DoCmd.OpenForm as any WHERE clause in SQL. Therefore, your field, Serial Number, needs to be repeated for each condition. And first LIKE is redundant as it uses no wildcards so can be replaced with equality =:
strfilter = "[Serial Number] = '" Me.txtserialap & "'" & _
" OR [Serial Number] LIKE '*" & Me.txtserialap & "'" & _
" OR [Serial Number] LIKE '" & Me.txtserialap & "*'" & _
" OR [Serial Number] LIKE '*" & Me.txtserialap & "*'"
DoCmd.OpenForm "frm8SerialsByModel", acFormDS, ,strfilter
Thank you, Wayne G. Dunn. So as you suggested I don't need to compare it 3 times, the following is enough for my purpose and it works:
DoCmd.OpenForm "frm8SerialsByModel", acFormDS, , "[Serial Number] like '" & Me.txtserialap & "'"
Still, I will be very grateful if you suggest any resources to read about complex criteria in VBA. Thanks a lot.

MS Access 2010 - Split form flickering [Conditional formatting]

Problem:
Everything was working fairly well, and then I added two fields to the form body (and the datasheet): "Impact" and "Interest". I set these to have conditional formatting, so that if they contain the text "red", they have a red fill.
When the form loads, the conditional formatting is instantly applied to the first entry in the datasheet view, and it takes a second to apply to the other two entries I put "yellow" and "green" in (which are conditioned to have a yellow and green fill, respectively). If I set the third entry to be selected when the form has loaded, this one has the formatting applied instantly to the datasheet view. MS Access says it is "Calculating", and when this finishes the conditional formatting is applied.
Further to this, when the first text filter is applied the form body text boxes "impact" and "interest" flicker, as well as the second text filter in the header and the "forename" of the selected entry in the datasheet view (my VBA code selects the forename after the filter is applied). When I move the mouse, the flickering stops and the filter applies. Sometimes the flickering doesn't happen, though. This has only happened since I added the "impact" and "interest" fields, with conditional formatting.
This is the basic structure of my form:
Form Header:
A few buttons, and two text boxes. The user types in their search terms, and hit the "apply filter" buttons to narrow down the data shown in the datasheet view.
Datasheet view:
The key columns are visible, and when the user clicks on one, it's profile is brought up in the form.
Form body: This contains the data from the datasheet, laid out according to category.
Here's the code for when my form loads - it loads all the relevant data, sets up a check box, and sets the focus on the first text filter:
Private Sub Form_Load()
Me.RecordSource = "SELECT * FROM Staff "
CheckImpInt.Enabled = True
CheckImpInt = False
StaffTotalSearchText1.SetFocus
End Sub
And here's the code for the first filter button:
Private Sub Filter1Button_Click()
If CheckImpInt = True Then
Me.RecordSource = "SELECT * FROM Staff " & _
"WHERE [Interest] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [Impact potential] Like '*" & Me![StaffTotalSearchText1] & "*'"
Else
Me.RecordSource = "SELECT * FROM Staff " & _
"WHERE [Forename] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [Surname] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [Position] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [Group] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [Division] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [All groups] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [Expertise] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [Institutes] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [Roles] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [skills - analysis] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [skills - compute] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [Notes] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [Paired IC] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [Paired Company?] Like '*" & Me![StaffTotalSearchText1] & "*' "
End If
End Sub
I'm not sure why recordsiurce is being set in your FormLoad event - it looks like it remains the same - except for filters.
I'd recommend
Set your form's recordsource to [Staff] and remove Me.RecordSource =
"SELECT * FROM Staff" from the Form_Load event
Change your filter_button event to use filters instead of using recordsource
Update FilterButton_Click sub
Private Sub Filter1Button_Click()
If CheckImpInt = True Then
Docmd.ApplyFilter , "[Interest] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [Impact potential] Like '*" & Me![StaffTotalSearchText1] & "*'"
Else
Docmd.ApplyFilter , "[Forename] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [Surname] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [Position] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [Group] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [Division] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [All groups] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [Expertise] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [Institutes] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [Roles] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [skills - analysis] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [skills - compute] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [Notes] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [Paired IC] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
"Or [Paired Company?] Like '*" & Me![StaffTotalSearchText1] & "*' "
End If
End Sub
You'd also get better performance if you moved these two filters into actual queries - then change the ApplyFilter to use the Where parameter instead of Filter, but that's a different tact altogether.
Finally if you want to remove the flicker completely you can add lines to stop the screen updates before you apply the filter - and then turn it on at the end of the sub.
Toggle Screen Updating
Add Application.Echo False before the if block
Add Application.Echo True after the if block

Pass value of combobox as field

Apologies if I have the wrong terminology. I am trying to build a form where the user selects the field from a dropdown and then enters search text into a text box. The form should then search the table for records that match the text in the field specified in the combobox.
However what I currently have is not working. I know where the error is but cannot fix it.
The current code is:
Private Sub btn_Search_Click()
Dim strSearchTerm As String
Dim strComboField As String
strComboField = Me!cmb_src
strSearchTerm = "SELECT Models.ID, Models.[Model Name], Models.[Model Brand], Models.[Model Category] " _
& "FROM Models " _
& "Where Fields(strComboField) LIKE '*" & Me.txtSearch & "*' " _
& "ORDER BY Models.[Model Name]; "
Me.sub_ModelList.Form.RecordSource = strSearchTerm
Me.sub_ModelList.Form.Requery
End Sub
I believe the error is in the line
& "Where Fields(strComboField) LIKE '*" & Me.txtSearch & "*' " _
As replacing Fields(strComboField) with a field name [Model Name] resolves fine.
Any help greatly appreciated
Thanks
Shouldn't it be:
strSearchTerm = "SELECT Models.ID, Models.[Model Name], Models.[Model Brand], Models.[Model Category] " _
& "FROM Models " _
& "Where [" & strComboField & "] LIKE '*" & Me.txtSearch & "*' " _
& "ORDER BY Models.[Model Name]; "
Try to use this:
"Where [Model Name] LIKE '*" & Me.txtSearch & "*' " _
or just create base query with WHERE like this:
Where [Model Name] LIKE '* & Forms![frm_YourForm]![cmb_src] & *'
and requery the form by click on Search button, the rest of VBA code not required
UPDATE: sorry, missed the point about selecting the field for search. In this case WHERE should be
"Where [" & strComboField & "] LIKE '*" & Me.txtSearch & "*' "
After
Me.sub_ModelList.Form.RecordSource = strSearchTerm
additional Requery not required, changing Recodsource automatically requeries the form