Simple scenario: a form and one text box (unbound), Text1.
If "" <> Text1 Then
MsgBox "Not Empty"
End If
The above code works. The expression ""<> Text1 evaluates to True if the text box contain characters.
The opposite doesn't work, regardless of the text box is empty or not:
If "" = Text1 Then ' or alternatively, False = ("" <> Text1)
MsgBox "Empty!"
End If
Can you clarify this issue?
The textbox is usually null if it does not contain anything, this is not the same as a zero length string (""). It can often be best to use:
If Trim(Text1 & "") = vbNullString
'Empty
This will be true if the textbox contains spaces, a zero length string or null.
Alternatively, I've got a small function that I find useful for checking that sort of thing when dealing with various variable types and I just want to know whether it's blank.
'-----------------------------------------------------------------------------'
' True if the argument is Nothing, Null, Empty, Missing or an empty string. '
'-----------------------------------------------------------------------------'
Public Function IsBlank(arg As Variant) As Boolean
Select Case VarType(arg)
Case vbEmpty
IsBlank = True
Case vbNull
IsBlank = True
Case vbString
IsBlank = (arg = vbNullString)
Case vbObject
IsBlank = (arg Is Nothing)
Case Else
IsBlank = IsMissing(arg)
End Select
End Function
Just Made a blog post today about it too.
I always use the function Nz (variable, valueIfNull) to check those kind of things. For example:
if (Len(Nz(Me.txt1, "")) > 0) then
'Me.txt1 does not contain a value
end if
Or if I want to save a textbox's value to a recordset:
rst!Name = Nz(Me.txtName, "No name given")
Related
I have a text box called [Status] that a user can populate using a combo box.
I want to filter my form based on two potential values in the text box value - Pipeline or Forecast. The filter is activated by a checkbox. Click check box get records where the [Status] is either Pipeline or Forecast.
The checkbox code is easy enough. I am having trouble getting Access to filter on two possible values in the same text box.
I've tried
Private Sub checkboxFilterActive_AfterUpdate()
If checkboxFilterActive = True Then
Me.Filter = "[status]='Pipeline'" And "[status]='forecast'" 'Filter Code
Me.FilterOn = True
Else
Me.FilterOn = False
End If
End Sub
This throws a type mismatch error.
I've tried
Private Sub checkboxFilterActive_AfterUpdate()
Dim strFilter As String
strFilter = "[Status]='Forecast'" & "'AND [Status] = 'Pipeline'"
If checkboxFilterActive = True Then
DoCmd.ApplyFilter , strFilter
Else
DoCmd.ShowAllRecords
End If
End Sub
This throws a Syntax error (missing operator) query expression error.
Any help is much appreciated.
Use OR instead of AND. Also, OR operator is literal text and needs to be within quote marks to build criteria string.
Me.Filter = "[status]='Pipeline' OR [status]='forecast'" 'Filter Code
Having a bit of trouble. I have a simple situation: 2 Radio buttons, Accept or Reject. Instead of doing IF statements I feel an obligation to do Select statements.
As I walk through my code, it gets to the Case rdbApprove.Value and complains that the value is null. I figure it isn't true, thus it should pass to the next Case. Nope - it hangs on the fact that it is null.
In an effort to try and get rid of the null value I tried the NZ(rdbApprove.Value, False) to try and tell it that it was false but that still isn't working.
Select Case True
Case rdbApprove.Value
Case rdbReject.Value
Case Else
MsgBox "ERROR!"
End Select
The usual way this is done is to include your radio buttons in an option group. Then your code can reference the option group .Value.
My form includes an option group named Frame15 which includes an "Approve" radio button whose Option Value is 1, and a "Reject" button whose Option Value is 2.
I also added a command button to inspect the option group .Value. Here is its click event procedure.
Private Sub cmdOptionValue_Click()
Dim strPrompt As String
Select Case Me.Frame15.value
Case 1
strPrompt = "approved"
Case 2
strPrompt = "rejected"
Case Else
If IsNull(Me.Frame15.value) Then
strPrompt = "Null"
Else
strPrompt = "this should not happen"
End If
End Select
MsgBox strPrompt
End Sub
I am trying to create a new function that I can pass variables to and those variables will be used to select and modify items.
Function
Function TabDisplay(Switch As String, TargetTab As String)
If (Me. & Switch & .Value) = -1 Then
Me.Tabs.Pages.Item("TargetTab").Visible = True
ElseIf Me.Switch.Value = 0 Then
Me.Tabs.Pages.Item(TargetTab).Visible = False
Else
Me.Switch.Value = "0"
End If
End Function
How I call the function
TabDisplay "SoftwareInstallable", "tabSoftware"
Working Code that I used before attempting this function
If Me.SoftwareInstallable.Value = -1 Then
Me.Tabs.Pages.Item("tabSoftware").Visible = True
ElseIf Me.SoftwareInstallable.Value = 0 Then
Me.Tabs.Pages.Item("tabSoftware").Visible = False
Else
Me.SoftwareInstallable.Value = "0"
End If
====Final Code====
Function
Function TabDisplay(Switch As String, TargetTab As String)
If Me.Controls(Switch).Value = -1 Then
Me.Tabs.Pages.Item(TargetTab).Visible = True
ElseIf Me.Controls(Switch).Value = 0 Then
Me.Tabs.Pages.Item(TargetTab).Visible = False
Else
Me.Controls(Switch).Value = "0"
End If
End Function
Function Call
TabDisplay "SoftwareInstallable", "tabSoftware"
I think I understand what you want. But let me describe it with a different example.
I have a form with a text box named txtFoo. So I can Debug.Print its value to the Immediate window like this ...
Debug.Print Me!txtFoo.Value
Later I decide I want to store the control name in a variable so that I can Debug.Print the value of any other control simply by changing the variable.
Dim strControlName As String
strControlName = "txtBar"
Then I can use that variable to reference the matching item in the form's Controls collection, and obtain its value.
Debug.Print Me.Controls(strControlName).Value
I think you can use that approach to accomplish what you want, but I won't attempt to rewrite your code sample because I suspect I would get it wrong.
What started as a simple validation code converted in something very paradoxical in my eyes.
The following code returns "Good work!" when I input a negative number in the InputBox popup
Dim myvar As String
myvar = InputBox("input a positive number, please")
If IsNumeric(myvar) Then
myvar = CDbl(myvar)
Select Case myvar
Case Is < 0
MsgBox "I need a positive number"
Exit Sub
Case Is > 0
MsgBox "Good work!"
[MyField] = myvar
RunCommand acCmdSaveRecord
Me.Requery
Exit Sub
Case Else
MsgBox "You entered '" & myvars & "'. I don't know what to do about"
End Select
Else
MsgBox "A Number, please"
End If
Is this really the best way to validate an InputBox?
Since myvar is a String, CDbl(myvar) will get implicitly converted back to a string. Create a temporary numeric variable for the Select Case.
I agree with some of the other answers. Think about this code re-write (notice the second variable declared):
Dim myvar as String
Dim myDbl as Double
myvar = inputBox ("Input a positive number, please")
if isnumeric(myvar) then
myDbl = cDbl(myvar)
else
msgbox "Enter a number please"
exit sub
end if
if mydbl <=0 then
msgbox "I need a positive number"
else 'if mydbl > 0 then
MsgBox "Good work!"
[MyField] = myvar
RunCommand acCmdSaveRecord
Me.Requery
end if
That would solve you problems by accounting for zero and declaring a separate variable as a double there. The if statement instead of the case is just preference I suppose. But now think about the two variables you have.
debug.print "MyVar is a string containing: " & myvar
debug.print cstr(cdbl(myvar)*2)
'This forces myvar into a double(a number-type) to do math on it and then print it out
However, if you were to re-run the first print, you would see that myvar is still a string and can be used like a string. In VBA, unlike some other languages, variables are only what you declare them as. If you want to use them as other things, you have to declare another variable of the needed type.
TL;DR Think of them as different containers. Strings are circle boxes and doubles are square boxes. They might be able to hold similar stuff but the functionality of the containers is limited by their shape. In VBA you don't have a way to force a circle into a square so you have to make a whole second container and transfer the stuff over.
im trying to check if a number is a text or number in a banking system. If the value is incorrect it should erase and send an error message, but my code isnt working at all... can someone help me? here is the code:
Private Sub TextBox1_Change()
name = TextBox1
If Application.IsText(name) = True Then
IsText = True
Else
MsgBox "Insert only words"
Selection.name.Delete
End If
End Sub
I think you should be using name = TextBox1.Text instead. It's probably giving you an error on that line.
Also, you may want to wait until the user is finished typing the response, I believe the changed event will run each time a character is pressed. The AfterUpdate will run after you tab or click away from the textbox.
And really, instead of deleting the response (which I don't think will work), you should simply set the textbox blank back to an empty string.
Private Sub TextBox1_AfterUpdate()
If Not MyIsANumber(TextBox1.Text) Then
MsgBox ("It's Text")
Else
TextBox1.Text = ""
End If
End Sub
Function MyIsANumber(str As String) As Boolean
For i = 1 To Len(str)
If IsNumeric(Mid(str, i, 1)) Then
MyIsANumber = True
Exit Function
End If
Next
MyIsANumber = False
End Function
I'm sure the code could be structured better, but there's something along the lines of what you need. In this case, it just wipes the textbox if there is a number entered. Obviously, things will need to be the opposite for your textbox that you want to contain a number.
Have you considered the vartype() function? https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/vartype-function