Use one filter value to search and output several fields - sql

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.

Related

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.

How to sort filtered ListBox

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"

AdvancedSearch on Outlook through Excel VBA

I'm trying to perform an advancedsearch in my mailbox through Excel VBA but i don't know how to make a case-insensitive search.
Here is my Filter:
Filter = "urn:schemas:httpmail:subject LIKE '%Pending%'" & _
"AND urn:schemas:httpmail:textdescription LIKE '%" & ISIN & "%'" & _
"AND urn:schemas:httpmail:textdescription LIKE '%" & StlDate & "%'"
I tried with this filter but it does not work. I can't find the emails with "PENDING" in the subject. I also tried with this filter:
Filter = "(urn:schemas:httpmail:subject LIKE '%Pending%'" & _
"AND urn:schemas:httpmail:textdescription LIKE '%" & ISIN & "%'" & _
"AND urn:schemas:httpmail:textdescription LIKE '%" & StlDate & "%' ) OR " & _
"(urn:schemas:httpmail:subject LIKE '%PENDING%'" & _
"AND urn:schemas:httpmail:textdescription LIKE '%" & ISIN & "%'" & _
"AND urn:schemas:httpmail:textdescription LIKE '%" & StlDate & "%' ) "
Using "Option Compare Text" above your sub will allow you to search and find regardless of case. Try that and see if it solves it.

Hyperlink to fire macro in personal.xlsb

I am having a column in an excel with some text(date, sender,subject) which refer to an email. The problem is that(as far as I know) you can hyperlink to an outlook email in public folders, because the email might move(link varies from pc to pc).
So my idea to obtain that email is to make a hyper link that fires of a macro in the personal.xlsb, that then search for that email and display it.
My only problem is that I can not figure out how to link text to start a macro, Worksheet_FollowHyperlink means that I need to put that code in the sheet where my text is.
I guess I could do that, but this implements that I need to create this code when the workbook is opened and remove it when the workbook is closed, unless I have to rename all the files xlsx to xlsm, and because I am unsure if other colleagues have link to the excel sheet I would like to avoid doing so.
So my question is, is there any way to make a hyperlink to personal.xlsb!ShowEmail(cellValue) ? Or is it possible to make direct link to the email in the public folder? Below is the code for creating the email text:
Function getEpostField(projectNumber As String, drawingNumber As String, partNumber As String) As String
On Error Resume Next
Dim myFolderArray() As String
Dim i As Long
Dim j As Long
Dim k As Long
Dim OutApp As Object
Dim myNameSpace As Object
Dim myFolder As Object
Dim myNewFolder As Object
Dim TopPublicFolder As Object
Dim olMail As Variant
Dim myTasks
Dim strFilter As String
Set OutApp = CreateObject("Outlook.Application")
Set myNameSpace = OutApp.GetNamespace("MAPI")
Set TopPublicFolder = myNameSpace.GetDefaultFolder(18)
getEpostField = ""
' array with all subfolders where the item might be...
myFolderArray = Post.helpRequest("XXXXXXXXX")
For i = LBound(myFolderArray) To UBound(myFolderArray)
Set myFolder = TopPublicFolder.Folders("Prototech").Folders(myFolderArray(i, 2)).Folders
For j = 1 To myFolder.Count
If InStr(myFolder(j).Name, projectNumber) Then
If drawingNumber <> "" And partNumber <> "" Then
strFilter = "#SQL=" & Chr(34) & "urn:schemas:httpmail:textdescription" & Chr(34) & " like '%" & drawingNumber & "%' " _
& "or " & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " like '%" & drawingNumber & "%'" _
& "or " & Chr(34) & "urn:schemas:httpmail:attachmentfilename" & Chr(34) & " like '%" & drawingNumber & "%'" _
& "or " & Chr(34) & "urn:schemas:httpmail:textdescription" & Chr(34) & " like '%" & partNumber & "%' " _
& "or " & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " like '%" & partNumber & "%'" _
& "or " & Chr(34) & "urn:schemas:httpmail:attachmentfilename" & Chr(34) & " like '%" & partNumber & "%'"
ElseIf drawingNumber <> "" Then
strFilter = "#SQL=" & Chr(34) & "urn:schemas:httpmail:textdescription" & Chr(34) & " like '%" & drawingNumber & "%' " _
& "or " & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " like '%" & drawingNumber & "%'" _
& "or " & Chr(34) & "urn:schemas:httpmail:attachmentfilename" & Chr(34) & " like '%" & drawingNumber & "%'"
ElseIf partNumber <> "" Then
strFilter = "#SQL=" & Chr(34) & "urn:schemas:httpmail:textdescription" & Chr(34) & " like '%" & partNumber & "%' " _
& "or " & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " like '%" & partNumber & "%'" _
& "or " & Chr(34) & "urn:schemas:httpmail:attachmentfilename" & Chr(34) & " like '%" & partNumber & "%'"
Else
getEpostField = "No emails found"
Exit Function
End If
Set filteredItems = myFolder(j).Items.Restrict(strFilter)
If filteredItems.Count = 0 Then
Debug.Print "No emails found"
getEpostField = "No emails found"
found = False
Else
found = True
' this loop is optional, it displays the list of emails by subject.
For Each itm In filteredItems
attachmentString = ""
If itm.Attachments.Count > 0 Then
For Each temp In itm.Attachments
temp2 = InStr(temp.filename, drawingNumber)
If temp2 > 0 Then
attachmentString = attachmentString & temp.filename & " "
End If
Next temp
End If
Debug.Print "Dato:" & Format(itm.CreationTime, "mm.dd.yyyy") & " Subject:" & itm.Subject & " From:" & itm.SenderName & " Attachment:" & attachmentString
getEpostField = getEpostField + "Dato:" & Format(itm.CreationTime, "mm.dd.yyyy") & " Subject:" & itm.Subject & " From:" & itm.SenderName & " Attachment:" & attachmentString
Next
End If
'If the subject isn't found:
If Not found Then
'NoResults.Show
Else
Debug.Print "Found " & filteredItems.Count & " items."
End If
Exit Function
End If
Next j
Next i
End Function
=HYPERLINK("#personal.xlsb!modUtility.TestHL()","Test")
and a test function (returning a range a just results in the link selecting the already-selected cell)
Function TestHL()
Debug.Print "OK"
Set TestHL = Selection
End Function

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