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.
Related
In my Access Form I have a Text Box that should have this value:
SELECT [QueryOne].[Company] FROM [QueryOne] WHERE ([QueryOne].[ID] = "1000"
So, I wrote in the ControlSource of this textbox the following string:
= ( SELECT [QueryOne].[Company] FROM [QueryOne] WHERE ([QueryOne].[ID] = "1000" )
but the result that I get is that the textbox.value is #Name?. Maybe this string should not be in the ControlSource?
Use DLookup:
=DLookup("[Company]"), "[QueryOne]", "[ID] = '1000'")
or more probably ID isn't a text column, then
=DLookup("[Company]"), "[QueryOne]", "[ID] = 1000")
Access text boxes cannot be directly used to execute SQL. You can however call a function from the text box by setting it as the Control Source and display the result. Access offers a number of functions which can be used to retrieve data from a database. DlookUp should do what you require here:
=DLookUp("[Company]","[QueryOne]","ID='1000'")
You probably want to be able to enter the company ID to return the name. To do that, change the function to refer to another input textbox on your form like so (obviously just change the form and text box names to match your elements):
=DLookUp("[Company]","[QueryOne]","[ID]='" & [Forms]![Form1].[txtCompanyID].[Value] & "'")
Of course, if you are doing anything more than the most trivial of lookups, the correct way of doing this would be to set up a Record Source for the form and set the text box Control Source to a field from this dataset.
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 using a DCOUNT function to look at a table in access and see if a record exists - if it does then return a yes value.
=IIf(DCount("*","COMMENTS","[PROJECTID] = " & [PROJECTID])>0,"YES","")
This works great if my ProjectID is all numeric values - I only get #Error on the alphanumeric values.
I know that I have to make the ProjectID criteria a string but am having trouble placing the quotes.
First, break out the DCount piece and work on that by itself.
It sounds like your COMMENTS.PROJECTID field is text datatype, so add quotes before and after the value you concatenate into the third DCount argument (Criteria):
DCount("*", "COMMENTS", "[PROJECTID] = '" & [PROJECTID] & "'")
After you have that piece working correctly, add it into your IIf() function.
So, say I have the following string
THIS IS STUFF (MORE STUFF)
How would I get the string to format as such in a textbox in SSRS?
THIS IS STUFF
(MORE STUFF)
This data is pulled from a single field in a query and I will not be able to manually inject a break line.
Also, sometimes the (More Stuff) is not in the field.
Extra examples:
STUFF AND THINGS
THINGS (STUFF)
THINGS AND STUFF (MORE STUFF)
You need to insert a line break into the string, i.e. set the textbox expression as something like:
="THIS IS STUFF" & vbcrlf & "(MORE STUFF)"
So the expression in a single textbox:
Will render as:
You can see there is a line break even though the string could fit on one line.
Edit after comment
OK, we need to handle various existing strings at the report level.
To do this, I would suggest using Custom Code, i.e. a function like:
Function SplitString (fieldValue As String) As String
If IsDBNull(fieldValue) OrElse IsNothing(fieldValue) Then
SplitString = ""
Else If InStr(fieldValue, "(") > 0
SplitString = RTrim(Left(fieldValue, InStr(fieldValue, "(") - 1)) & _
vbcrlf & Right(fieldValue, Len(fieldValue) - InStr(fieldValue, "(") + 1)
Else
SplitString = fieldValue
End If
End Function
I'm suggesting this as SSRS expression are notoriously brittle, and the lack of short circuiting means that what works for one string might display #Error on another. You could get around this with numerous IIf statements but Custom Code is much neater and easier to understand.
You can call the function with an expression like:
=Code.SplitString(Fields!MYFIELD.Value)
Either in a textbox expression or as a Calculated Field in the Dataset. The above works for me on your data:
I came across this issue while doing my project. And I would like to share my steps for this. Create individual placehloders within the textbox by selecting the expression and format each as you desired. You can easily add extra line by just moving the another item(another placeholder) to next line(by pressing enter-on the keyboard)
Here I created individual placeholders inside the list, gave the names and format just as I want.
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.