Query from a multi line textbox - ms-access

I have a table called A5K with a few columns, Most important being Location and Serial number
I have a split form with a multi line textbox and a search button. If I type in any location or serial number and click search. The record would appear in the datasheet form below.
What I'm trying to do is to search for multiple serial numbers/location at a time and these would appear in the datasheet.
For example, lets say I'm searching for location A,B,C&D, I would like to enter these into the multiline textbox as
A
B
C
D
Click search
and records A,B,C&D would all appear.

It can be done but is poor design. VBA code would have to build comma separated parameter array for use with IN() function. If the field type is text, also need apostrophe delimiters. Consistency of structure is critical in string manipulation. If the items are ALWAYS typed with a single CrLf between them:
Me.Filter = "Location IN('" & Replace(Me.textboxname, vbCrLf, "','") & "')"
Me.FilterOn = True

Related

Creating a link to a file on a form but file address is stored on another table

I have made a form that is used as an inter-department sign-off sheet for controlled documents on the network. The database already includes a table with a hyperlink field for each document's location. The table also has a text field of the document id. My form is storing its information in a different table, but I am hoping that I can still use the hyperlinks from the first table instead of creating new links in the second table.
My google searching lead me to this forum post that showcased a single line using FollowHyperlink and DLookup:
Application.FollowHyperlink DLookup("Document_ID", "Documents", "Document_ID = '" & Me.DocumentID.Value & "'")
Document_ID is the field that holds the hyperlink.
Me.DocumentID is the textbox that the user types in and the code runs when this field is clicked.
I have tried multiple variations of the code including wrapping table fields in brackets "[]", using either the hyperlink field or the text field for the criteria, using Like instead of = (along with asterisks around Me.DocumentID). All of which result in a run-time error 2471:
The expression you entered as a query parameter produced this error:
'Document_ID'
Which makes me think that Dlookup doesn't like hyperlink fields as I can pull other fields just fine. What am I missing? Or is there a better way to reference hyperlinks in a different table?
Thanks to HansUp for pointing me in the right direction. Turns out that "Document_ID" didn't exist in the table. Instead the hyperlink field had a space between the underscore and ID ("Document_ ID") and I didn't catch it until copying the name from the table into vba where spaces are more pronounced.
The Dlookup would not work when using the hyperlink field in the criteria so the text field with the document id was used instead and changed my code to the following as .FollowHyperlink wouldn't take the whole field value (title#address)
Dim LinkText As String
LinkText = DLookup("[Document_ ID]", "Documents", "[DocID] = '" & Me.DocumentID & "'")
LinkText = Application.HyperlinkPart(LinkText, acAddress)
Application.FollowHyperlink LinkText
The control will now open the documents that exist (now to work on code to use for when the documents don't exist)

Use a text box as a wildcard search on a field within a linked SQL Server Table within MS Access

I've managed to successfully build a combo-box that will allow a user to select a record from a list to get an ID from a table. With the table I'm currently working with running to around 60,000 records, it's not realistic to use this method to find the record.
What I want the user to be able to do is enter a name in a text box, and a combo box be populated with the relevant records from the table where one of the fields matches that. So if the user entered 'This' into the text box, the combo box would present records where the field had 'This', 'This and That' and 'this'. It would not present the record that only had 'That' in the field.
Lets say the Text box is called 'txtBox', the combo-box is called 'comBox' and the field in the linked SQL Server table 'LinkedTable' is called 'SearchField'
I would suggest you not use the combo box. Just have a text box in which the user can type in a few characters and hit enter key.
You THEN display a “list” of results that allows the user to select and click on any of the results to “edit” or “view” the given row of data.
In the following screen shot we working with a VERY small table of 500,000 rows. We looking for smith, so we just type in smi and hit enter. The results are displayed instant, and at that point the user can type in “first name” or just a few chars of first name and further drill down and filter.
The form looks like this:
The code in the after update event of the text box is simply:
Dim strSQL as String
strSQL = "select * from tblCustomers where LastName like '" & me.TextSearch & "*’"
me.RecordSource = strSQL
So very little code is required. You could I suppose fill in the results to a combo box, but then the user has to type into a box, then select something from a combo box and then somehow you bring up that record for editing. That’s like 3+ steps for the user.
Just use what Google or most accounting or darn near any computer software does:
A simple text box – you type in a few characters and when they hit enter the results are displayed for the user to pick.
Note in above the user can click on the “glasses” icon button to launch a form that displays the single record. The code behind that button is:
Docmd.Openform "frmEditDetails",,,"ID = " & ME!ID
I went down a different route to the suggested option in the other answer. This met my needs more accurately and might help someone with a similar issue.
First, I amended the query on the Combo box so that it used the value in the text box as a criteria for one of the fields.
In the Criteria for the 'SearchField' within 'LinkedTable' I entered:
Like "*" & [forms]![*FormName*]![txtBox] & "*"
This will restrict the results in comBox to those where the SearchField contains the value entered into txtBox. This doesn't update dynamically however and will only update after the first value entered into txtBox.
To get around this problem, I added the following to the 'On Get Focus' event for comBox
Private Sub comBox_GotFocus()
Dim ctlCombo As Control
' Return Control object pointing to a combo box.
Set ctlCombo = Forms!FormName!comBox
' Requery source of data for list box.
ctlCombo.Requery
End Sub
This will force the combo box to run the query again, using the current value in txtBox, each time it is 'clicked on'
As a result, I get the restricted list of values in the combo box that I need and allows the user to 'search' for a record that can then be used for creating a new record on a form.

Dcount wont count records matching a number entered into a form control

I want to have a form / database that the data goes into automatically search for duplicate entries. Duplicates can either be by reference number (which contains numbers an a letter) or by name.
I have tried this dcount on the control source for [counter] in form "Add New" that is bound to database "tracker" to count reference numbers that are duplicates, so that anything higher than a "1" count in the database would be flagged, but i can't get it to to count based on the value that the user enters into [reference number] on form "add new".
=DCount("*","tracker","'[Reference number]'= '[Tracker]![Reference number]'")
I want it to search all records in the table "tracker", where [counter] on form "add new" equals the number of records in "tracker" that have the same reference number as the user enters into the form control [retention number].
I ave set the "on Change" property to [Event Procedure} to trigger this count anytime that there is a change on the form. What am I doing wrong? Open to other approaches to solving the problem as well.
Your syntax is a little out, this assumes [Reference number] is a text string, and that the control on your form is also called [Reference Number];
=DCount("*","tracker","[Reference number]= '" & Me.[Reference number] & "'")
If its a number remove the single quotes.
This also demonstrates why its a good idea to rename controls on forms so you know it is the control and not the field. So if your control was called txtRefNumber you would know what you were referencing (and take the spaces out of field names - it makes for lots of extra typing of square brackets and mistakes.
Further Edit : If you put in the after update event of your txtRefNum field the following Me.YourCountControlName = DCount("*","tracker","[Reference number]= '" & Me.txtRefNum & "'") It should update. Remove the control source from the YourCountControl

How to Use a Form to Search a Table and Display Results in a Report in Microsoft Access 2010

I am fairly new to Access and I am having some trouble figuring out how to perform a search.
I have a table that contains records with multiple fields and one primary key field called "Serial".
I have created a form that is linked to this table and all it contains is a text box and a button where the user can type in any word or serial number and anything and the table will be searched for records that have a field that matches the criteria entered.
I have gotten to the point where I can search through the table and find any record with a match but I cannot figure out how to post this record (and any other records that match) to a newly created report so that the user can see all of the results that matched his criteria.
The code I have is as such and the output gives only a blank report.
The msgbox line always outputs the correct Serial number for every search.
I believe that the issue is related to the DoCmd.OpenReport line.
Do While Not rs.EOF 'iterate through table and check all fields
For Each Field In rs.Fields
If Field = SearchBar.Value Then
found = True
MsgBox (rs.Fields("Serial")) 'debugging
**DoCmd.OpenReport "Asset Inv", acViewReport, , "[Serial] = '" & rs.Fields("Serial") & "'"**
Exit For
End If
Next Field
If found Then
Exit Do
Else
rs.MoveNext
End If
Loop
Thanks for the help!
If you Want To search in Multiple Fields Based On one Keyword Do the Following Steps :
1- Suppose we have the following table That Want to search using a keyword in multiple fields
2- Open The Query Design And Choose You Table That You Want Search It
3 - Add Fields To The Query By Dbl Clicking On Desired Fields
4-Now Click On a Blank Column And Go To Design Tab
5-Save You Query
5-Click On Builder
6-Select Your Saved Query From The List
7-Select Fields You Want To Search in And Append Them
concatinate Fields That You Want Search in
8-Now You Have The New Column That Contained All Desired Fields
9-Now In Criteria Section Type Like ""+[]+""
10-Run The Query
Write "Like" Sql Command And Run Query
Enjoy It ...

Best practice for working with long texts in ms-access

I would be very thankful if somebody resolves my problem.
I'm new in working with Ms Access and I still gain experience on its basic functionality.
I have a table MyItems. 2 of its fields are: ItemCode and ItemName. ItemName is a very long text (Memo type). I have also a query and a form with many fields. The form's record source also consists of many fields. All these things (associated with 1 field) have the same or similar names so I can't differentiate them quite well.
What I want is when I set the value of ItemCode (in a not bound Combobox or Listbox with name ItemCode) the value of ItemName to be displayed in a control - maybe TextBox.
I can display its value in a ListBox (by sql query in its row source), I have no problems with this, I have no problems with managing events, but the text is very long and is cut. I understood that unfortunately ListBoxes don't have multiline property. So maybe the most appropriate control to deal with is a TextBox. And maybe the most appropriate way to display the value is using DLookUp function in the TextBox's control source. But in this sea of items with similar or the same names I just can't deal with its syntax, I was trying again and again for a very long time. So I have 2 questions:
Are the TextBox control and DLookUp function in its control source the best way to extract long texts from a table without binding or there are more suitable controls (which directly work with sql query)?
What is the right syntax of DLookUp? - where exactly are there ' ', " ", [ ], .Value, =, &, where must I write the path to the table or the form and where it would be mistake? If I just write [ItemCode] what it would be associated with - the form record source, the table, the form control or anything else? I would be grateful if someone writes the correct syntax for my case or if he shares a link with plenty of examples for using DLookUp. Those that I found didn't satisfy me.
Either a bound control, or an unbound one. If unbound, you need to load the text with VBA code or with DLookup in the control source. There are no other options.
Personally I'd rather use the AfterUpdate event of ItemCode, and call DLookup there, but that's a matter of preference.
2.
It's not that complicated. You basically have the SELECT, FROM, WHERE parts of an SQL query in the 3 arguments. [] are needed for all identifiers containing spaces or other special characters, and when refering to form controls.
=DLookup("ItemName", "[my Table]", "ItemCode = '" & [ItemCode] & "'")
The single quotes '' are needed if ItemCode is text, not when it is a number.
You could also use doubled (escaped) double quotes, but that is much less readable.
=DLookup("ItemName", "[my Table]", "ItemCode = """ & [ItemCode] & """")
Now where does [ItemCode] come from?
Access first looks for a control on the form with the name ItemCode.
If there isn't one, it looks for a field ItemCode in the form's RecordSource.
These are the only ways [ItemCode] can be evaluated. To avoid confusion, it is recommended to name bound controls with the same name as their source field.
The syntax above is only valid if everything is on the same form. If [ItemCode] is on a different form, or you refer to it from a query, you use
=DLookup("ItemName", "[my Table]", "ItemCode = '" & Forms![my Form]![ItemCode] & "'")
For more complicated cases with subforms, see Refer to Form and Subform properties and controls
And to use it in VBA (in ItemCode_AfterUpdate):
Me!ItemName = DLookup("ItemName", "[my Table]", "ItemCode = '" & Me![ItemCode] & "'")