Microsoft Access SearchForRecord macro action useless? - ms-access

I have been trying to incorporate a built in macro action (SearchForRecord) in MS Access, however cannot get it to work for the life of me. There is minimal resources available online for this operation, and I've noticed that other people have struggled with the same issue.
I made a test database just to see if I could get it to work in the most basic form. I made a table with 3 columns (ID, Name, Colour) - I turned the table into a tabular form using the Form Wizard. I created a text box with a search button.
I then made a macro operation:
SearchForRecord
Object Type: Form
Object Name (Name of the Form) "frmNewSearch"
Record: First
Where Condition: ="txtIDSearch = '" & [Forms]![frmNewSearch]![txtSearchBox] & "'"
I took the where condition syntax directly from https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/searchforrecord-macro-action
I set the button click event to the Macro that I made.
In theory, I enter the ID into the txtSearchBox and it should bring up the appropriate record within the same frmNewSearch form.
When I try this, nothing happens and it just sits on the first record. I am using MS Access 2016 - is the macro action maybe just not supported in this version?
If there is another way at approaching this it would be much appreciated!
Cheers

Referenced article states:
Note the equal sign (=) at the beginning of the expression, and the
use of single quotation marks (') on either side of the text box
reference:
="Description = '" & Forms![frmCategories]![txtDescription] & "'"
Have to actually type = sign into argument. (Yes, I hear your ranting and cursing, but that's life.)
I presume txtIDSearch is name of textbox. The criteria must use name of field, which you say is ID. If ID is number type, don't use apostrophe delimiters (apostrophe delimiters are used for text fields, # delimiter for date/time, nothing for number type). So result will show like:
Where Condition: = ="ID = " & [Forms]![frmNewSearch]![txtSearchBox]
or since code and controls are on same form, simply:
Where Condition: = ="ID = " & [txtSearchBox]
However, both fail if form is a subform. This is because form is not open independently in Forms collection. A reference incorporating parent form name fails as well. Use VBA code methods.

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)

Filter subform table with textbox

I am trying to filter a subform with a textbox.
I have a query to show table records in the subform and I have the criteria to filter the table, but when I type in the textbox to filter the sub form it only shows me one record with that name. I need it to show me all the names.
The criteria for my query is below.
Like "*" & [Forms]![frmPlanningForecast]![FETextbox].[Text] & "*"
I then have an OnChange event on the textbox to requery the subform.
As mentioned above I need it to show me all the records matching what i have typed, not just one.
When I use the filter option within the table itself(Dropdown box on the field header) and select the filter from there, it works great. But I need it to be typed into a textbox.
The picture attached will show you what I mean, I have "EQ" typed in the text box but it has only returned 1 record when their are 15 called "EQ" on the table.
First of all add single quotes around Like parameter:
Like "'*" & [Forms]![frmPlanningForecast]![FETextbox] & "*'"
and second - Access has an old bug: if you refer in the query to form field like you did, it doesn't renew the value, used for criteria during Requery, you always will receive the same results. Workaround - replace reference to field by global function, which returns textbox value or use dynamic generating of RecordSource for subform.
Global function example:
Public Function GetFE() As Variable
GetFE = [Forms]![frmPlanningForecast]![FETextbox]
End Function
Place it in any standard VBA module. Then your Like will look like this:
Like "'*" & GetFE() & "*'"
I found an easier method and just wanted to let others know.
Instead of using criteria I used the following vba code.
DoCmd.ApplyFilter , (FETextbox = qryPlannedHours.LeadFE), SubFormPF
In older versions of Access, didn't there used to be an option in the query to use the sum field something like this:
[Formnane].[TextField]
I know that's very simplistic and I don't fully recall how to use it but it was something like that, simple and straight forward if you're not a VB user. Forgive my lack of conviction, I've used it before but it was 20 years ago.

Access Main Form 'Enter Parameter Value'

Sorry, I’m pretty new at Access so I might not be using some terms correctly (or might not know some terms at all).
I’m encountering an ‘Enter Parameter Value’ error when I put my subform1 into my mainform. On subform1 I have a button that runs query1, query2, query3. These 3 queries query tables and also calculated fields that are located on subform1. Subform1’s data source is query4. When I press the button (with 3 queries), everything works.
Once I place that subform1 onto my mainform (so that my user can press the button to run the queries without entering the subform1) I receive an ‘Enter Parameter Value’ error. Query1, query2, query3 are unable to find those calculated fields located on subform1. For example the ‘enter parameter value’ error is as follows: Forms!subform1!calculatedfield1. I’ve tried changing the ‘location’ to things like: Me.subform1!calculatedfield or Form!mainform1!subform1!calculatedfield but I still receive that same parameter error. I could move the calculated fields to mainform1, which makes the queries work fine. But I would like to keep all of the calculated fields on the subform. Does anyone have any suggestions?
I always name subform container control different from the object it holds, such as ctrDetails. Then this works for me:
Forms!Main!ctrDetails!fieldname
However, my preference would be to run SQL statement in VBA. So code behind button could be like:
CurrentDb.Execute "UPDATE table1 SET table1field = " & Me.subform1calculatedfield & _
" WHERE table1field Between " & Me.subform1calculatedfield & " And " & me.subform1calculatedfield2
Why do you need to save calculated data?

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

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] & "'")