Comparing object.Value = Null does not produce expected results - vba

So I have a frustratingly simple issue that I cannot seem to solve.
If Me.Bank_Credit.Value = Null Then
Me.Bank_Credit.Value = 0
End If
Basically, I have an unbound box that the user enters data into and then hits a button. After a YES on confirmation box, the data on the unbound box is copied over to the bound box. However, if the user does not enter anything, that in turn creates an empty bound field which can seriously screw up queries down the road.
That being said, the above code simply will not work for me. If I set, for instance, If Me.Bank_Credit.Value = 1 and then run it, the 1s get turned into 2s, as should happen. But it simply refuses to work for Null or even "".
I'm so sure there is a simple solution to this issue, I just can't figure it out.
Thanks in advance

Nothing is ever equal to Null, not even another Null. And nothing is ever not equal to Null, not even another Null.
When Bank_Credit is Null, the following expression will return Null ... not True as you might expect, or even False.
Debug.Print (Me.Bank_Credit.Value = Null)
It's the same reason for this result in the Immediate window:
Debug.Print Null = Null
Null
Use the IsNull() function.
If IsNull(Me.Bank_Credit.Value) Then
Also, look at the Nz() help topic to see whether it can be useful. You could do this, although it's not really an improvement over IsNull(). But Nz() can be very convenient for other VBA code.
Me.Bank_Credit = Nz(Me.Bank_Credit, 0)

HansUp's answer is right, but I thought it relevant to add that there is a similar construct for "Nothing" which is basically a VBA keyword for a dereferenced object. You have to use statements like
If myRange is Nothing Then
You will see these sort of statements all over the VBA help files (and actually in other languages that have a keyword similar to this).

Related

DLookup returning True for some values, and False for others

I am writing a program, and I need to be able to check whether a certain 'tag' exists, by looking at all the 'tags' in the column 'CowTagMain' of the 'CowTable' table.
The code I am using is,
DLookup("[CowTagMain]", "[CowTable]", "[CowTagMain]") = Tag ...
Where tag is a String, TagMain is the column, and MainTable, is the table I am fetching data from.
I am getting inconsistent results, when I try this with 18C, it returns true. While if I try this with 18, it returns false.
I would assume I am fundamentally misunderstanding how to use DLookup, but after searching the internet for hours I cannot understand what I am doing wrong.
Even just pointing me in the right direction would be very appreciated! I am new to working with Access and VBA.
The search criteria is not within the function WHERE CONDITION argument.
The field is text type so need apostrophe delimiters.
Consider:
If IsNull(DLookup("[CowTagMain]", "[CowTable]", "[CowTagMain]= '" & Tag & "'")) Then

vba inputBox: Alternative Solutions on “cancel” and “OK” with an empty textbox

For some reason, the solution those two questions (Q1, Q2) give on how to differentiate between "cancel" and "ok with an empty string" for an inputbox don't always work within a specific code of mine. I wasn't able to reproduce the problem so far so I'm looking for a different solution.
In my scenario, the inputbox pops up with a default value it got from somewhere else. So far I only needed to change this value through the inputbox so the simple check for if len(inputboxString) = 0 then to determine if cancel was pressed was working well. Now I got the case where I need to "clear" the existing default value. I'm trying to avoid using StrPtr for the reason I described above.
Here's the code that worked so far (as long as I don't need to use the inputbox to delete the value, since 'strInputBox = ""' is reserved for the cancel-event and I want to avoid using 'strPtr').
Dim strSomeStringFromSomewhere as String
strSomeStringFromSomewhere = someSubThatGetsTheStringValueFromSomeWhere
Dim strInputBox As String
strInputBox = InputBox(Prompt:="Edit/Delete Value", Title:="Title", Default:=strSomeStringFromSomewhere)
If strInputBox = "" then
'do nothing cause cancel was pressed
else
someSubThatWritesTheChangedStringBackToItsOrigin strInputBox
End If
The used value is usually some alphanumeric string e.g. d939d8ej3.
Any hints/ideas for a different user friendly approach would be welcome. (user meaning the person using the inputbox not the person who implements/maintains the code).
I figured out a nice work around, which should work in many other cases as well. In my case a single or multiple spaces are not a valid entries. And since I have to get rid of pre- and succeeding spaces anyway I just use trim(strInputBox) which will "clear" my value.
So the user just has to enter a space into the inputbox, which is good enough for prototypes.

Excel VBA: Userform Checkbox - If Statement syntax differences

I'm working with some checkboxes on a userform, and am checking if they have been checked by the user in my code. The question I'm running into is in how to properly check for this condition. I've found several examples, and am wondering where I may run into trouble with each.
For example, I have seen several snippets use:
If checkBox1.Value = "True" Then
or
If checkBox1.Value Then
or
If checkBox1 Then
Is there any functional difference between these? It seems like the latter is the shortest, most succinct application, however the engineer's brain in me is saying there must be a reason for the other two cases (although the more I work with VBA, the less I believe in that concept).
Any assistance or guidance you could provide would be appreciated.
I would recommend the second one. It is always best to specify the property of an object explicitly rather than relying on defaults. There is no point in comparing a Boolean to True/False, since it already is True or False, and there is less point comparing it to a String value (which would be different in different regions).
In this case they are all the same and can be used interchangeably. Yet, I would only use the first or the second option.
checkBox1 is essentially incomplete and VBA automatically assumes (for you) that you are referring to its value and hence "completes" (during run-time) your code to checkBox1.Value. This takes time and may essentially slow down your code (even if this is barely noticeable).
To most programmers the second option is the preferred option because the if statement evaluates if something is True or False. Since the .Value of the checkbox is already True or False the first option is unnecessarily long. The value of checkbox1 is alredy True. So, why would you compare it to True?
At the same time I always prefer to use the first option as it makes the code easier to read for me (personal preference).
So, I'd say option 1 or 2: it's your choice.

How to ignore blank criteria in queries in Access

I'm currently working with an update query, that works as expected until I add in criteria that makes the query not display any results (which I expected). The criteria is currently coming from textboxes on a form.
What I want to be able to do is, in the criteria line, specify that if the the textbox is blank with nothing in it, then the criteria should just skip that.
I've tried in the Criteria line:
[Forms]![Formname].[txtboxName] OR [Forms]![Formname].[txtboxName] Is Null
but that doesnt work.
Thank you for any help or guidance!
You should be able to use a wildcard:
Like [Forms]![Formname].[txtboxName] & "*"
How about:
where [whatever your field is] = [Forms]![Formname].[txtboxName]
OR Nz([Forms]![Formname].[txtboxName]) = ""
The use of Nz will catch both null values and zero length strings, which look null but are not.
If that doesn't work, please do as Remou requested. I.E. Update your question with the actual SQL query instead of just part of it.
try this:
Like IIF(IsNull([Forms]![Formname].[txtboxName])=Fasle;[Forms]![Formname].[txtboxName];"*")
*Note: my system default separator is ";", make sure what yours.
Enjoy the Ride

How to fit VERY long condition in conditional formatting

I have a very long condition, about 3,000 characters. Access has only space for about one tenth of that.
Is there some other way to set conditional formatting on a textbox besides through the dialogue, or can I do it in VBA and, if so, HOW?
I have conditional formatting on a bunch of textboxes that trigger when the report is opened.
Would be curious to know if the VBA approach worked?
Another idea: create a hidden field on the form and set it's value based on your 3000-character condition. If you are just trying to have 1 conditional expression, you could give this hidden field a false/true or 0/1 value; if you want mulitple conditions, you could give it a value of 0, 1, 2, or 3 corresponding to the conditions you want applied. In either case, your conditional expression test(s) is(are) now trivial: [HiddenFieldName]=ConditionValue
According to Access' Help topic, the FormatConditions Collection has methods (Add, Delete, Modify) which should allow you to adjust your FormatConditions with VBA code. I've never tried, so offer no opinion as to whether this would be a practical approach for you.
I also tried to find out if there is a capacity limit for the number of characters FormatConditions can accept. I didn't find anything there.
Yes, you can manipulate Format Conditions in VBA. There's a full detailed article knowledgebase article at http://support.microsoft.com/kb/304104.
Code snippet here showing a basic example. Refer to the above link to get the VBA for AddFormats:
Public Function HighLightForeignKeys(argFieldName As String, argFieldValue As Integer)
Dim FormatCondition As String
Dim CodeReception As Integer
FormatCondition = "[" & argFieldName & "] = " & ArgFieldValue
With Me.ID
.FormatConditions.Delete
.FormatConditions.Add acExpression, , FormatCondition
.FormatConditions(0).BackColor = 16510422
AddFormats Me.ID, Me
End With
End Function
I dont know if this is what you were after, since you mentioned there was only a tiny box to enter all of your conditional text. Others have shown you VBA solutions - since you asked if that was one way to achieve it.
I have always resorted to using the "zoom" feature which is accessible via the SHIFT-F2 keystroke. Is that what you were after? I think it goes back several Access versions too.
A good set of Access shortcut keystrokes is here:
http://www.joyedaniels.com/keys_access.htm