locked
VBA Conditional Statement and the Object Required Error RRS feed

  • Question

  • Hi I'm trying to run a vba statement like this

    Private Sub cmdBatchUpdate_Click()
    If Me.txt3fieldA Is Not Null Then
    CurrentDb.Execute (FieldAUpdateQuery)
    ElseIf Me.txt3FieldB Is Not Null Then
    CurrentDb.Execute (FieldBUpdateQuery)
    ElseIf Me.txt3FieldC Is Not Null Then
    CurrentDb.Execute (FieldCUpdateQuery)
    ElseIf Me.txt3FieldD Is Not Null Then
    CurrentDb.Execute (FieldDUpdateQuery)
    ElseIf Me.txt3FieldE Is Not Null Then
    CurrentDb.Execute (FieldEUpdateQuery)
    ElseIf Me.txt3FieldF Is Not Null Then
    CurrentDb.Execute (FieldFUpdateQuery)
    Else
    End If

    End Sub

    and I'm getting an object required error.  I believe the It's expecting something like fieldA = X or some other object like that.  any ideas?

    Thanks for your help

    Friday, July 5, 2013 10:02 PM

Answers

  • Hi I'm trying to run a vba statement like this

    Private Sub cmdBatchUpdate_Click()
    If Me.txt3fieldA Is Not Null Then
    CurrentDb.Execute (FieldAUpdateQuery)
    ElseIf Me.txt3FieldB Is Not Null Then
    CurrentDb.Execute (FieldBUpdateQuery)
    ElseIf Me.txt3FieldC Is Not Null Then
    CurrentDb.Execute (FieldCUpdateQuery)
    ElseIf Me.txt3FieldD Is Not Null Then
    CurrentDb.Execute (FieldDUpdateQuery)
    ElseIf Me.txt3FieldE Is Not Null Then
    CurrentDb.Execute (FieldEUpdateQuery)
    ElseIf Me.txt3FieldF Is Not Null Then
    CurrentDb.Execute (FieldFUpdateQuery)
    Else
    End If

    End Sub

    and I'm getting an object required error.  I believe the It's expecting something like fieldA = X or some other object like that.  any ideas?

    You can't use "Is Not Null" in VBA code. You have to use the IsNull() function instead, like this:

    If Not IsNull(Me.txt3fieldA) Then
        CurrentDb.Execute (FieldAUpdateQuery)
    ElseIf Not IsNull(Me.txt3FieldB) Then
        CurrentDb.Execute (FieldBUpdateQuery)
    '...


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Friday, July 5, 2013 10:12 PM

All replies

  • If you debug it...which line are you getting the error?

    What office suite are you using?

    Friday, July 5, 2013 10:08 PM
  • Hi I'm trying to run a vba statement like this

    Private Sub cmdBatchUpdate_Click()
    If Me.txt3fieldA Is Not Null Then
    CurrentDb.Execute (FieldAUpdateQuery)
    ElseIf Me.txt3FieldB Is Not Null Then
    CurrentDb.Execute (FieldBUpdateQuery)
    ElseIf Me.txt3FieldC Is Not Null Then
    CurrentDb.Execute (FieldCUpdateQuery)
    ElseIf Me.txt3FieldD Is Not Null Then
    CurrentDb.Execute (FieldDUpdateQuery)
    ElseIf Me.txt3FieldE Is Not Null Then
    CurrentDb.Execute (FieldEUpdateQuery)
    ElseIf Me.txt3FieldF Is Not Null Then
    CurrentDb.Execute (FieldFUpdateQuery)
    Else
    End If

    End Sub

    and I'm getting an object required error.  I believe the It's expecting something like fieldA = X or some other object like that.  any ideas?

    You can't use "Is Not Null" in VBA code. You have to use the IsNull() function instead, like this:

    If Not IsNull(Me.txt3fieldA) Then
        CurrentDb.Execute (FieldAUpdateQuery)
    ElseIf Not IsNull(Me.txt3FieldB) Then
        CurrentDb.Execute (FieldBUpdateQuery)
    '...


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Friday, July 5, 2013 10:12 PM