MS Access query check if form field is empty - ms-access

I have a multi-field form and use it to retrieve records of a table. If there is no user input in a field I want to allow "*" and Null Values for the according column, the user input otherwise.
I tried
Like IIf([Forms]![frm_searchForm]![Titel]="" Or IsNull([Forms]![frm_searchForm]![Titel]);Like "*" Or Is Null;[Forms]![frm_searchForm]![Titel])
as well as
Like IIf(IsEmpty([Forms]![frm_searchForm]![Titel]);Like "*" Or Is Null;[Forms]![frm_searchForm]![Titel])
But in both cases, the IIf function always triggers the else expression.
How can I check whether the field in the form is empty? Is this even possible without VB?
Thanks

AFAICT, when [Forms]![frm_searchForm]![Titel] contains a value, you want only those rows where some field contains that value.
But, if [Forms]![frm_searchForm]![Titel] is Null or an empty string, you want to retrieve all the rows.
If that is correct, try a WHERE clause similar to this:
WHERE
Len([Forms]![frm_searchForm]![Titel] & '') = 0
OR some_field Like '*' & [Forms]![frm_searchForm]![Titel] & '*'
Also add a PARAMETERS clause at the beginning of your SQL statement:
PARAMETERS [Forms]![frm_searchForm]![Titel] Text ( 255 );

Related

Linking a query criteria to a value in a local table

I have a 5 column table with thousands of rows that I need to be able to filter. In the form where the table is displayed, Form1, I have a text field, Value1, where the table will only display the rows where Column 0 = Value1. If Value1 is blank, all rows will be displayed.
The code below is what is being currently used in the criteria and or field of my query builder and works great:
Like [Forms]![Form1]![Value1] & "*"
IsNull([Forms]![Form1]![Value1])
My goal is to have the criteria grab the value from a table, rather than a form. I created a form that pops up before Form1 that allows you to insert Value1 then hit Search. The value then goes into [Table1]![Value1] then opens Form1 and displays the filtered results. I am successfully inputting Value1 into Table1 but can't seems to get the query to react to [Table1]![Vale1]. I have tried a few different codes, the code below is an example of one:
Like [Tables]![Table1]![Value1] & "*"
IsNull([Tables]![Table1]![Value1])
Any suggestions?
You could use the DLookup function to get the value from the table, like this:
Dlookup("Value1","Table1")
This will work fine if you have only one value in the table. If you have multiple values in the table and need to have a specific value, you need to add a filter in the third parameter of the Dlookup function.
Dlookup("Value1","Table1","TableFieldName=" & [Value])
or, if the criteria value is a string
Dlookup("Value1","Table1","TableFieldName='" & [Value] & "'")
or, if the criteria value is a date
Dlookup("Value1","Table1","TableFieldName=#" & [Value] & "#")
See the docs also at : https://learn.microsoft.com/en-us/office/vba/api/access.application.dlookup

IIF query expression using Like "*" for the True condition

I have a database which tracks employee QA. I'd like to be able to search by a single Staff Member, a whole team, or a Unit. I have three controls that correspond to those fields and only one can ever have a value at once. In my quesry I'd like to have threee expressions that will limit my results by one of those three fields. I'm adding just one to start and I've hit a problem.
I found this https://www.acuitytraining.co.uk/microsoft-training-courses/access/if-statements/ which seems to do what I want. Here is the code I'm trying.
IIf(IsNull([Forms]![MainMenu]![btnManagersMenu].[Form]![cmbStaffSelect]),
[UserLogin] Like "*",[UserLogin]=[Forms]![MainMenu]![btnManagersMenu].
[Form]![cmbStaffSelect])
Which works fine if the control has a value. (condition is false) If the dropdown has no value (condition is true) I get zero results. I suspect the problem lies with the Like "*" on my UserLogin field. Here is my query wizard and the buildler wizard for the IIF expression
Can anyone see why I'm not getting any results for the dropdown control being empty. To my thinking this should give me an unfiltered list of results. I have double checked my data and there are 137 records that should appear if I'm not limited by the staff selection.
The short version of this is if cmbStaffSelect has a value I want my records limited by that value. If cmbStaffSelect is blank I want to get all records.
Keep in mind that the iif function will always evaluate both the then and else arguments, before returning the appropriate value depending on the value returned when evaluating the supplied test expression.
As such, if either the then or else arguments have the potential to error when evaluated (regardless of the result of the evaluation of the test expression), then the iif expression has the potential to error.
As an alternative, you could use the Nz function to achieve the same result:
[UserLogin] LIKE Nz([Forms]![MainMenu]![btnManagersMenu].[Form]![cmbStaffSelect],"*")
Perhaps your IsNull([Forms]![MainMenu]![btnManagersMenu].[Form]![cmbStaffSelect]) is always returning false because cmbStaffSelect might be equal to empty string?
Try something like this:
IIf(Trim([Forms]![MainMenu]![btnManagersMenu].[Form]![cmbStaffSelect] & "") = "",
[UserLogin] Like "*",[UserLogin]=[Forms]![MainMenu]![btnManagersMenu].
[Form]![cmbStaffSelect])
This checks to see if the cmbStaffSelect is "" ... if cmbStaffSelect is null - it converts it to "" by appending an "" to the null value.
I believe your hunch is exactly correct. If you want your query result to return the * symbol for the UserLogin field; then alter your IIF statement to be: [UserLogin] = "*"

MS Access, Use Expression Builder to Compare Field in One Table to DLookup in Another Table

I'm trying to make a MS Access report, where I use a text box to display a field value, then have another text box indicating if the first value is higher or lower than an entry in a separate table.
The report has a record source of "Table 1", and a textbox named "txt_Value1" which displays the number in Field: "Value1". I have a second table, "Customer_Criteria" which has a field "PassValue" that I want to compare against. My expression builder statement is:
IIf([txt_Value1]<(DLookUp("[PassValue]","[Customer_Criteria]","[Customer] = 'ABC'")),"TRUE","FALSE")
This statement always returns false, regardless of what the correct logical result is.
I've tested it, writing:
IIf(1<(DLookUp("[PassValue]","[Customer_Criteria]","[Customer] = 'ABC'")),"TRUE","FALSE")
And I get the correct results. Also, if I write:
IIf([txt_Value1]< 1,"TRUE","FALSE")
I get the correct results. What am I missing to compare the textbox value vs. the Dlookup?
As I understand, both fields are numeric. Access may consider those fields as text, so for correct comparing use type conversion.
Try this:
IIf(CLng(Nz([txt_Value1],0))< _
CLng(Nz(DLookUp("[PassValue]","[Customer_Criteria]","[Customer] = 'ABC'"),0)), _
"TRUE","FALSE")
Nz required if fields may contain NULL values, in this case type conversion function will return error.

SQL insert of '' is neither null or empty in Access

I have a function that essentially copies records and inserts them with new IDs. The code works and everything copies fine.
The SQL that occurs is essentially:
INSERT INTO myTable (ID, field, select) VALUES ('newID','stuff','')
Notice the last value is blank,empty,null, whatever. Sometimes it has something in it, sometimes it doesn't.
I have a check against that value in an access form and it uses isNull or isEmpty and both come up false, with only records that are inserted in this manner.
So what is being inserted, if nothing is being inserted? it's neither null nor empty and there is nothing in there, so I'm completely baffled. If I enter something and wipe it out, it'll be fine.
The IsEmpty() Function is generally only of use to check to see if a variable of type variant has not been initialised.
A database field can be said to be empty which is not the same as if it was Null.
For example if you have a field called Gender which was not a required field on a new record the value could be Null, then if a user typed in Female obviously it would a contain value. If they were then to delete the word Female then the field would now be empty (Not Null).
The most useful function to use when dealing with Nulls in Microsoft Access is the following:
Nz()
This function has two arguments, the first one being the variable you wish to check and the second one being the value you wish to return if the variable is Null. For example the following will return an empty string:
Dim variantName As Variant
variantName = Null
Debug.Print Nz(variantName,"")
Also please note if you try to join a variable that contains Null with a String using the plus character (+) then this will result in Null which is probably where you're having problems.
A try typing the following into the immediate window results are also shown here:
? len(null)
Null
? len(null + "abc")
Null
? len(null & "abc")
3
could be a \r or \n or any other non printable character,
you may want to select such a row and cast it as varbinary to see the content

SSRS expression with missing column not working

I'm trying to create an expression with a missing column. as in the column is not returned from the query but does exist in the list of fields.
The problem I'm having is that every time I do an expression and one of the parameters is from a field that doesn't have a return from query the query fails silently. the following example is looking at a field "test" that, as I said, exists in the list of fields but is not returned from the query, how can I have this is statement send "alert"??
=IIF(Fields!test.IsMissing,"alert",Fields!test.Value)
The reason that I don't return the field is that the columns are dependent on the parameters I enter in the procedure (so they could be used or not depending on what the user is asking)
Thank you
From reading your question I think, you have dynamic columns which will be returns conditionally
So what you should do is,
1) Create the parameter in the parameter list and set it as the internal and assign the field value to that parameter, suppose the parameter is dyanmicfiledvalue
2) Change your expression as,
=IIF(IsNothing(Parameters!dyanmicfiledvalue.value),"alert", Parameters!dyanmicfiledvalue.value )
that should do it. Let me know in case of issues.
Or if you want to check null or empty value for that column just change as
=IIF(IsNothing(Fields!test.value),"alert",Fields!test.value)