Home > Enterprise >  VBA if statement does not trigger empty string VBA Access
VBA if statement does not trigger empty string VBA Access

Time:09-28

im trying to delete the content of an input field when the value of a related one is deleted.

However the if statement does not trigger when the value is Null ... (empty string)

I tested it with the "Hello" message box and it does not appear when the textbox is cleared, how it should.

Private Sub Student_AfterUpdate()


If Me.Student.Value = "" Then

MsgBox "Hello"

    Me.InputBeginn = ""
    Me.InputEnd = ""
    
    End If



End Sub

CodePudding user response:

In databases, Null means "no value" and this is something different than an empty string.

To check for Null, you can use the function IsNull. If your value can be either Null or an empty string, you can use the Nz-function: Nz(anyValue, "") will return an empty string if anyValue is null, else it will return anyValue itself:

If Nz(anyValue, "") = "" Then

Note that this is no SQL standard, for Oracle users, its NVL, for MS SQL it's IfNull)

CodePudding user response:

In Access, an empty textbox is Null even when bound, as you normally should not allow empty strings in the table field. Thus:

Private Sub Student_AfterUpdate()

    If IsNull(Me!Student.Value) Then
        MsgBox "Hello"

        ' Reset textboxes.
        Me!InputBeginn.Value = Null
        Me!InputEnd.Value = Null
    End If

End Sub
  • Related