473,396 Members | 2,010 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,396 software developers and data experts.

How To set the focus of a text field after updating the field

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.

Expand|Select|Wrap|Line Numbers
  1. If Me.Arrival_Date > Me.Departure_Date Then
  2.   MsgBox ("Departure date should be later than Arrival Date")
  3.   Me.Departure_Date.Undo
  4.   Me.Dirty = False
  5.   End If
Jun 29 '18 #1

✓ answered by PhilOfWalton

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

Expand|Select|Wrap|Line Numbers
  1. Private Sub FromDate_BeforeUpdate(Cancel As Integer)
  2.  
  3.     If FromDate > ToDate Then
  4.         MsgBox "'From Date' must be before 'To Date'"
  5.         Cancel = True
  6.     End If
  7.  
  8. End Sub
  9.  
  10. Private Sub ToDate_BeforeUpdate(Cancel As Integer)
  11.  
  12.     If ToDate < FromDate Then
  13.         MsgBox "'To Date' must be after 'From Date'"
  14.         Cancel = True
  15.     End If
  16.  
  17. End Sub
  18.  
Phil

10 1519
twinnyfo
3,653 Expert Mod 2GB
Between Lines 4 and 5 add:
Expand|Select|Wrap|Line Numbers
  1. 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!
Jun 30 '18 #2
PhilOfWalton
1,430 Expert 1GB
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

Expand|Select|Wrap|Line Numbers
  1. Private Sub FromDate_BeforeUpdate(Cancel As Integer)
  2.  
  3.     If FromDate > ToDate Then
  4.         MsgBox "'From Date' must be before 'To Date'"
  5.         Cancel = True
  6.     End If
  7.  
  8. End Sub
  9.  
  10. Private Sub ToDate_BeforeUpdate(Cancel As Integer)
  11.  
  12.     If ToDate < FromDate Then
  13.         MsgBox "'To Date' must be after 'From Date'"
  14.         Cancel = True
  15.     End If
  16.  
  17. End Sub
  18.  
Phil
Jun 30 '18 #3
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.
Jun 30 '18 #4
PhilOfWalton
1,430 Expert 1GB
@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
Jun 30 '18 #5
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.
Jun 30 '18 #6
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
Jul 1 '18 #7
PhilOfWalton
1,430 Expert 1GB
Are you sure the value is not updated?

Phil
Jul 1 '18 #8
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?
Jul 1 '18 #9
thanks for your response now it is working fine.
Jul 2 '18 #10
NeoPa
32,556 Expert Mod 16PB
Excellent. And well done also for selecting the Best Answer.
Jul 2 '18 #11

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

Similar topics

16
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...
7
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...
4
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...
2
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...
9
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...
7
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...
5
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...
3
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...
7
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...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...
0
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...
0
agi2029
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,...

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.