Link to home
Start Free TrialLog in
Avatar of Sacha Walter
Sacha WalterFlag for Canada

asked on

Update a field based on completion of other fields using VBA in Access

Hi Experts,

I am attempting to update a field such that the field Completed reads 'Yes' when 3 date fields are populated and 'No' when any one of the 3 date fields are not populated.  I read that you can't test for Null values unless you convert the variable to a variant so that's what I've tried to do below.

 The code below returns 'No' even when the 3 date fields are populated, what am I doing wrong?

Thanks!

Private Sub Form_AfterUpdate()
    Dim vconvert As Variant
    Dim vconvert2 As Variant
    Dim vconvert3 As Variant
   
    Dim BValue As Boolean
    Dim BValue2 As Boolean
    Dim BValue3 As Boolean
   
    Dim sSQL As String
    Dim iID As Integer
   
    iID = Me.ID.Value
   
    vconvert = CVar(Me.DATE_AUTHORIZED)
    vconvert2 = CVar(Me.DATE_COMPLETED_FOR_RELEASE)
    vconvert3 = CVar(Me.DATE_RELEASED)
       
    BValue = IsNull("vconvert")
    BValue2 = IsNull("vconvert2")
    BValue3 = IsNull("vconvert3")
       
    If BValue And BValue2 And BValue3 = False Then
   
        sSQL = "Update Main " & _
               "SET [Completed]= 'Yes' " & _
               "WHERE Main.[ID] = " & iID & ";"
        DoCmd.SetWarnings False
        DoCmd.RunSQL (sSQL)
       
        Else
       
        sSQL = "Update Main " & _
               "SET [Completed]= 'No' " & _
               "WHERE Main.[ID] = " & iID & ";"
        DoCmd.SetWarnings False
        DoCmd.RunSQL (sSQL)
       
    End If
End Sub
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

why not just do it using a query..

Update Main
Set [Completed]=IIf([DATE_AUTHORIZED] & ""<>""  and [DATE_COMPLETED_FOR_RELEASE] & ""<>"" and [DATE_RELEASED] & ""<>"","Yes","No")


.
I agree with above.
Just time highlight the issue in your code:
= IsNull("vconvert")
Should be:
= IsNull(vconvert)
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial