I have a combobox that I use for filtering entries in the column below. The code reads the entries of the column and adds what it finds into the combobox (SELECT DISTINCT).
This works fine if the columns don't have entries that are NULL. If they do, a blank field will show in the combobox (which is good, I want to be able to filter for empty entries). However, if I click on this field in the combobox, all entries are shown, not just the NULL ones. Does anyone know why this could be? Does Access convert NULL to an empty string? If so, can I circumvent this somehow?
You are looking for the NZ function
Syntax
Nz ( variant [, valueifnull ] )
The Nz function syntax has these arguments:
variant
Required. A variable of data type Variant.
valueifnull
Optional (unless used in a query). A Variant that supplies a value to be returned if the variant argument is Null. This argument enables you to return a value other than zero or a zero-length string.
Note: If you use the Nz function in an expression in a query without using the valueifnull argument, the results will be a zero-length string in the fields that contain null values.
MS DOC:
https://support.office.com/en-ie/article/nz-function-8ef85549-cc9c-438b-860a-7fd9f4c69b6c
Related
As the subject expresses, I'm trying to sum the values of a string field where spaces may exist. It must be done this way, unfortunately.
The database is very old. The original developer chose to make all fields Text fields; to get over the null value problems, a function was written in VB6 to replace any null value with a space. This cannot be changed.
Fast forward to now, I'm trying to create a report that sums the length field without changing spaces to nulls first, and it should be done entirely through the control source property of the report.
I've added some of what I've tried below, but every time the report is run, I receive:
Data Type Mismatch
...and I'm not sure how to get around it.
Ideally, I'd like to keep the users out of the database completely, and just add a combo box that lists the reports created in the database so they can be opened by name without having to run any additional update queries first.
=Sum(IIf([MY_LEN]<>" ",DCount("[MY_LEN]","MY_TABLE"),0))
=Sum(Nz(Iif(Trim([MY_LEN])='',Null,[MY_LEN]),0))
=DSum("[MY_LEN]","[MY_TABLE]","[MY_LEN]<>' '")
=Sum(Iif(Val([MY_LEN])>0,[MY_LEN],0))
=(SELECT Sum([MY_LEN]) AS MyLen FROM MY_TABLE WHERE (((MY_TABLE.[MY_LEN])<>' ')))
Is this possible?
Can't compare anything to Null. Can't say If x = Null Then because Null is undefined. So you can't test if undefined = undefined. Use If IsNull(x) Then in VBA and Is Null in query criteria. Don't really need IIf() for Sum() aggregate, other aggregates such as Count or Avg would.
To handle possible space, empty string, or Null for a text field holding numeric data.
=Sum(Val([MY_LEN] & ""))
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] = "*"
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.
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
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)