?
Solved

Save Button with conditions

Posted on 2012-08-24
4
Medium Priority
?
362 Views
Last Modified: 2012-08-24
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
0
Comment
Question by:PipMic
  • 2
4 Comments
 
LVL 31

Accepted Solution

by:
hnasr earned 450 total points
ID: 38328753
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
 
LVL 18

Expert Comment

by:Cluskitt
ID: 38328769
Or simply, in each check, add an Exit Sub after the MsgBox.
0
 

Author Comment

by:PipMic
ID: 38328798
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
 

Author Closing Comment

by:PipMic
ID: 38328801
Applied idea and it worked. thanks
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question