473,387 Members | 1,925 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,387 software developers and data experts.

Validation of Dates

My table is defined as follows :

Reference text Primary Key
StartDate date Primary Key
EndDate date can be null

I currently have a row with :

Reference = ABC,
StartDate = 1st Jan 2009, and
EndDate = 1st November 2009.

I want my Form to allow me to enter a 2nd row with
Reference = ABC, and
StartDate greater than or equal to the EndDate above.

If I enter an EndDate prior to this, then I want to display a meaningful message.

This should be the case for all References, not just ABC.

Should I do so as a Validation Rule in the table design
Or an ‘on enter’ event in the Form design ?

And what would the coding look like ?

Many thanks
Nov 19 '09 #1
8 1598
ChipR
1,287 Expert 1GB
I think I would use the BeforeUpdate event of the form, and check the entered date against a
Expand|Select|Wrap|Line Numbers
  1. DLookup("StartDate", "myTable", "Reference = """ & Reference & """")
Nov 19 '09 #2
NeoPa
32,556 Expert Mod 16PB
@grinder332518
I would assume you actually mean StartDate.

With that basic assumption, and that the controls on your form are named as the fields are in the table, then you want something like the following in your Form_BeforeUpdate() event procedure :
Expand|Select|Wrap|Line Numbers
  1. Dim datMax As Date
  2.  
  3. datMax = DMax("[EndDate]", _
  4.               "[YourTable]", _
  5.               "[Reference]='" & Me.Reference & "'")
  6. If Me.StartDate < datMax Then
  7.     Call MsgBox("Your failure message here")
  8.     Cancel = True
  9.     Exit Sub
  10. End If
Nov 23 '09 #3
Thanks NeoPa.
As you gathered, I did mean StartDate.
Your suggestion works beautifully.... as far as it goes.
However, I simplified my example when posting.
The field “Ref” should actually be 2 separate fields : “Ref1” and “Ref2”.
I have tried to enhance your coding by altering the 3rd argument :
Expand|Select|Wrap|Line Numbers
  1.    "[Ref1]='" & Me.Ref1 & "'" , "[Ref2]='" & Me.Ref2 & "'")
but cannot get the syntax correct.
Can you assist again please ?
Many thanks.
Nov 24 '09 #4
NeoPa
32,556 Expert Mod 16PB
You're nearly there. You have to use "AND" between the two items :
Expand|Select|Wrap|Line Numbers
  1.    "[Ref1]='" & Me.Ref1 & "' AND [Ref2]='" & Me.Ref2 & "'")
The syntax is identical to the WHERE clause in SQL except without the keyword WHERE, if that helps.

Although it's rarely necessary, I usually put parentheses around separate items thus :
Expand|Select|Wrap|Line Numbers
  1.     "([Ref1]='" & Me.Ref1 & "') AND " & _
  2.     "([Ref2]='" & Me.Ref2 & "')")
Nov 24 '09 #5
Thanks NeoPa
I should have mentioned that Ref2 is defined as a number, not text.
Will this effect the coding ?
Also, when I create a new Ref1 / Ref2 combination, there is no existing EndDate for a previous row. Will the code still allow me to enter a StartDate ?
Many thanks for your continuing support.
Nov 24 '09 #6
missinglinq
3,532 Expert 2GB
Excuse me, but do you ever plan on giving us all the facts here? Posting a problem, and when it's answered, saying "Oh, but that's not all" and when that's answered saying "Oh, but that's not all" and repeating this four times is simply not acceptable!

State your problem, in full, when you originate your post!

Linq ;0)>
Nov 24 '09 #7
NeoPa
32,556 Expert Mod 16PB
As Linq says, it is only considerate to give a bit more thought to your question before posting it. It may be tedious, but if not doing so causes those trying to help you to spend extra time, fundamentally unnecessarily, on your problems, then this should be seen in the context of asking a favour. We're happy to put in the time if it's for something you're struggling with, but less happy to spend our time simply to save you the effort of preparing your question properly.

I appreciate your first effort was an attempt to be quick and not unduly complicated, which is a good thing. I'd just request, much as Linq did, that you prepare a little more carefully to avoid unnecessary time wastage.

Moving on now to the question.
@grinder332518
Yes. Definitely.

The single-quotes will not be required for numeric literals. They are only required for strings. See Quotes (') and Double-Quotes (") - Where and When to use them & Literal DateTimes and Their Delimiters (#) for the full explanation.
@grinder332518
This is a scenario I hadn't coded for. Such a situation would crash the code as it now stands. Try instead, something like :
Expand|Select|Wrap|Line Numbers
  1. Dim datMax As Date
  2.  
  3. datMax = Nz(DMax("[EndDate]", _
  4.                  "[YourTable]", _
  5.                  "[Ref1]='" & Me.Ref1 & "' AND " & _
  6.                  "[Ref2]=" & Me.Ref2), #1/1/1900#)
  7. If Me.StartDate < datMax Then
  8.     Call MsgBox("Your failure message here")
  9.     Cancel = True
  10.     Exit Sub
  11. End If
Nov 24 '09 #8
many thanks for your help.
the code is working fine now.
I'll be more specific in future.
Dec 2 '09 #9

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

Similar topics

21
by: AnnMarie | last post by:
<script language="JavaScript" type="text/javascript"> <!-- function validate(theForm) { var validity = true; // assume valid if(frmComments.name.value=='' && validity == true) { alert('Your...
4
by: Dale | last post by:
Hi Everyone, I've got a form that provides a pop-up calendar for users to select dates for requesting jobs to be completed. The calendar works great, but it unfortunately allows users to select...
30
by: Dr John Stockton | last post by:
It has appeared that ancient sources give a method for Numeric Date Validation that involves numerous tests to determine month length; versions are often posted by incomers here. That sort of code...
14
by: Steve Wright | last post by:
I am trying to validate if an entry on a form is a date. I have adapted code I found here http://www.codingforums.com/archive/index.php/t-14325 as below but I can't seem the get the results that I...
23
by: Ash | last post by:
Hi everyone, This problem has been puzzling me for a fair time now, and has severely frustrated me!! Perhaps I'm just not getting the syntax right, but the problem is rather simple... Take a...
3
by: Vi | last post by:
Hi, I have a form which displays some orders based on dates selected or based on an order number. I have two different buttons for each kind of query, but both buttons call the same method in the...
5
by: JIM.H. | last post by:
Hello, I have this validation expression: ^(?:(?:0?|1)|(?:0?|11)(?!\/31)|(?:0?2)(?:(?!\/3|\/29\/(?:(?:0||)00|(?:\d{2}(?:0||))))))\/(?:0?||3)\/\d{4}$ This is supposed to match MM/DD/YYYY it is...
3
by: panjap | last post by:
Hello i have had problems with validation with dates, as i find this difficult to insert into my coursework on a shop and ordering goods. Heloo everyone, i am currently struggling on how to...
2
by: mshroom12 | last post by:
I am having trouble with the following project on hand. I use Eclipse to do my work in Java. This is what I'm supposed to complete. Date Validation In this exercise you will write a program...
5
by: maz77 | last post by:
I'm trying to develop a good validation for a date in C#; a valid date can be inserted in these formats: - dd/mm/yyyy - dd/mm/yyyy h24:m - mm/dd/yyyy - mm/dd/yyyy h12:m How can I proceed? Is...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...

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.