473,386 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Validating the date field using VBA code

vs2k8
13
Hello guys,

New to this forum and new to access programing, my issue is
I am comparing 2 date fields, I have to validate that Order Rcvd Dt should be less then Ord Comp date and Order Comp date cannot be null.
So I written a function to validate the field which is shown below,
If I write before update on Order_comp_date field then I get run time error message : You must save the field before you execute setfocus method

Private Sub Order_Dt_Comp_BeforeUpdate(Cancel As Integer)
If ChkFld = False Then
Cancel = True
End If

End Sub

This is my function
------------------------
Private Function ChkFld()

Dim isValid as Boolean
If (IsNull(Ord_Comp_Dt.Value)) Then
MsgBox "Order Complete Dt cannot be null,Please enter date"
Ord_Comp_Dt.SetFocus
isValid = False
End If
If (Ord_Rcvd_Dt > Ord_Comp_Dt.Value) Then
MsgBox "Order Complete Dt should be greater then Order Recv Date!"
Ord_Comp_Dt.SetFocus
isValid = False
End If

ChkFld = isValid

End Function

So my question is how I can overcome this error and I want user to enter the correct value and can't be null and even if just entering date and hitting save button it should also fire at save button too.

Thanks in advance

-visa
Jun 30 '08 #1
8 13714
hjozinovic
167 100+
hi Visa!

I would recommend using Validation rule property, unless you MUST use VBA for this.

a) Validation rule solution:
You can put in Ord_Rcvd_Dt's validation rule property:
Ord_Rcvd_Dt < Ord_Comp_Dt AND Len(Ord_Comp_Dt)>0

:::::::In Validation text property put a message like: "Order Complete Dt should be greater then Order Recv Date!"

b) VBA solution:
On form's before update event you can put a code:
Expand|Select|Wrap|Line Numbers
  1. If Me.Ord_Rcvd_Dt >= Me.Ord_Comp_Dt OR Len(Me.Ord_Comp_Dt)=0 Then
  2. MsgBox "Order Complete Dt should be greater then Order Recv Date!"
  3. DoCmd.CancelEvent
Hope this helps,
H.
Jul 1 '08 #2
vs2k8
13
Thanks H. for reply, I used your method but it seems Validation rule property is not working properly in access 2003, if I entered wrong date or kept null and hit the tab key it didn't fire the event, it works if i enter the date and remove the date and hit tab it says it cannot be null but it doesn't fire for wrong date.It should stop user there unless it enters right date.

Secondly I entered the code at form before update event,
it worked when I hit the save button but after the message it gives MS message 'You canceled the previous operation'
, i guess its bcos of DoCmd.Cancelevent, so how i can stop popping this message.
I am tired that my validation is not working, there are few more validation this one is easiere one its not working properly.

Hope to get the solution.

Thanks,
-visa

hi Visa!

I would recommend using Validation rule property, unless you MUST use VBA for this.

a) Validation rule solution:
You can put in Ord_Rcvd_Dt's validation rule property:
Ord_Rcvd_Dt < Ord_Comp_Dt AND Len(Ord_Comp_Dt)>0

:::::::In Validation text property put a message like: "Order Complete Dt should be greater then Order Recv Date!"

b) VBA solution:
On form's before update event you can put a code:
Expand|Select|Wrap|Line Numbers
  1. If Me.Ord_Rcvd_Dt >= Me.Ord_Comp_Dt OR Len(Me.Ord_Comp_Dt)=0 Then
  2. MsgBox "Order Complete Dt should be greater then Order Recv Date!"
  3. DoCmd.CancelEvent
Hope this helps,
H.
Jul 1 '08 #3
hjozinovic
167 100+
Thanks H. for reply, I used your method but it seems Validation rule property is not working properly in access 2003, if I entered wrong date or kept null and hit the tab key it didn't fire the event, it works if i enter the date and remove the date and hit tab it says it cannot be null but it doesn't fire for wrong date.It should stop user there unless it enters right date.

Secondly I entered the code at form before update event,
it worked when I hit the save button but after the message it gives MS message 'You canceled the previous operation'
, i guess its bcos of DoCmd.Cancelevent, so how i can stop popping this message.
I am tired that my validation is not working, there are few more validation this one is easiere one its not working properly.

Hope to get the solution.

Thanks,
-visa
hey Visa,

i'm not sure why you get the message 'You canceled the previous operation'.
Obviously it's because of DoCmd.CancelEvent command but, I tried it myself and I got no message.

Attached here is my Test base. In MainForm's before update property you will find the code similar to the code you were using.

If bValue is less than 30 I get the message I asked.
After that I get no messages, only the record is still in editing mode as it should be, not allowing to save it before it meets the criterion set before.

Please try this and let us know the result.

h.
Attached Files
File Type: zip Test.zip (36.2 KB, 311 views)
Jul 1 '08 #4
vs2k8
13
hey Visa,

i'm not sure why you get the message 'You canceled the previous operation'.
Obviously it's because of DoCmd.CancelEvent command but, I tried it myself and I got no message.

Attached here is my Test base. In MainForm's before update property you will find the code similar to the code you were using.

If bValue is less than 30 I get the message I asked.
After that I get no messages, only the record is still in editing mode as it should be, not allowing to save it before it meets the criterion set before.

Please try this and let us know the result.

h.
Thanks H for ur reply, I tried ur script beforeupdate, its still not working, so I used validation rule what u suggested before, instead Ord_Rcvd_Dt < Ord_Comp_Dt AND Len(Ord_Comp_Dt)>0 , I just tweaked it to < Ord_Rcvd_Dt and is not null, it works very well.

Now I have another question if you can help I have this combo box ord status, open (default) & closed, if user changes to close it should check other combo box status should be yes and one field should not be null,
Following is scenario user changes ord status = closed
combo box InOrdDoc = Yes and
InOrdDoc2 = Yes and field Product cannot be null,
I entered in Validation rule of Ord Status combo box
ord status = closed and InOrdDoc = Yes and InOrdDoc2 = Yes And Product is Not Null
Validation text: InOrdDoc and InOrdDoc2 must be yes and Product cannot be null.
I am trying to change Ord Status to Closed it fires continously,even I select open and my cursor sticks there and don't come out of my Order status field. So what event I should try or how to write in Validation rule.

Thanks in advance

-visa
Jul 2 '08 #5
hjozinovic
167 100+
hey Visa,
If I understood you correctly then this is what you should do.
In form's before update event put a code:
Expand|Select|Wrap|Line Numbers
  1. If Me!OrdStatus = "Closed" Then
  2.     If InOrdDoc=No OR InOrdDoc2=No OR IsNull Product Then
  3.     MsgBox "InOrdDoc and InOrdDoc2 must be yes and Product cannot be null."
  4.     DoCmd.CancelEvent
  5.     End If
  6. End If
btw, I don't understand why the VBA solution I suggested before didn't work for you?

H.
Jul 3 '08 #6
vs2k8
13
hey Visa,
If I understood you correctly then this is what you should do.
In form's before update event put a code:
Expand|Select|Wrap|Line Numbers
  1. If Me!OrdStatus = "Closed" Then
  2.     If InOrdDoc=No OR InOrdDoc2=No OR IsNull Product Then
  3.     MsgBox "InOrdDoc and InOrdDoc2 must be yes and Product cannot be null."
  4.     DoCmd.CancelEvent
  5.     End If
  6. End If
btw, I don't understand why the VBA solution I suggested before didn't work for you?

H.
Thanks H. I don't know y its not working, do we have to change any properties of the field? I used ur code at form level beforeupdate, but nothing happened ,my cursor went to next field, it works only if I hit the save button, so I used the code at field level beforeupadte it works there if I hit tab it fires the trigger when criteria not matched, but issue is that it doesn't allow me to do any changes to be made in Indoc or Indoc2 or product field and continously it fires the message.So how I can overcome this pls let me know.
Below is the code
If Me!OrdStatus = "Closed" Then
If Me.InOrdDoc="No" OR Me.InOrdDoc2="No" OR IsNull(Me.Product) Then
MsgBox "InOrdDoc and InOrdDoc2 must be yes and Product cannot be null."
DoCmd.CancelEvent
End If
End If
Thanks again!

-visa
Jul 3 '08 #7
hjozinovic
167 100+
Hi again.
The situation you described happens because you entered the code on field level.
You should have this code to run on form's before update property!
that way you will be able to adjust the values in specified fields and finaly save the current record after the criteria is met.
On field level it fires again and again because you need to exit current field in order to change other fields.
But you can't exit because it keeps firing the message!

Why is it not enough for you to check the fields before saving the record?
You keep insisting on field level checking, but that is not always easy...

H.
Jul 4 '08 #8
vs2k8
13
Hi again.
The situation you described happens because you entered the code on field level.
You should have this code to run on form's before update property!
that way you will be able to adjust the values in specified fields and finaly save the current record after the criteria is met.
On field level it fires again and again because you need to exit current field in order to change other fields.
But you can't exit because it keeps firing the message!

Why is it not enough for you to check the fields before saving the record?
You keep insisting on field level checking, but that is not always easy...

H.
Hi H,
Thanks for ur reply,I got it what u r saying, if Before update works at form level then it wouldn't be problem, user says we want that after entering the wrong value it should fire the trigger so I am trying to meet their requirement, can you tell me how I should convince user saying that this will work only at form level,while saving your record. I am not that expert in access so I dont know how to explain them.
One more thing I found that when I try to save it gives me following message
"The DoMenuItem action was cancelled" and it undo everything, I made in existing record, how I can trap this message and user can save existing record.

Thanks again.

-visa
Jul 4 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: C | last post by:
Hi, I have two input fields on an ASP Page. User enters 2 dates. I want to check that the difference in months does not exceed 360 months. Is this possible in Javascript to get the...
4
by: peashoe | last post by:
I have an asp page that uses a calendar.js (pop-up) file to add an exact date format in the text field (txtDDate). My problem is I need some javascript that sets an alert that does not allow them...
1
by: C | last post by:
Hi, I have two input fields on an ASP Page. User enters 2 dates. I want to check that the difference in months does not exceed 360 months. Is this possible in Javascript to get the...
3
by: natin | last post by:
Hi How do i display a date field using databind.eval ?
2
by: Winshent | last post by:
I have a dataset which is populated from a webservice. I want to filter a date field using DateTimePickers, but this just produces an error.. '########################################### If...
1
by: viki.sanjeeva | last post by:
Hi, There is a date field in JSP form where user will enter date in dd-mm-yyyy format. Now before saving into DB2, I want to validate the date format against dd-mm-yyyy format and then save into...
10
by: Jes | last post by:
Dear all I have a date field on a HTML form where the user is asked to key in dd/mm/yyyy However, when that is written to MySql it is either not accepted or another value is tored in the...
4
by: thelner | last post by:
Well, the app im working on makes an error when trying to populate a datagridview when the source contains date field. Any solution to my problem? dgvInvoices.DataSource =...
7
by: nhkam | last post by:
I am using MS Access 2007 I have a transaction table which holds all records with posting date on each of them. I used a make table query to find out the max date hold in the transaction table and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.