Access Main Form 'Enter Parameter Value' - ms-access

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?

Related

Microsoft Access SearchForRecord macro action useless?

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.

Possible to change MS Access subform fields through VBA?

I can find plenty of examples of how to return specific records in a subform through altering the underlying source query in code and re-querying, but I'm struggling to use the same principle to alter the fields that are returned.
I have the following situation:
Combobox to select 1 of 5 fields. Subform is supposed to show the selected field plus a couple of static fields, lets call them fields 6 and 7
So in the case the user selects field 1 from the dropdown, subform should show fields 1, 6 and 7. In the case they pick field 4, subform should show fields 4, 6 and 7 etc.
This is what (amongst other things) I've tried:
Set the subform up through the wizard with a query (select field1, field6, field7) as source, amend said query after combobox selection is made:
Set qd = CurrentDb.QueryDefs("myqueryname")
qd.SQL = "Select " & mycomboboxselection & ",field6,field7 from mytablename"
Form_mymainformname.mysubformname.Requery
The query itself updates fine if I run that standalone after the change, but the subform within the main form doesn't change and when I click on the subform itself from the navigation window it seems to be stuck looking for field 1 as it asks me to input a parameter value
Can anyone help with how to achieve this please?
Set the RecordSource of the subform to the SQL:
Dim SQL As String
SQL = "Select " & mycomboboxselection & ",field6,field7 from mytablename"
Me!YourSubformControl.Form.RecordSource = SQL
It will force a requery of the subform.
Ok I found a solution after a few more hours searching and trial and error.
There are two seperate problems here. Firstly creating the subform uses the newly created form as the source object rather than directly using the query, and it seems that no matter how you try to manipulate the record source of said form, it doesn't like changing the fields it was built with.
So, create the subform, change the source object from the form to your query (and delete the now pointless newly created form), then you can use the code I started with:
Set qd = CurrentDb.QueryDefs("myqueryname")
qd.SQL = "Select " & mycomboboxselection & ",field6,field7 from mytablename"
Which works! sort of....
You have to close and reopen the form every time to see the actual updates though, which obviously isn't what we're going for
The other line does nothing:
Form_mymainformname.mysubformname.Requery
It works fine for a change of records, but apparently not for a change of fields. I suspect this is a bit of a ms quirk
The below, however, works, even though I feel like it should do exactly the same as the code line above:
Form_MyForm.MySubForm.SourceObject = Form_MyForm.MySubForm.SourceObject

Access ODBC Subform field size too small to edit

I have a form with a combo box that allows a user to filter data in a subform using
DoCmd.SearchForRecord , "", acFirst, "[Division] = " & "'" & Screen.ActiveControl & "'"
The user should then add data to the subform below.
I've two problems:
When the user tries to edit this data, they get "The field is too
small to accept the amount of data you attempted to add. Try
inserting or pasting less data". It doesn't matter which of the
fields the user tries to enter data into, they all suffer this
problem.
The "Division" field that has been filtered, shows up as #Error for
the new record line. I've set a temp variable for this (see below), so it should
be showing the value used for the filter, but does so with or without the default set.
TempVars.add "Div", [Combo5].Value
I've looked in my ODBC forms and all the data types/sizes etc look to be the same.
There's data already in this form, which can be edited, even though it's the same size, but no new records can be saved. As well as this I can edit data in the supporting table.
Answer
Thanks to #AVG and #WayneG.Dunn for help. I ended up using the following code and using the filter as suggested. I couldn't get it to work very well on the subform, so I hid the field and used that as a Master Field for the subform. Still getting and error message "field too small" and "#ERROR" in the new record label, but the form works, so I'm just going to use it as is. Code for reference:
DoCmd.SetFilter "Division", "Division = " & "'" & [Combo5].Value & "'"

LinkChildFields and LinkMasterFields being automatically set when changing recordsource

In Access Properties for a subform, I have "LinkChildFields = '' " and ""LinkMasterFields = '' " (i.e. blank for both, this is what I want: no link)
When I change the Recordsource of the subform, and requery the subform, (in VBA) they are both automatically being set to a field, in my case "CaseID" (resulting in no records being displayed)
This is my recordsource:
stSQL = "SELECT qryCasesAndCards1.CaseID, qryCasesAndCards1.CaseStatusID, qryCasesAndCards1.Status, qryCasesAndCards1.CompanyName, qryCasesAndCards1.NumberOfCards, qryCasesAndCards1.FullName, qryCasesAndCards1.CaseCreatedDate, qryCasesAndCards1.CaseClosedDate, qryCasesAndCards1.CreatedBy" & _
" FROM qryCasesAndCards1 where not caseID = " & Me.CaseID & " and cardnumber in (select qryCasesAndCards1.cardnumber from qrycasesandcards1 where qryCasesAndCards1.caseID = " & Me.CaseID & ")"
I've tried substituting a simpler query "select * from qryCases1" and the problem still occurs.
In VBA, right after the requery, I am debug.print'ing ".linkchildfields" and ".linkmasterfields" and this is how I can see they are both being automatically set to "CaseID"
I have tried changing both values in Access form properties to nonsense value, resaving, changing again, resaving etc, and still no joy.
I can workaround problem by setting both those values right after the recordsource runs, but there is an unacceptable delay when doing this (about 5 seconds for each value)
One thing I'm just wondering now, is wether my form filter is being propagated to the subform? (am opening it via "docmd.open ... caseid = x" )
CaseID might have at ONE stage some long time ago been entered in to those link fields... but it's definitely not now. It's like they're locked in an Access vault somewhere and it's thinking "golly gee i'm pretty sure he wants CaseID in there gee whiz I'll get right on that!"
I've found MSAccess: Change in subform recordsource causes loss of LinkChildFields binding and I've found Linking SubReports Without LinkChild/LinkMaster but I can't get any help from them.
Thanks for any help offered: I'm tearing my hair out :)
To keep Access from automatically updating the LinkChildFields and LinkMasterFields on a subform...
Ensure that the RecordSource queries do not have matching key fieldnames.
Ensure that the Link Child Fields and Link Master Fields are blank on the SubForm object's Property Sheet.
DO NOT set the LinkChildFields and LinkMasterFields properties in code (i.e. remove any previous code like MySubForm.LinkChildFields = ""), otherwise the negative side effects may not be resolved.
If the form and/or subform are bound directly to tables, simply update one of them to query the table and add an alias for one of the key fields. For existing queries, it is possible to add an alias for one of the key field names without negative consequences in probably most cases. In my case, I only had to add "AS Acct" to a single field in the SELECT part of the subform's RecordSource SQL--no other change was necessary in the FROM, WHERE or ORDER BY clauses even though the original [Account] fieldname is referenced in each of those clauses.
SELECT Actions.[Account] As Acct, ...
Of course, this required an update to the ControlSource property for the relevant control on the form.
These changes eliminated the extra few-second lag when changing both the parent record and when switching records on the subform! Likewise, it eliminated redundant events during record navigation!
I found no other setting that keeps Access from automatically re-binding the subform via the LinkChildFields and LinkMasterFields properties. As already pointed out in the comments, it also doesn't help by resetting the link fields (e.g. LinkChildFields = "") after setting RecordSource, primarily because Access takes time to reset the fields, much of that time spent in events fired both upon the automatic assignment of the link fields and again when they are reset to blank. (e.g., Form_Current is called at least twice for each new record navigation.)
Years passed... as even on Microsoft Access 2019, changing subform .RecordSource always alters original .LinkChildFields and .LinkMasterFields, reassigning good fields will cost us 5s time for each property, we have no way to avoid this.
As a workarround, we can use always the same query, for example, qrySelTmp, as .RecordSource for the subform, at the design time
Mainform.subform.Form.RecordSource = "qrySelTmp"
When we must change .RecordSource content, we change only the definition of the Query, like this
CurrentDb.QueryDefs("qrySelTmp").SQL = "SELECT qryCasesAndCards1.CaseID, qryCasesAndCards1.CaseStatusID, qryCasesAndCards1.Status, qryCasesAndCards1.CompanyName, qryCasesAndCards1.NumberOfCards, qryCasesAndCards1.FullName, qryCasesAndCards1.CaseCreatedDate, qryCasesAndCards1.CaseClosedDate, qryCasesAndCards1.CreatedBy" & _
" FROM qryCasesAndCards1 where not caseID = " & Me.CaseID & " and cardnumber in (select qryCasesAndCards1.cardnumber from qrycasesandcards1 where qryCasesAndCards1.caseID = " & Me.CaseID & ")"
If(Mainform.subform.Form.RecordSource = "qrySelTmp") then
Mainform.subform.Form.Requery
else
Mainform.subform.Form.RecordSource = "qrySelTmp"
End if
As shown in the code, we do only a subform .Requery instead of assigning a new .RecordSource.
As Subform.Form.Requery does not alter subform properties .LinkChildFields and .LinkMasterFields, we gain about 8s for each real change of .RecordSource.
In my case with 1 Customer - Many Orders diagram, I've a gain of time at the proportion of 2s over 20s.
What I done in the past is simply REMOVE the link child/master settings.
Then in code I SET the child critera. So in the following, the link master/child SHOULD filter by tour_id, but I simply INCLUDED the criteria in the SQL and then HAMMER OUT the settings.
Dim strSql As String
' load sub-form to this tour....
strSql = "select * from qryGuidesForTour2 where Tour_id = " & Me!ID
Me.subGuides.Form.RecordSource = strSql
Me.subGuides.LinkChildFields = ""
Me.subGuides.LinkMasterFields = ""
The above was a fix and fixed any performance issues. I suppose it really depends on "how" the main form record is set, but if you are setting up the "main" form record via code, then as above simply stuff in the child forms recordset directly with the critera you need. And while in above I did blank out the master/child settings, if you ARE going to allow adding of reocrds in the sub form, then likely in above you should set the master/child fields as opposed blanking them out.

DLookup in ControlSource property of a TextBox

I'm working on a little project in Access using VBA. I created a report with some data from three different tables. In the Report_Open method I want to assign a query to the ControlSource of a TextBox.
Here is my code
Me.textImpactP.ControlSource = DLookup("[ImpactText]", "Root_cause_basic_reports", "[Report_id] =" & rid & " And [Root_cause_basic_id] =" & rootCauseId)
When I run this I keep getting the dialog "Enter Parameter Value".
The parameter "people" is the result of my DLookup and is supposed to be the value of my TextBox on my report.
Does anyone have an idea how to get the parameter "people" as a value in my TextBox?
Enter Parameter value does not represent, what you think it does. It normally pops up when you try to open a Report whose record Source is based on a Query which requests a parameter. Check the Query again.