our project we have a date field which should be validate, Like
departure date should not be less than arrival date.
i have written the code and it is working fine, but the problem is when i click on tabout the cursor should be in the same field,until the departure date changes,it should not go to any other field.
could any one please suggest me any solution to this problem. - If Me.Arrival_Date > Me.Departure_Date Then
-
MsgBox ("Departure date should be later than Arrival Date")
-
Me.Departure_Date.Undo
-
Me.Dirty = False
-
End If
You need to check both dates. You are making the assumption that the Arrival Date is correct, and that there has been an error in entering the departure date. It could be the other way round.
This bit of code checks both dates and works for me. The cursor remains in the textbox -
Private Sub FromDate_BeforeUpdate(Cancel As Integer)
-
-
If FromDate > ToDate Then
-
MsgBox "'From Date' must be before 'To Date'"
-
Cancel = True
-
End If
-
-
End Sub
-
-
Private Sub ToDate_BeforeUpdate(Cancel As Integer)
-
-
If ToDate < FromDate Then
-
MsgBox "'To Date' must be after 'From Date'"
-
Cancel = True
-
End If
-
-
End Sub
-
Phil
10 1519
Between Lines 4 and 5 add: - Me.Departure_Date.SetFocus
That should keep the cursor on that field.
This assumes that Me.Departure_Date is the name of the Text Box, not just the name of the field. The Text Box should have a name unique from the field, such as txtDeparture_Date . This is a good practice with all your controls.
Hope this hepps!
You need to check both dates. You are making the assumption that the Arrival Date is correct, and that there has been an error in entering the departure date. It could be the other way round.
This bit of code checks both dates and works for me. The cursor remains in the textbox -
Private Sub FromDate_BeforeUpdate(Cancel As Integer)
-
-
If FromDate > ToDate Then
-
MsgBox "'From Date' must be before 'To Date'"
-
Cancel = True
-
End If
-
-
End Sub
-
-
Private Sub ToDate_BeforeUpdate(Cancel As Integer)
-
-
If ToDate < FromDate Then
-
MsgBox "'To Date' must be after 'From Date'"
-
Cancel = True
-
End If
-
-
End Sub
-
Phil
NeoPa 32,556
Expert Mod 16PB
Also, don't include your original line #4. That is a way of saying "Save the changes to this record". I suspect that's not what you were after.
Look at how Phil has posted his response and notice he's included all the relevant information - much that you missed out of your post.
From that you can see that the call to .Undo is not necessary. Setting Cancel = True is what ensures that he entered value is not accepted. Whether you revert it to what it was beforehand with .Undo is up to you.
NB. Technically you should at least compare the dates for <= & >=. Just < & > does not test for what you need.
@Neopa
The ToDate (ArrivalDate) and FromDate (DepartureDate) could well be the same.
On most or my car trips I arrive the day I leave, so, yet again I beg to differ on your suggestion about using the <= and >= comparisons.
As ever, your disagreeable friend,
Phil
NeoPa 32,556
Expert Mod 16PB
You're absolutely correct (on this occasion) Phil.
I'd misremembered the explanation from the first post and assumed the values being stored were DateTimes. Now I've reread it, it seems likely that we're dealing with simple dates after all and their being equal can be perfectly valid.
The code which is posted in the question is working fine, but my problem is when i intentionally pressed tabout the courser is going to another field.It should no goto another field without updateing the departure date with correct value
Are you sure the value is not updated?
Phil
NeoPa 32,556
Expert Mod 16PB Malavika:
The code which is posted in the question is working fine, but my problem is ...
Why are we talking about your original code at this stage? We should be talking about what happens when you use the code that's been suggested.
Otherwise it's all a bit pointless asking the question no?
thanks for your response now it is working fine.
NeoPa 32,556
Expert Mod 16PB
Excellent. And well done also for selecting the Best Answer.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Michael Walton |
last post by:
I am trying to write some code that inserts or updates a text field in my
SQL Server 2000 database. The SQL statement is created using a submit from
a form, and the text is coming from a...
|
by: Rick Caborn |
last post by:
Does anyone know of a way to execute sql code from a dynamically built
text field?
Before beginning, let me state that I know this db architecture is
built solely for frustration and I hope to...
|
by: MT |
last post by:
Hi all, this sounds like an easy enough thing to do, but after
spending 45 minutes searching google and various javascript sites I
can't find out how to make a textfield (textbox or whatever you...
|
by: NewBob |
last post by:
Since Access automatically highlights all of the text in a text
control (I use it to hold data from a memo field) when the control is
activated, I've added the following code to put the cursor at...
|
by: Pam Ammond |
last post by:
I need the code to update the database when Save is clicked and a text field
has changed. This should be very easy since I used Microsoft's wizards for
the OleDBAdapter and OleDBConnection, and...
|
by: usenet |
last post by:
I would like, if it's possible, to set the value of a field in a table
to a number of spaces. One space would be fine, I just want to be
able to set the field to a default value that's not NULL...
|
by: Bubo.virginianus |
last post by:
Heya!
I feel I'm beating an old horse to death with this question but I
can't get around the problem.
I want to update a text field on a form with a string from a VB
module. I need...
|
by: dugald.morrow |
last post by:
I have some javascript that updates the text in a text field after
certain actions take place such as clicking a checkbox. The javascript
works fine in Safari and Firefox, but in IE, the text in...
|
by: gmiotke |
last post by:
I know that this is easy for those that know but alas this is not me ;)
I have a multi part form that is going to write to a dB.
As a point of reference I can offer geico.com (i.e., the quote...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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: 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: 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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |