473,463 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Trying to set a date field to autofill based on a another date field

Hello,

I am trying to get a date field to autofill based on the date of another date field. I want the autofilled date to be 30 days after the other date. For example, if the user has entered 06/15 in the first date field, I want the second field to automatically populate with 7/15. I have tried everything I can think of to no avail! I can't imagine this is a difficult thing to do........

Anyone have any ideas?
Jan 14 '10 #1

✓ answered by NeoPa

The answer depends on how you intend to use this field to my mind. If you're entering new data then it's probably better to set the .DelaultValue property of the control. If it's simply for display, then set the .ControlSource to :
Expand|Select|Wrap|Line Numbers
  1. =IIf([DateField1] Is Null,Null,DateAdd('d',30,[DateField1]))

12 15935
ADezii
8,834 Expert 8TB
Assuming your 2 Date Fields are [Date1] and [Date2], in the AfterUpdate() Event of [Date1], place the following code. When finished, it should look like:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Date1_AfterUpdate()
  2.   'Is there a Value in [Date1] AND is it a Valid Date?
  3.   If Not IsNull(Me![Date1]) And IsDate(Me![Date1]) Then
  4.     Me![Date2] = DateAdd("d", 30, Me![Date1])
  5.   End If
  6. End Sub
Jan 14 '10 #2
MMcCarthy
14,534 Expert Mod 8TB
In the after update event of datefield1 put the following code ...

Expand|Select|Wrap|Line Numbers
  1. Me!datefield2 = DateAdd("d", 30, Me!datefield1)
Jan 14 '10 #3
MMcCarthy
14,534 Expert Mod 8TB
LOL! Snap
Jan 14 '10 #4
Thanks ADezii!

I was trying something close to that, but apparently ooooh soooo far off!!! :)
Jan 14 '10 #5
NeoPa
32,556 Expert Mod 16PB
The answer depends on how you intend to use this field to my mind. If you're entering new data then it's probably better to set the .DelaultValue property of the control. If it's simply for display, then set the .ControlSource to :
Expand|Select|Wrap|Line Numbers
  1. =IIf([DateField1] Is Null,Null,DateAdd('d',30,[DateField1]))
Jan 14 '10 #6
@NeoPa
NeoPA, I see what you were talking about now. The date field is populating on the form, but it not in the table. How do get the date into the table as well?
May 11 '10 #7
missinglinq
3,532 Expert 2GB
You don't! Very seldom is there a valid reason to store a calculated value in a table! It wastes storage space, and running a calculation is almost sure to take less time than retrieving a value form the disk.

Instead, you merely re-calculate the value as needed, in a form, report or query.

Welcome to Bytes!

Linq ;0)>
May 11 '10 #8
NeoPa
32,556 Expert Mod 16PB
What he just said :)
May 11 '10 #9
Not sure if this will help but I had a similar issue and this is what I did....

ACTION DESIRED: Have one date field trigger (calculate) future or past date values in other date fields in a form and (Most importantly)have those values save in the master database table and queries.

TRIGGER FIELD: Re Insp Date

AFFECTED CALCULATED FIELDS: Re Insp Date 90 Day (To be set to -90 from "Re Insp Date") Re Insp Date 60 Day (To be set to -60 from "Re Insp Date") Re Insp Date 30 Day (To be set to -30 from "Re Insp Date")

SOLUTION: Opened up the properties of what is to be the "trigger" date field went to 'All' tab Opened up a 'Code Builder' Event Procedure in the "After Update" property Entered the following VBA code

Expand|Select|Wrap|Line Numbers
  1.    Private Sub Re_Insp_Date_AfterUpdate()
  2.    Re_Insp_Date_90_Day = Re_Insp_Date - 90
  3.    Re_Insp_Date_60_Day = Re_Insp_Date - 60
  4.    Re_Insp_Date_30_Day = Re_Insp_Date - 30
  5.  
  6.    End Sub
NOTE: If there are spaces in the name fields, an underscore has to replace the spaces. As shown in the VBA code above, "Re Insp Date" field name must be coded as "Re_Insp_Date". (This is why many suggest you use no spacing in when naming database fields, tables, queries, forms, objects, etc)

Hopefully this helps someone else in the future.

Anyway, there it is.
May 20 '15 #10
NeoPa
32,556 Expert Mod 16PB
David.

I applaud your intention to help, but am less sanguine about your posting this in spite of the strong advice that the intended solution is a very bad idea. Sometimes, to give someone what they ask for is not an act of kindness.

I'm not sure why you would do so after this has been clearly pointed out.
May 21 '15 #11
zmbd
5,501 Expert Mod 4TB
There are a few times when storing calculated results can be justified - admittedly very few; however, for some things like discounts that may change over time or in my case where I do store certain correction factors due to legal/contractual obligations.

However, as missinglinq so clearly pointed out, this should not be the normative action.

As shown in the VBA code above, "Re Insp Date" field name must be coded as "Re_Insp_Date". (This is why many suggest you use no spacing in when naming database fields, tables, queries, forms, objects, etc)
Ok this is not an absolute requirement that the spaces be replaced... one need only enclose the names in brackets:
Expand|Select|Wrap|Line Numbers
  1. [Re Insp Date 90 Day] = [Re Insp Date] - 90
I do however strongly advise against using spaces in field names etc... as, IMHO, spaces needlessly complicate the coding and there are also other potential issues that can crop up :(
May 22 '15 #12
NeoPa
32,556 Expert Mod 16PB
zmbd:
There are a few times when storing calculated results can be justified
Normalisation requires that values that can otherwise be calculated are not saved. If the ability to calculate a value is no longer there then saving that value contravenes no normalisation requirements. That would certainly be the case for time-relevant calculations such as you mention.
May 23 '15 #13

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

Similar topics

0
by: Chris Sharman | last post by:
I'd like to design my pages to work cooperatively with browser autofill features. I've loked around, but can't find any good documentation on supported/unsupported field names...
0
by: Ray Holtz | last post by:
Is it possible to autofill a field based on what is entered into another field in a form? My form has an employee field, and department field. In an Items Table, I have fields FldEmployee, and...
2
by: Edwin Knoppert | last post by:
I would like to instruct a grid which autofill's the columns to exclude time or date from a date field when date or time is 0. Date and time can be stored in a date field, my grid shows for...
1
by: Stephen D Cook | last post by:
I have a search form with a query-linked subform. One of the fields in the subform are date fields. Is there a way to autopopulate the DateFilled field when the person enters the field? I need...
10
by: sandraz444 | last post by:
I have an expression in the query under my form to autofill the date under a certain condition but it wont write to the underlying table?? The date shows in the form but not the table. Does anyone...
2
by: dympna | last post by:
Hi can anyone suggest a fix for this... as I am a novice in access. I have created a training table with the following fields Employee Name - joe Training Received - Fork lift Date Received...
0
by: Randy | last post by:
Hi, I have some comboboxes that are created dynamically at run time based on user actions. I found a procedure on a message board to autofill the combobox text from the dataview that the...
3
by: newyorkcity | last post by:
I have two fields in a form: Client ID Client Name I wan to be able to enter the Client ID and have the name of that client to appear automatically under the client name field. How do I...
8
by: mjvm | last post by:
Hi, I'm new to asking for help on the web, but haven't been able to find an answer to my question through Google - probably because I am not using the right words. In my form, I want to be able...
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
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,...
1
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...
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,...
0
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...
0
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...
0
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 ...

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.