I have input from Form Name,Branch (text) x,y and Total are (number) fields
- Record needs to be saved when user provides all the input fields
- Total field = sum (x + Y)
I tried the below code during testing …
Problem:
I provided the name field data but Branch was provided null and tried to save.. ( Below logic sending message “A value is required for Branch …”
when clicked on ok still logic is validating the validatenum function and sending message if total is not equal…
But I need form to stop when first validation failed itself it show branch data missing message and control should go to branch filed and other logic should not get executed..
I think I am doing something wrong with the logic .. can some help me with this simple logic…
I also given outline VB logic here.
Code:
---------
Option Compare Database
Option Explicit
Dim cancel As Boolean
---------------------------
Public sub save_click()
call validateinput
call validatenum
DoCmd.RunCommand acSaveRecord
End sub
----------------------------
Private Sub validatenum()
If Me.Total.Value = Me.X.Value + Me.Y.Value then
Else
MsgBox " Total s not matched with x + Y feilds"", vbRetryCancel
End If
End sub
----------------------------
Private Sub validateinput()
If Me.Name.ListIndex = -1 Then
MsgBox "A value is required for Namefeild ..."
Me.Name.SetFocus
cancel = True
End If
If Me.Branch.ListIndex = -1 Then
MsgBox "A value is required for Branch ..."
Me.Branch.SetFocus
cancel = True
End If
End sub
Just add line with text “Exit Sub” before “End If” for each validation
PS: Read your code once again and noticed that “cancel = True” actually does nothing, so I would rewrite it something like this: