473,387 Members | 1,416 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.

How do I assign a default date value to one control based on the date of another??

I have two date controls in my main form. I would like the default value of the second date control to reflect 7 days prior to the first control. I know what event I should put the code in I just don't know what to put. Does anybody have any ideas?
Aug 27 '11 #1
7 1744
NeoPa
32,556 Expert Mod 16PB
Try :

Expand|Select|Wrap|Line Numbers
  1. Private Sub txtDate1_AfterUpdate(Cancel As Integer)
  2.     With Me
  3.         .txtDate2.DefaultValue = IIf(IsDate(.txtDate1, _
  4.                                      DateAdd("d", -7, CDate(.txtDate1)), _
  5.                                      Null)
  6.     End With
  7. End Sub
Aug 28 '11 #2
It wouldn't compile but I figured out what I needed by tinkering around. My two controls are examCompDate (the first date) and examEffDate (the second). I didn't think it would be this simple. However, without your code I wouldn't have thought to do this. Thank You!!

Expand|Select|Wrap|Line Numbers
  1. Private Sub examCompDate_AfterUpdate()
  2. [examEffDate].Value = [examCompDate].Value - 7
  3. End Sub
Aug 28 '11 #3
NeoPa
32,556 Expert Mod 16PB
Of course it won't compile without the proper control names in there. You didn't share that information in your question, so I had to put in something in their places to make it obvious what you needed to do.

Your code is thoroughly wrong though, as it doesn't allow for there being no date in the first control (we now know to be called [examCompDate]) or non-date data being entered into it and, although adding day values to dates can be done using integer arithmetic, that is not the recommended way for good reason. Also, your question asked for how to set the default value of the other control, which your code also doesn't manage properly.

Unless you now need something different from what you originally asked for, what you need is :

Expand|Select|Wrap|Line Numbers
  1. Private Sub examCompDate_AfterUpdate(Cancel As Integer)
  2.     With Me
  3.         .examEffDate.DefaultValue = IIf(IsDate(.examCompDate), _
  4.                                         DateAdd("d", -7, CDate(.examCompDate)), _
  5.                                         Null)
  6.     End With
  7. End Sub
... which is what I said in post #2 but which you didn't properly understand.
Aug 28 '11 #4
Oh I knew to replace the control names. I'm sorry I probably should've said so in my last post. They're both date/time data types so no worries about putting in anything other than the required info. It turns out I did actually just want it to set the value and not the default value(although now I have it set for both) :). I have the format on both controls set to short date. I didn't think of the null value so I tried it and all the second date does is return a null value so no worries there. Thanks again!!
Aug 29 '11 #5
NeoPa
32,556 Expert Mod 16PB
In light of that I was able to determine that I had actually posted suggested code with an error. It was a missing closing parenthesis for the IsDate() call (which I've now fixed retrospectively). Apologies for that.

It sounds like you have already considered most of the issues, but let me just bring up a few considerations for you anyway, in case you missed any.
  1. Are both of the controls bound (IE. Connected to fields from the data source of the form)?
    1. If they are both bound then you need to be careful about assigning a value to [examEffDate] in case one already exists. Overwriting a value indiscriminately makes little sense unless you always want the value to reflect seven days prior to [examCompDate]. I hope this isn't the case because then you're getting into issues of normalisation (and in this case non-conformant design to the concept - See Database Normalisation and Table structures). To assign a value only when the control doesn't already contain a value requires slightly changed code, but at least makes sense within a well-designed database.
    2. If [examCompDate] is unbound, then assuming that the value of it within your procedure is a valid date is not reliable unless you have configured the control to ensure this is the case (The Format property has absolutely no effect on this of course, but other properties can do - particularly, but not exclusively, ValidationRule).
    3. If [examEffDate] is unbound then there should be no issues.
    From the fact that you state :
    MSignor:
    They are both Date/Time types
    I would surmise they are actually both bound (as controls are not typeable in that way so you must be referring instead to the bound fields). The linked article explains why that's not too good an idea, but I'll leave you to explore the details of that.
  2. The only other point really is the advisability of using standard arithmetic on date values. It works - There's no question of that. Is it a reliable way to think though? A habit of using that will leave you exposed to writing code that is both less readable (If you don't know already you will come to experience how important an issue is the clear readability and sense of code.) and potentially less reliable. It may well be that allowing ordinary numeric values is precluded somewhere in your design, but can you guarantee you will never be in a situation where none slip through in other areas of your code when you follow a similar approach with dates? Frankly, the clincher for me is the readability and maintainability of your code, so I always feel it's worth using the date/Time specific functions.

Ultimately you make your own choices. I simply want to bring some ideas to your attention for your awareness which I hope prove useful. Good luck.
Aug 29 '11 #6
You were right. They're both bound. I got it to compile by removing the "Cancel As Integer" and then I realized after it didn't change the value (in the current record) on the second date control because it was still set to defaultvalue which is what I originally asked for and am still going to use now along with being able to alter the value if necessary. If at the very least I will use your code as to not confuse someone else down the road. Thanks for all the time you put into this thread. This was my first thread on here but next time I will make sure to be more specific. :)
Aug 29 '11 #7
NeoPa
32,556 Expert Mod 16PB
MSignor:
This was my first thread on here but next time I will make sure to be more specific. :)
If that's the only thing you learn from this experience then it's a gold mine. You can get so much more out of this site with that understanding :-)
Aug 30 '11 #8

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

Similar topics

2
by: Job Lot | last post by:
how can i assign a default value for a custom property?
6
by: Greg | last post by:
I am trying to change the value of one field in my query based on the value in another. If Field 1 had the text "ONACCNT" then in Feild 2 I want the date to change to today's date. Any help would...
1
by: David Lozzi | last post by:
Howdy, I am trying to accomplish the following: dim DateSend as date if txtDateSend.text = "" then DateSend = dbnull.value else DateSend = txtDateSend.text
10
by: Brad | last post by:
If I need to assign a NULL value to a date variable, how would I go about doing that? For right now I am using 01-01-2001, however I don't want to use a "real" date if the date should really be...
2
by: planetthoughtful | last post by:
Hi All, I have a calendar form that updates a date field on the form from which it was called. I have code in the OnChange event of the date field that I would like performed whenever the date...
3
by: Liam Mac | last post by:
Hi All, Can anyone direct me or provide advice on how I can assign a null value to a date variable in vb.net. Basically what I'm doing is that I'm looping through a recordset where I have three...
0
by: venky | last post by:
Can anybody let me know is windows date time control with time format at hh:mm:ss , when i click to increment , by default it increments the hour value, but for some controls i want to increment...
1
by: shapper | last post by:
Hello, I have a function as follows: Public Function myF() As String .... End Function
7
by: sharmilah | last post by:
Hi all It's me again. I now want to specify the current date in the format '26/06/2007' ie date/month/year as the default value in my mysql database. Which implies that by default the value of my...
0
by: jholland13 | last post by:
Hi, I manage a Access 07 dbase as part of my job. I am not very good at programming, no formal training. Most things it does by code I found snippets and modified them. Something I want to do is...
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:
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
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
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
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...

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.