Save Button with conditions

Hi all,

My problems is as follows:    (using A97 and A03)

I have a form for inputting data and a Save button created using the wizard which saves the data. However I have added additional code to the the Event procedure which I want carriedout prior to the save:


Private Sub Command181_Click()

Dim total As Integer
total = ([share1] + [share2] + [share3] + [share4])

If Text118 = "" Then
MsgBox ("Please enter Reference")
End If

If total < 99.5 Or total > 100 Then
MsgBox ("Share total is not equal to 100")

ElseIf total >= 99.5 And total <= 100 Then
MsgBox ("Share total has been accepted as 100")

End If

On Error GoTo Err_Command181_Click

    DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Exit_Command181_Click:
    Exit Sub

Err_Command181_Click:
    MsgBox err.Description
    Resume Exit_Command181_Click
   
End Sub

Everything functions ok. However the problem that i have  is that even though the conditions work, the data is saved irrespective.

I would like that when the condition fails i.e. the share total is not equal to a hundred that the data is not saved until the condition is met i.e. that the total share equals a hundred.

Grateful for assistance or ideas or similar code or a similar sample I could try.

thanks
PipMicAsked:
Who is Participating?
 
hnasrConnect With a Mentor Commented:
Add the code in the Before Update event, so you may cancel the event if condition fails.

Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Condition Then
        Cancel = True
    End If
End Sub

Open in new window

0
 
CluskittCommented:
Or simply, in each check, add an Exit Sub after the MsgBox.
0
 
PipMicAuthor Commented:
Hi hnasr,

Used your idea and did the following:

Dim total As Integer
total = ([share1] + [share2] + [share3] + [share4])

If Text118 = "" Then
MsgBox ("Please enter Tax Reference")
End If

If total < 99.5 Or total > 100 Then
MsgBox ("Share total is not equal to 100")

ElseIf total >= 99.5 And total <= 100 Then
MsgBox ("Share total has been accepted as 100")


Cancel = True
End If

It worked ...simple....many thanks
0
 
PipMicAuthor Commented:
Applied idea and it worked. thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.