473,545 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validating the date field using VBA code

vs2k8
13 New Member
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_B eforeUpdate(Can cel 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_Com p_Dt.Value)) Then
MsgBox "Order Complete Dt cannot be null,Please enter date"
Ord_Comp_Dt.Set Focus
isValid = False
End If
If (Ord_Rcvd_Dt > Ord_Comp_Dt.Val ue) Then
MsgBox "Order Complete Dt should be greater then Order Recv Date!"
Ord_Comp_Dt.Set Focus
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 13732
hjozinovic
167 New Member
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 New Member
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.Canceleve nt, 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 New Member
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.Canceleve nt, 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.CancelEve nt 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 New Member
hey Visa,

i'm not sure why you get the message 'You canceled the previous operation'.
Obviously it's because of DoCmd.CancelEve nt 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,eve n 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 New Member
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 New Member
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="N o" OR IsNull(Me.Produ ct) Then
MsgBox "InOrdDoc and InOrdDoc2 must be yes and Product cannot be null."
DoCmd.CancelEve nt
End If
End If
Thanks again!

-visa
Jul 3 '08 #7
hjozinovic
167 New Member
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 New Member
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
1513
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 difference in
4
3430
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 to select today. example: var dtToday = Date() if(document.frmSoftware.txtDDate.value == dtToday) { alert("You cannot select same day...
1
1399
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 difference in
3
6102
by: natin | last post by:
Hi How do i display a date field using databind.eval ?
2
1371
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 chkDateFilter.Checked Then d = calDateFrom.Value.ToString d = d.Substring(0, 10)
1
4190
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 DB2. I've tried reading SimpeDateFormat and other date format classes, but couldn't understand. It will be great if somebody tell the following...
10
2530
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 database. Is there any way to change value of this field back to yyyymmdd format as accepted correctly in sql. ? The change should preferably be when...
4
2036
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 = richdb.getTableData("Select invoiceno,duedate as string from invoice Where IsCleared = '0' and duedate <= '" & convert.tomysqldatenow & "'") Error: Unable to convert...
7
3465
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 use the dateAdd function to find the date with 1 month, 5 months and 6 months ago and export it into a table name TRSMPD that only have one record. I...
0
7468
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7808
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7423
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7757
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4945
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3450
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3443
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1884
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.