DLookup in ControlSource property of a TextBox - ms-access

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.

Related

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.

MS Access 2016 - parameter input popup when passing a parameter using a where condition to open a report

My question is a simple one, and I cannot find a simple answer to this question, after much searching. Many of the answers are about more complex coding situations that are difficult for me to relate to this question.
I am opening a report from a form. I can specify the username parameter as "bill" and the report displays only the records where "bill" is the user name. If I manually change "bill" to "tom" it still works and displays the records where the user name is "tom"
Private Sub Command11_Click()
Const cstrForm2 As String = "Report1"
'DoCmd.OpenReport cstrForm2, acViewPreview, WhereCondition:="[username]=" & "'bill'"
End Sub
If I use the following code, and put in Me.username (username being the name of the textbox, and is unbounded)
DoCmd.OpenReport cstrForm2, acViewPreview, WhereCondition:="[username]=" & Me.username
MS Access prompts me to enter the parameter value. If I enter the parameter value, "tom" or "bill" I get the correct data on the report.
How do I get rid of the parameter input box, basically how do I pass the correct format of parameter value to get the result I want? It may have to do with using a combination of quotes and other characters, I think.
The answer is to create a query first. Apparently creating a query avoids the problem.
Create a query for the table in question, and in the criteria section of the query, under design view :
enter the following under criteria
[Forms]![Form1]![username]
Where Form1 is the name of the form and username is the name of the textbox.
When you get the query running, create a report using the query as a basis.
You still need the single-quotes:
DoCmd.OpenReport cstrForm2, acViewPreview, WhereCondition:="[username]='" & Me!username.Value & "'"

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?

ms access openform where no data

I have a frmA, based on qryA.
Button btnOpenfrmB on frmA opens another frmB, based on qryB.
But this frmB includes some unbound textboxes, with data from qryC, i.e.
=DLookUp("Field";"[qryC]";"[ValueC] = " & [ValueB]). If qryB has no data (records), Dlookup fields return errors and opening frmB, which includes these textboxes, ends with an error. I understand this behaviour of frmB (there is no value B, that's why an error), but HOW CAN I AVOID IT and correctly open frmB for entering the first record ? When qryB has at least one record, everything works OK.
Thanx in advance
Duski.
Just use Nz function if Field don't have, for instance 0 values:
=DLookUp("Field";"[qryC]";"[ValueC] = " & Nz([ValueB],0))
or iif function if Field can contain any value:
=iif(IsNull([ValueB]),"",DLookUp("Field";"[qryC]";"[ValueC] = " & [ValueB]))

error 2115 on combobox selection

I have a combobox on a form and I'm trying to programmatically select one of the items in the combobox after I've run my SQL query on the access database.
I use the following code to iterate over the items and set the selected item:
'Make the appropriate location appear in the combobox
For i = 0 To cboLocations.ListCount - 1
If Me.cboLocations.Column(0, i) = locindex Then
Debug.Print "locindex: " & locindex & vbCrLf & " Me.cboLocations.Column(0, i):" & Me.cboLocations.Column(0, i)
Me.cboLocations.SetFocus
Me.cboLocations.ListIndex = i '<<< error 2115
Exit For
End If
Next i
As indicated, I keep getting error 2115: The macro or function set to the BeforeUpdate or ValidationRule property for this field is preventing Access from saving the data in the field.
Neither of the properties for this combobox indicated in the error message is set to anything. So I'm stuck. Please advise.
programmatically select one of the items in the combobox
The way I've always done that is to assign something to the combo's Value like this ...
Me.MyCombo.Value = "target text"
Value comes from the Bound Column of the combo's selected row. (You can find Bound Column on the Data tab of the combo's property sheet.) Conversely, assigning "target text" to Value selects the matching row.
In your situation, I think you're trying to select the combo row which contains the same text as your locindex variable. And if that is true, then I think all you need is this ...
Me.cboLocations.Value = locindex
As far as when you do that, neither Before Update nor Validation Rule seems like the right choice to me. I suggest you do it from whatever code you're using to run your "SQL query on the database", immediately after the query.
You are probably colliding with the BeforeUpdate event.
Try using AfterUpdate.