Link to home
Start Free TrialLog in
Avatar of gsilouisvilleic
gsilouisvilleicFlag for United States of America

asked on

Access VBA If Statements

I have a database where information is stored in a table.  There is a column names "Errors".  If there is at least one value in one of the error fields I want to run a macro, otherwise, if the Errors column is null, I want it to run another macro.  I've got the following VBA but it's not functioning correctly because I can't figure out how to add in if there is at least one record, run the macro, if there are no records run the other.  Any help would be greatly appreciated.

Private Sub C_Save_Tote_Audit_Info_Click()

If DCount(1, "T_Preview Tote Audit", "IsNull([Errors])") Then
DoCmd.RunMacro "M_Append Tote Information to Master and Reset Good Records"

Else:
DoCmd.RunMacro "M_Append Tote Information to Master and Reset"

End If

End Sub

Thanks,
Cassie
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

How about something like:

IF DSUM("NZ([Errors],0)", "T_Preview Tote Audit") = 0 Then
    'do something, no errors
Else
    'do something with errors
End IF

Open in new window

SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore 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
ASKER CERTIFIED SOLUTION
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
The two answers you selected produce different results.  Some people like to award participation points.  This is not a practice of which I approve but the choice is yours.  In all fairness though, when you choose conflicting answers it would be better for you to at least specify which one actually worked.

Ryan's suggestion produces True when every row in the recordset contains a non-null value.

My suggestion produces True when at least one row in the recordset contains a non-null value

Dale's suggestion gives results similar to mine but only if Errors is numeric.  If Errors is text then you can't sum a text field.

Based on your statement -
If there is at least one value in one of the error fields
which solution is correct?
Avatar of gsilouisvilleic

ASKER

Pat - Your solution was the one that I selected, I did award points for assistance, but I understand where you are coming from.  Thanks for your help.

Thanks,
Cassie