Link to home
Start Free TrialLog in
Avatar of PipMic
PipMicFlag for Gibraltar

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Hamed Nasr
Hamed Nasr
Flag of Oman 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
Or simply, in each check, add an Exit Sub after the MsgBox.
Avatar of PipMic

ASKER

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
Avatar of PipMic

ASKER

Applied idea and it worked. thanks