MS Access Query: Use a passed parameter for a list of records or the parameter " ALL" to generate the whole list - vba

I'm trying to pass a parameter to my query in the criteria field. The parameter is contained on a form in the combobox cboReportSender. cboReportSender contains a list of departments that I run reports for. Also contained in the list is " ALL". When this is selected, I wish the report to display all records. I'm sure that the query is looking for a field literally containing "Like *"
Am I going at this from a wrong angle?
IIf([Forms]![frmRunReport]![cboReportSender]=Trim(" ALL"),"Like *",[Forms]![frmRunReport]![cboReportSender])

Firstly, I wondered whether your combobox selected a numerical value or an alphanumerical value. But when I created a database for testing, neither worked for me in combination with the operator Like * . I needed to typ Like '\*' , with a single inverted comma before and after the asterisk. Even better, adding this inverted comma's works with both numerical and alphanumerical values.
The second thing I needed to change was, to add a second like-operator. In the end, your criterion would become :
IIf([Forms]![frmRunReport]![cboReportSender]=Trim(" ALL"),"Like '*'","Like '" & [Forms]![frmRunReport]![cboReportSender] & "'")

Consider returning the field itself when combobox selects ALL.
SELECT ...
FROM ...
WHERE myDepartmentField = IIf(
TRIM([Forms]![frmRunReport]![cboReportSender]) = 'ALL',
myDepartmentField,
TRIM([Forms]![frmRunReport]![cboReportSender])
)
And if combobox is empty, use NZ to default to field itself.
WHERE myDepartmentField = IIf(
TRIM([Forms]![frmRunReport]![cboReportSender]) = 'ALL',
myDepartmentField,
TRIM(
NZ(
[Forms]![frmRunReport]![cboReportSender],
myDepartmentField
)
)

Related

SSRS Setting Specific Value that includes multiple values

I have a report that I want to offer filtering options. I have a column [Division] with values Division1, Division2, Division3, ect.
I can filter the report using a parameter and choosing from Avaliable Values the Get Values from a query section.
However, I want to include a value that will be a "select all" value that won't apply any filtering.
(I've tried the multiple values box and it also does not work)
Thus I've tried to "Specify Values" for all the values, however I cannot figure out an expression that will include all the values.
I've tried """= Division1 or Division2 or (ect.)""" but it doesn't seem to work.
Any ideas?
Your parameter query should have a UNION like the one below
SELECT '<All values>' As val
UNION ALL
SELECT division
FROM mytable
The report parameter doesn't have to be multivalue in order not to apply the filter
In your query WHERE you should have an expression like the one below
WHERE (division = #Parameter OR #Parameter = '<All values>')
In case you want to use multivalue parameter
WHERE (division IN (#Parameter) OR '<All values>' IN (#Parameter) )
You should mark "allow multiple values" and add in Specific Value only all possible individual values. Then you will have "select all" value without adding it.

Coldfusion Query of Queries with Empty Strings

The query I start out with has 40,000 lines of empty rows, which stems from a problem with the original spreadsheet from which it was taken.
Using CF16 server
I would like to do a Query of Queries on a variably named 'key column'.
In my query:
var keyColumn = "Permit No."
var newQuery = "select * from source where (cast('#keyColumn#' as varchar) <> '')";
Note: the casting comes from this suggestion
I still get all those empty fields in there.
But when I use "City" as the keyColumn, it works. How do the values in both those columns differ when they both say [empty string] on the query dump?
Is it a problem with column names? What kind of data are in those cells?
where ( cast('Permit No.' as varchar) <> '' )
The problem is the SQL, not the values. By enclosing the column name in quotes, you are actually comparing the literal string "P-e-r-m-i-t N-o-.", not the values inside that column. Since the string "Permit No." can never equal an empty string, the comparison always returns true. That is why the resulting query still includes all rows.
Unless it was fixed in ColdFusion 2016, QoQ's do not support column names containing invalid characters like spaces. One workaround is to use the "columnNames" attribute to specify valid column names when reading the spreadsheet. Failing that, another option is to take advantage of the fact that query columns are arrays and duplicate the data under a valid column name: queryAddColumn(yourQuery, "PermitNo", yourQuery["Permit No."]) (Though the latter option is less ideal because it may require copying the underlying data internally):

Expression Criteria for Access

I've created a database with about 15000 records and I want to create a form that filters the records according to entries inputed in textboxes through a Query. For the most of my columns i've used this Expression :
Like "*" & [Forms]![Testform]![Testtxt] & "*"
I use "Like" so that if the user decides not to input anything in a textbox, the query ignores that parameter. However when it comes to Dates (I have two columns that contain Dates) I can't make it ignore the empty textbox when the user decides not to write anything. Can you help me make the expression below show all the records in the query if the DateTesttxt is empty ?
> [Forms]![TestForm]![DateTesttxt]
If you're using query designer, just add another condition
Is Null
so visually in query builder it will be something like:
Criteria: > [Forms]![TestForm]![DateTesttxt]
or: Is Null
UPDATE:
This should work as well as a single expression:
Is Null Or > [Forms]![TestForm]![DateTesttxt]
and it is more correct solution in case if you have conditions on more than one column

Trying to join Access tables with like statement with list in field

I have a problem that I have been hunting for a solution to, but to avail.
The basics are that I am trying to join 2 tables in Access by comparing a value in a field of Table 1 to a field in Table 2 that contains the number concatenated along with a few others in a list type format. (both fields are text type)
Example.
Table1.CWT value = 640242
Corresponding Table2.TAG_NO value I want to match to = 640242; 635894; 058426
So that it links the two tables based on the common value (640242 in this case).
So far, I have tried the following:
LEFT JOIN [Table2] ON [Table1].CWT like '*' & [Table2].TAG_NO & '*'
and
LEFT JOIN [Table2] ON [Table1].CWT & '*' like [Table2].TAG_NO
and what seems like every variation in between, I have even tried using % instead of *. But nothing works. In some cases, the value will be the second or third element in the string (635894 in above example), so I am looking for an option that will work in all cases. This is akin to looking for the equivalent of the CONTAINS function, but that does not seem to exist either.
Can anyone help me out?
Thanks
Ted
You need to switch the operands. And make sure that '640242' doesn't match '6402423', so add delimiters to both strings:
' ' & Table2.TAG_NO & ';' like '* ' & Table1.CWT & ';*'
You can use the Instr Function that tests if a string exists in other string as below:
Select [Table1].CWT, [Table1].OtherColumn, [Table2].Column1Needed,[Table2].Column2Needed
From [Table1], [Table2]
Where Instr([Table2].TAG_NO,[Table1].CWT)>0
See http://www.techonthenet.com/access/functions/string/instr.php

How to Filter a row

I created a view for report. I imported data to the report. In columns null values are also present. If i filter one row using filter in vb.net the null values of row cannot be displayed.
For example, column names are ID, Name, Number, Place. In this some of the places number has null values I include filter ID, Name ,Number,Place. If I filter using ID the null values of number cannot be displayed.
This is the code I tried but not filter
IN FORM TextBox1.Text=""
TABLE1BindingSource.Filter = "YOUR FIELDNAME LIKE '" + TextBox1.Text.Equals (String.Empty) + "'")
Expected Result
Table
YOUR FIELDNAME
value1
value2
NULL
value3
NULL
NULL
value4
That filter doesn't make sense. This part:
TextBox1.Text.Equals(String.Empty)
is going to evaluate to Boolean, i.e. True if the TextBox is empty and False if it's not. That means that your filter ends up being:
YOUR FIELDNAME LIKE 'False'
or the like. What you should actually be doing is something like this:
Dim columnName As String
Dim fieldValue As String
'...
TABLE1BindingSource.Filter = String.Format("{0} LIKE '{1}%'", columnName, fieldValue)
Note a number of things there. Firstly, the use of String.Format to make this sort of code more readable. Secondly, the use of the actual value entered by the user and not a Boolean indicating whether that value was empty or not. Thirdly, the use of a wildcard because using LIKE without a wildcard makes no sense.
It should also be noted that that code is only going to work with text fields, because they are the only ones that get delimited by single quotes and the only ones that you can use LIKE with. If you want to filter on a numeric field or some other data type then you'll have to write your code to create the filter differently.