Access search form - search a delimited string - ms-access

I have created a simple search form with the ability to search for an individual Box reference number. The output is a report with the box number (or list of box numbers when the search returns multiple matches). For example searching for ABC111, returns a report like:
Box Description
ABC1110 Stuff
ABC1114 More stuff
ABC1119 Even more stuff
I use the following Criteria in my Search_Query
Like "*" & [forms]![Search_form]![Boxref] & "*"
But my customer wants to paste a list of boxes in the BOX Ref field like:
ABC1110, ADF1234, AGT2112
...and have the report display like this:
Box Description
ABC1110 Stuff
ADF1234 Cool stuff
AGT2112 More cool stuff
What criteria command do I need to write to achieve this?

You can use it this way
IN ("*ABC1110*","*ADF1234*","*AGT2112*")
or if you want you can use the textboxes of the search form
Criteria ="In ("
with [forms]![Search_form]
Criteria = Criteria & "*" & ![Boxref1] & "*"
Criteria = Criteria & ",*" & ![Boxref2] & "*"
Criteria = Criteria & ",*" & ![Boxref3] & "*"
......
end with
Criteria = Criteria & ")"
Or even write a loop to do it

Use Regular Expressions in your search criteria , go through below links you will get some idea
http://timothychenallen.blogspot.in/2006/05/ms-access-vba-regular-expressions-regex.html
http://bricestacey.com/2010/07/09/Regular-Expressions-in-MS-Access.html

Related

Migrating MS Access SQL and VBA functionality into Power App connected to data on Dataverse

After transferring a majority of my MS Access tables into Dataverse, I'm looking to transport this query as a DataSource for a Power Apps search box:
SELECT tblGCPC_ALL.ID, tblGCPC_ALL.DocumentNumber, tblGCPC_ALL.PONum AS [PO#], tblGCPC_ALL.Description, Nz([tbl_Vendors].[Vendor],"Pending") AS Vendor, tblGCPC_ALL.[J-Staff], tblGCPC_ALL.RemoteID, tblGCPC_ALL.Status, Nz([DateReceived],"Pending") AS Received, Nz([tblGCPC_ALL].[Receiver],"Pending") AS Receiver, tblGCPC_ALL.AssignedBuyer, IIf([Discrepancy]=0,"N","Y") AS Discr, Nz([tblGCPC_ALL].[REQ_ID],"N/A") AS REQ_ID, Mid([tblgcpc_all].[DocumentNumber],7,9) AS Expr1
FROM (tblGCPC_ALL LEFT JOIN q_VHUB_ALL ON tblGCPC_ALL.DocumentNumber = q_VHUB_ALL.VendorHolderID) LEFT JOIN tbl_Vendors ON q_VHUB_ALL.VendorID = tbl_Vendors.[Vendor ID]
WHERE (((tblGCPC_ALL.DocumentNumber) Is Not Null))
ORDER BY Mid([tblgcpc_all].[DocumentNumber],7,9) DESC;
The search box would only be about 1.5" long, but the dropdown datagrid that the query produces would produce a grid that is about 7" or more, wide.
Additionally, when the user searches in the box, the grid should grow or shrink depending on any qualified match of each column in the grid. Here is a sample of my KeyUp event in VBA for the search box:
Private Sub cboGCPC_Search_KeyUp(KeyCode As Integer, Shift As Integer)
Dim strSQL As String
strSQL = "SELECT * " _
& "FROM SRCH_GCPC " _
& "WHERE [DocumentNumber] Like '*" & Me.cboGCPC_Search.text & "*' OR [PO#] Like '*" & Me.cboGCPC_Search.text & "*' OR [Description] Like '*" & Me.cboGCPC_Search.text & "*' OR [Vendor] Like '*" & Me.cboGCPC_Search.text & "*' OR [Status] Like '*" & Me.cboGCPC_Search.text & "*' OR [Received] Like '*" & Me.cboGCPC_Search.text & "*' OR [Receiver] Like '*" & Me.cboGCPC_Search.text & "*' OR [J-Staff] Like '*" & Me.cboGCPC_Search.text & "*' OR [AssignedBuyer] Like '*" & Me.cboGCPC_Search.text & "*' OR [REQ_ID] Like '*" & Me.cboGCPC_Search.text & "*';"
Select Case KeyCode
Case 38, 40
KeyCode = 0
Case 1, 9, 13
Exit Sub
Case Else
Me.cboGCPC_Search.RowSource = strSQL
Me.cboGCPC_Search.Dropdown
End Select
End Sub
MS Access also allows either showing or hiding the column headers as well as AutoExpand. I'm sure this should also be available in PowerFX
Because this a typical convention of something complex in MS Access and VBA, I feel that mimicking this exact type of functionality into a Power App would serve as template that covers a lot of questions for my migration.
To replicate the functionality of your MS Access search box and dropdown datagrid in Power Apps, you can use a gallery control to display the search results and a text input control for the user to enter search terms.
Here are the general steps to achieve this:
1.Create a gallery control and set its Items property to a formula that retrieves data from Dataverse using the same SQL query as your MS Access search box. You can use the Filter function to filter the results based on the user's search terms. For example, if the user enters "ABC" in the search box, the formula would look like this:
Filter('GCPC ALL', StartsWith(DocumentNumber, "ABC") Or
StartsWith(PONum, "ABC") Or StartsWith(Description, "ABC") Or
StartsWith(Vendor, "ABC") Or StartsWith(Status, "ABC") Or
StartsWith(Received, "ABC") Or StartsWith(Receiver, "ABC") Or
StartsWith([J-Staff], "ABC") Or StartsWith(AssignedBuyer, "ABC") Or
StartsWith(REQ_ID, "ABC"))
Note that you should replace 'GCPC ALL' with the actual name of your Dataverse table.
2.Add text input control and set its OnChange property to a formula that updates the gallery's Items property based on the user's input. For example, if the text input control is named SearchBox, the formula would look like this:
ClearCollect(SearchResults, Filter('GCPC ALL',
StartsWith(DocumentNumber, SearchBox.Text) Or StartsWith(PONum,
SearchBox.Text) Or StartsWith(Description, SearchBox.Text) Or
StartsWith(Vendor, SearchBox.Text) Or StartsWith(Status,
SearchBox.Text) Or StartsWith(Received, SearchBox.Text) Or
StartsWith(Receiver, SearchBox.Text) Or StartsWith([J-Staff],
SearchBox.Text) Or StartsWith(AssignedBuyer, SearchBox.Text) Or
StartsWith(REQ_ID, SearchBox.Text)))
Note that you should replace SearchResults with the name of a collection that will store the search results.
3.Customize the gallery control's layout to match the appearance of your MS Access dropdown datagrid. You can use the same techniques as in MS Access to show or hide column headers and enable auto-expansion of columns.
4.Add any additional functionality you need, such as sorting the results by a specific column or adding pagination.
By following these steps, you should be able to replicate the functionality of your MS Access search box and dropdown datagrid in Power Apps, while leveraging the power of Dataverse as your data source.
The best approach to migrate this functionality into Power Apps is to utilize the Filter and Search functions. To do this, you will need to create a data source that combines the tables you have previously imported into Dataverse. You can then create a search box and use the Filter function to narrow down the results based on what the user is typing in the search box.
You can also add the ability to show or hide column headers and AutoExpand as you mentioned. To do this, you will use the Visible and Expand/Collapse properties in the Power Apps UI.
Finally, you can use the OnChange event of the search box to set the Filter for the data source of the search results. This way, when a user types something in the search box, the results will be updated accordingly.

Access VBA Keyword Search Function on multiple fields

I currently have a simple Access database with forms for the users to fill out based off a queried table.
My goal is to use a search box that can filter results based off a keyword multiple times. My existing code works great for a single search on 1 field. I want to be able to drill down off the first search by searching off another field. After I select my field from combo box and search keyword, my results are displayed. Once I pick another field from the same box and search, the results do not include my 1st filter.
On the form, I already have a combo box with a list of all the fields to choose from. Then next to that is a text box for the user to search off the chosen field list. I have correct VBA code to search off a single field, but I'd like to drill down from there. Basically, I want the ability to search a keyword on a selected field, and then be able to filter those results further by using the same search box again.
Example: On form, select "borrower" from drop down list and type "Smith" in search box, click search button. THEN I'd like to choose another field such as "Issue Category" from the same drop down list and type "late payment", then click search button. Thus, giving me all records containing the borrower Smith where issues exist of late payments.
I've been spending days on this and finally broke down to come here. I need to know what code I'm needing to add that would accomplish my goal of multiple searches without filter resetting. I am hoping you can help. Here is my code (Text35 is the text box and searchlist is the combobox list of field names):
Private Sub Search_Click()
Dim strSearchValue As String
strSearchValue = Me.Text35.Value
Select Case Me.searchlist.Value
Case "Date"
Me.Filter = "[Date] = #" & strSearchValue & "# "
Case "Account number"
Me.Filter = "[Account number] = #' & strSearchValue & '# "
Case "Borrower"
Me.Filter = "[Borrower] LIKE '*" & (Replace(strSearchValue, "'", "''")) & "*'"
Case "Issue Category"
Me.Filter = "[Issue Category] LIKE '*" & (Replace(strSearchValue, "'", "''")) & "*'"
End Select
Me.FilterOn = True
End Sub
I think you would use the OR keyword instead of &

Creating an Interactive Multi Field Search Bar in MS-ACCESS

I know this is probably a simple thing I'm missing, but I'm hoping you all can help. I'm trying to add a textbox to my form that allows users to type in search criteria and have the query filter the records to only display those that qualify. The trick is I want the user to be able to type in info and have it check all the fields of the form and return records for any that are valid.
I set up a query with the fields that I want checked and I watched a few tutorials on setting criteria, but they are all working with multiple search bars. Is there a way to do it with only 1?
Like "*" Or [Forms]![Publications Page]![FilterBox] OR "*"
This is the criteria expression I wrote. It returns records just not the ones I want and doesn't seem to change after I change what is in [FilterBox]. I have 4 fields I'm running this same criteria on. All thoughts and suggestions are greatly appreciated!
Thanks!
The resulting criteria should be something like
[LastName] Like '*searchtext*' Or [FirstName] Like '*searchtext*' Or ...
So, you would have to set up a single criteria like this
Dim crit As String
crit = " Like '*" & Replace(Me!FilterBox, "'", "''") & "*' "
where the replace statement replces single (') by 2. This enables the user to enter an apostrophe in the search box.
Now you must create the whole criteria
crit = "[Field1]" & crit & "Or [Field2]" & crit & "Or [Field3]" & crit & "Or [Field4]" & crit

Set up a parameter with multiple criteria in a query?

I am trying to set up a parameter in a query which will ask the user for two different letters, and will then display all records that have info which starts with either of those letters typed in by the user. What code would I put in the criteria part to accomplish this? Thanks
Like "[" & [Enter 2 letters] & "]*"
The user would enter, for example, ad or da. They could enter more than 2 letters.
If you want specifically 2 letters, or just more control, then you'll need to use VBA, and perhaps a TextBox on a Form, rather than a simple parameter-query.
As you want two dialogs (parameter boxes) you can use:
Like [First letter] & "*" Or Like [Second letter] & "*"
Again, they can enter more than a single letter in each box - which I consider a useful feature. You could restrict it to a single letter each with:
Like Left([First letter],1) & "*" Or Like Left([Second letter],1) & "*"
If they don't enter anything into the boxes then it will show all records. As mentioned, VBA would be needed to control the criteria more precisely.
If you really wanted to restrict to a single letter each then you can use:
Like IIf(Len([First letter])=1,[First letter] & "*",False) Or Like IIf(Len([Second letter])=1,[Second letter] & "*",False)

Search Function Using Queries and Controls

I am trying to utilize a search function on a form. I based the form on a query that is a copy of the table, except the criteria are linked to a control on the form. Ex.
WHERE (((tblFamily.FamilyName) Like "*" & [Forms]![frmFamily]![cntrlFamilyName] & "*")
I want to do this in other fields such as address, city, etc. as well. However, if I apply the same logic to the address field, blank records are ignored and never returned, even if nothing was put into the control.
How do I fix it so that when nothing is put in to the cntrlAddress the search does not ignore records with blank addresses.
You can append an empty string to your field and search on that:
WHERE tblFamily.FamilyName & ""
Like "*" & [Forms]![frmFamily]![cntrlFamilyName] & "*"
This will mean that tblFamily.FamilyName will not be null and when [Forms]![frmFamily]![cntrlFamilyName] is empty, the query will read:
Where "" Like "*"
Where "bob" Like "*"
And so on.