I want to create a query which takes field parameters through a form. For this, I created a form with combo boxes and drop down options to select the values from, this populates a text value in the respective invisible text fields whose default value I have set to null. Now in my query I give criteria for column as iif(isNull([Forms]![Conditions]![text_on_form]), [column_in_table], [Forms]![Conditions]![text_on_form]). I have done this for all the columns on which the where clause comes from the form. I have tried running this. The results seem to be random. It worked for three columns, but when I played around with it, it was giving me empty result set. Can anyone tell me what I am doing wrong? Or if there is a better way to implement query by form in Access.
It sounds like you are trying to create dynamic SQL. Here is the method in vba I generally prefer:
Dim SQL As String
SQL = "SELECT tblName.* From tblName WHERE (1=1)"
If Not IsNull(Me.combo1) Then
SQL = SQL & " And ([Field1] Like ""*" & Me.combo1 & "*"")" ' I am using like statements here, but that is because this is a search tool.
End If
If Not IsNull(Me.combo2) Then
SQL = SQL & " And ([Feild2] Like ""*" & Me.combo2 & "*"")"
End If
Docmd.RunSQL SQL
End Sub
Basically, add on to the SQL statement only if the user has put a value into your text box/ combo box or whatever. The "Where (1=1)" is to account for a situation where all fields are null.
Play with this concept to create your SQL statements. Avoid using invisible text boxes to store data, it generally means you are doing something wrong and will get mixed results (someone else on this forum can explain better than me why that is).
Just use the Like operator. Put this in your criteria field in the query Like "\*" & Forms![Form_Name]![Form_Field] & "\*" -- This tells it to get anything if the field is blank (or null) and matches whatever you have in the field. This may not be what you want. It should be noted that it will return anything with the text string in it. For example: if you type "the" it will return tether, these, theses, thermometer (anything with the word "the" in it. It works best for multi word or longer strings that can be matched more accurately, however it works for a search query because there is usually a set of human eyes looking for the result and erroneous results is not a huge problem.
Related
I need to create a query in MS access where the parameter is a list (given by me).
This works In ("2209487";"2102669";"2727930";"3727550"), but if I try to put a parameter inside the "IN" like this: In ([NUM]) It doesn't return a result!
I write 2209487";"2102669";"2727930";"3727550 when the parameter window appears.
PS: my laptop is in European Portuguese so I use the ";"
You can't use a "list" or "multiple" values for this. But you CAN do this in the where clause of hte form (or report).
First, remove any paramters for the form/report. You want to be able to open/launch the form/report WITHOUT any prompts from the query. In fact, what follows will even work if you based the form/report directly on the table.
So, you can do this:
dim strInvoices as string
strInvoices = InputBox("Enter invoice numbers ',' between each")
dim strWhere as string
strWhere = "InvoiceNumber in (" & strInvoices & ")"
docmd.OpenReport "frmInvoices",,,strWhere
So, you can provide a list as per above and use the "in"
the IN clause is this
InvoiceNumber in (234,433,555)
So, you can pass the conditions as a "where" clause to a open form or report.
Thanks to all!!!!
i've redone the query and logic of the database) in another way.
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.
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] & "'")
I am trying to build an update query in which once the user enters some text into a text box I have installed on a form, will then have a query (or perhaps loop) run so that all records in an underlying table which contain the value entered into the text box will be flagged.
At the moment my strategy is to take the value in the text box, paste it into a holding table (which I called tblSearchEngine07), and then run an my query which is aimed to identify all records where in the field called 'tblMasterListOfEventsNotes' the value pasted in my holding table will be referenced. Unfortunately, I am having a syntax issue and am not successfully getting the holding table value to be correctly inserted into my query.
This is my current query syntax:
SELECT tblSearchEngine01.tblMasterListOfEventsNotes
FROM tblSearchEngine01, tblSearchEngine07
WHERE (((tblSearchEngine01.tblMasterListOfEventsNotes) Like "*[tblsearchengine07].[parolachiave]*"));
Try to substitute your like with this:
like "*" & tblsearchengine07.parolachiave & "*"
Update.
If you are not interested in storing your search values in a table, you can simply create your query string by reading textBox.value like this:
Dim sql as string
sql = "update yourtablename set yourfiled = true where yourconditionfield like '*" & textboxname.value & "*'"
Docmd.runsql sql
I have a subform that returns data from a table. However I want to further filter this data by using the filter property.
I have a group of radio buttons, combo boxes and a case statement which sets the variables I want to use to the correct values depending on the radio button selection
My code for filling the variables works perfectly but I cannot use vba to set the filter unless I manually type the string that I want.
I assume that my issue is that my filter string is syntactically incorrect but I am unsure of how. Probably something to do with text delimiters.
Forms![frmPendingActions]![qryPendingAction subform].Form.Filter = Filterby = FilterCrit
Forms![frmPendingActions]![qryPendingAction subform].Form.FilterOn = True
Assume for this question that Filterby=[Reporter] and FilterCrit= Fake Name
Yes I think the problem is to do with text delimiters. The code should look similar to the following:
Forms![frmPendingActions]![qryPendingAction subform].Form.Filter = "[Reporter] ='" & FilterCrit & "'"
The filter should be built exactly the same as a where clause without the word where. If the column you are filtering by has a text datatype then the criteria needs to be enclosed in single or double quotes. If the column is a date datatype then it needs to be enclosed in #. If the column is a number datatype then it does not need to be enclosed.
If you do not always wish to filter by the Reporter column then you can build a string using if statements or select case statements and then apply that string to the form filter.
For example:
If [somecondition] Then
strFilter="[Reporter]='" & FilterCrit & "'"
Else
strFilter="[ID]=" & 0
End If
Forms![frmPendingActions]![qryPendingAction subform].Form.Filter = strFilter
I hope this helps.