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.
Related
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.
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"
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
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
I am stuck trying to fix this error and have been at it for far too long. I've done a lot of Googleing and searching this site but can't seem to find the right answer for me even though there are many different variations of this question.
I am writing a front end GUI using VB.Net with an Access database on the back end. The DB has NULL values in PartnerFirstName, PartnerLastName, and Organization in my Partner Table. I am loading these into a Datatable in my application so they can be searched by the user. I just found out last week that some partners (the one's with NULL values) are not loading.
Code below:
("SELECT IDPartner, IIF(ISNULL(PartnerFirstName),'',PartnerFirstName) as PartnerFirstName, IIF(ISNULL(PartnerLastName), '', PartnerLastName) AS PartnerLastName, IIF(ISNULL(Organization), '', Organization) AS Organization, " & _
"Address1, City, State, Zip, Country, Active " & _
"FROM tblPartner " & _
"WHERE IDPartner like '%" & txtPartnerID.Text & "%' AND " & _
" PartnerFirstName like '%" & txtFirstName.Text & "%' AND " & _
" PartnerLastName like '%" & txtLastName.Text & "%' AND " & _
" Organization like '%" & txtOrganization.Text & "%' AND " & _
" Address1 like '%" & txtStreet.Text & "%' AND " & _
" City like '%" & txtCity.Text & "%' AND " & _
" State like '%" & txtState.Text & "%' AND " & _
" Zip like '%" & txtZipCode.Text & "%' AND " & _
" Country like '%" & txtCountry.Text & "%' " & _
"ORDER BY IDPartner, partnerlastname, partnerfirstname", cn)
The reason for the likes are because I want the search to update in real time based on the form's text box values. This is the source of my problem since they are blanks by default and the DB has nulls.
===================
EDIT:
Thank for the initial comments. I realize now that I was using an improper function. the IIF suggested by Baro gets me through the SQL now which is great!
It appears the issue I'm having with records not showing up is in my WHERE clause. If I remove that then all the records load into my Datatable. When I include the Where clause as shown the records will NULL values are excluded.
I have tried to use an OR PartnerFirstName IS NULL (as well as the other fields) but that leaves too many extra results that also have NULL values. I cannot figure out what to do to allow the search now that I know the problem is in the Where section of the statement.
The issue is that the WHERE predicate is targeting the original field(s), which have not had the IIF(ISNULL(...)) expression applied.
It is difficult to see because you've aliased the field in the SELECT statement as the same name as the underlying field. So, the query is doing what you've instructed it to do, albeit not what you've meant for it to do.
To resolve, I would use a subquery which converts the NULL values to empty strings and then in the outer query add your WHERE predicate. Something like the following should do the trick:
("SELECT q.* FROM (SELECT IDPartner, IIF(ISNULL(PartnerFirstName),'',PartnerFirstName) as PartnerFirstName, IIF(ISNULL(PartnerLastName), '', PartnerLastName) AS PartnerLastName, IIF(ISNULL(Organization), '', Organization) AS Organization, " & _
"Address1, City, State, Zip, Country, Active " & _
"FROM tblPartner) q " & _
"WHERE q.IDPartner like '%" & txtPartnerID.Text & "%' AND " & _
" q.PartnerFirstName like '%" & txtFirstName.Text & "%' AND " & _
" q.PartnerLastName like '%" & txtLastName.Text & "%' AND " & _
" q.Organization like '%" & txtOrganization.Text & "%' AND " & _
" q.Address1 like '%" & txtStreet.Text & "%' AND " & _
" q.City like '%" & txtCity.Text & "%' AND " & _
" q.State like '%" & txtState.Text & "%' AND " & _
" q.Zip like '%" & txtZipCode.Text & "%' AND " & _
" q.Country like '%" & txtCountry.Text & "%' " & _
"ORDER BY q.IDPartner, q.partnerlastname, q.partnerfirstname", cn)
Thanks to all for you help and suggestions, I have learned a lot from the few suggestions and really appreciate it!
I have solved the problem by going back into Access. I ran an update query on any NULL field and changed it to a "". Such a simple solution!
If I didn't have the WHERE requirements in place that I do this wouldn't have been such a big deal.