Results 1 to 9 of 9
  1. #1
    Dave14867's Avatar
    Dave14867 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Location
    Upstate NY
    Posts
    376

    Issue with DateAdd function

    Hello all,

    VBA is not my strong suit yet and I am having yet another issue with it. I am trying to use the DateAdd function to take a date and add days to it based ion data in a table. I cannot seem to get the syntax correct in the function. I tried using the builder then copying that and placing it in the after update event and it doesn't like it.

    Can someone let me know how to correct it.

    If you open up the frmAnimalDataEntry-tabbed, go to the 4th record, select the "Breeding" tab, then update the "Date Bred" field it will cause the problem.



    Pets-13.zip

    Thanks

    Dave

  2. #2
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    You can't just throw in a [table].[field] reference and expect Access to resolve it from within some other function. Even if it could, which record would you have it pick? And what if the field contained no value for the selected record? You need to explain what you want to do if you need further help. Otherwise, Dim a variable, assign it via DLookup, deal with the possibility of null, and pass the value as part of your expression. Maybe even better if you do the math before the DateAdd.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Calculation should be in ControlSource, not DefaultValue. DefaultValue serves no purpose because it only works for new record and a new record would not have DateBred and Early values.


    Can't reference a table directly like that. Access has no idea which record you need. Options in order of preference to retrieve species info:

    1. Include Early field as a column in Species combobox on main form then textbox expression references that column.

    =DateAdd("d", [Forms]![frmAnimalDataEntry-Tabbed].[Species].Column(3), [DateBred])

    That will show an error if DateBred has no value. Day is default unit for arithmetic with dates, don't really need DateAdd:

    =[DateBred]+[Forms]![frmAnimalDataEntry-Tabbed].[Species].[Column](3)

    2. DLookup() domain aggregate

    3. Include 'lookup' tables in form RecordSource


    Really no need to save this calculated value, calculate when needed.

    Advise not to use spaces nor punctuation/special characters (underscore only exception) in naming convention.

    Recommend naming controls different from fields or other objects, such as cbxSpecies. I always name subform container control different from the object it holds, like ctrBreeding.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    so to be clear the 'problem' is the code in the datebred after update event which is

    Me.EarlyDate = DateAdd("d", [DateBred] + [tblSpecies]![Early], [DateBred])

    I'm not going to spend a lot of time trying to decode your variables or what you are trying to acheive

    by now you should know that dates are stored as numbers - today is 43582 for example

    so if datebred is today, you are adding 43582 days to it - plus whatever [tblSpecies]![Early] is

    I don't know where you are getting [tblSpecies]![Early] from but for it to work, you would need a recordset called tblSpecies which is in scope at the time the function is run

    You also have the early field with a control source of your formula - so the afterupdate code is not required and should be removed.

  5. #5
    Dave14867's Avatar
    Dave14867 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Location
    Upstate NY
    Posts
    376
    June7,

    I can't seem to get the DLookup correct. Can you take a look at it. I am close I believe but just not quite there. Go through the same staep as previous and when you change the Date Bred date the error will occur.

    Pets-14.zip

    Thanks

    Dave

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Expression in textbox. Eliminate the VBA.

    =[DateBred]+DLookUp("Early","tblSpecies","SpeciesID_PK=" & [Forms]![frmAnimalDataEntry-Tabbed].[Species])
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Dave14867's Avatar
    Dave14867 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Location
    Upstate NY
    Posts
    376
    June7,

    Should that go in the default value of the DateBred or the default value of the unbound EarlyDate, or somewhere else?

    Dave

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    As advised in post 3, in EarlyDate textbox ControlSource property.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    Dave14867's Avatar
    Dave14867 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Location
    Upstate NY
    Posts
    376
    I got this figured out using a different method.

    Thanks for everyone's help.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. used function DateAdd
    By azhar2006 in forum Queries
    Replies: 2
    Last Post: 10-07-2015, 03:31 PM
  2. DateAdd Function?
    By zburns in forum Reports
    Replies: 7
    Last Post: 06-23-2015, 01:55 PM
  3. DateAdd issue
    By edmscan in forum Queries
    Replies: 7
    Last Post: 09-25-2014, 02:30 PM
  4. DateAdd Function
    By jschlapi in forum Forms
    Replies: 4
    Last Post: 10-23-2013, 01:36 PM
  5. Help Please - DateAdd Function
    By graviz in forum Queries
    Replies: 3
    Last Post: 03-02-2010, 02:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums