Results 1 to 12 of 12
  1. #1
    strangebiscuit is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    6

    Automatically update a field when a box is checked

    Hi, I'm fairly new to using Access for any serious purposes. I'm trying to replicate some database functionality I setup in Salesforce and just trying to get my head around a few core concepts in Access, so pardon if this question seems silly.

    I'm messing around in the Contact Management template and I'm wondering how I can make it so when so when a yes/no checkbox field (called "Active") is ticked a date/time field (called "Last Date") is automatically set to today's date + 60 days.

    In Salesforce I would create a Workflow Action triggered by an if Active = true statement, with a Field Update something like TODAY() + 60.

    I can tell I need to create an After Insert/After Update Data Macro but can't quite figure out what to put in.

    Any tips?

    Thanks for your advice and patience with the simplistic question.

  2. #2
    alansidman's Avatar
    alansidman is offline Indifferent
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,536
    Create an after update event for the checkbox, then insert this code
    Code:
    If me.Active = True then
    me.[Last Date] = Date + 60
    End if

  3. #3
    strangebiscuit is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    6
    Quote Originally Posted by alansidman View Post
    Create an after update event for the checkbox, then insert this code
    Code:
    If me.Active = True then
    me.[Last Date] = Date + 60
    End if
    Hey, thanks for the reply! Greatly appreciated.

    When I click the "After Update" button I don't have any place to drop in complete code like that...just this kind of macro builder screen where I have to select commands and fill in the values...the only way I can seem to do it is to use these Edit Record > Set Field commands. Screen shot below:

    Click image for larger version. 

Name:	macrobuilder.jpg 
Views:	27 
Size:	129.0 KB 
ID:	17441

    But even with this when I go to edit a contact and check the active box I get the error: "Edit record failed because the default alias represents a record which is read only". The Last Date field is not set to read-only as far as I can tell.

    I'm I going about this in a totally incorrect way? Should I not be clicking the "After Update" button in the "Table Tools" tab?

    Sorry again if this is painfully obvious and I'm just missing it. Thanks again for your help!

  4. #4
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    What Alan showed you was VBA, not a macro. You need to create an On Click event procedure for the checkbox, and put the code in there.

    If you are not familiar with how to do that, post back.

    HTH

    John

  5. #5
    strangebiscuit is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    6
    Quote Originally Posted by John_G View Post
    What Alan showed you was VBA, not a macro. You need to create an On Click event procedure for the checkbox, and put the code in there.

    If you are not familiar with how to do that, post back.

    HTH

    John
    OK...that helped. Think I got it...didn't realize I had to do this on the form rather than the table. Went into Design View on the form that the checkbox is on, clicked the box and then went to the Event tab in the Properties section on the right. Found the On Click box, selected [Event Procedure] and then clicked the litter "..." button. This opened up the VBA editor, pasted in the code and it seems to work. Thanks so much.

    Didn't seem inherently obvious to me, but I get it now...though not sure I fully understand the difference between an Event Procedure and a Data Macro. Hate to ask for more, but just curious...could you tell me what's the purpose of the "me." in front of the field names and why "[Last Date]" needs brackets but "Active" does not?

    Thanks again for the help you've already given though. I greatly appreciate it and it's definitely helping me understand how things work.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    Saving calculated data is usually a bad idea and there are few justifications for it. Your date value can probably be calculated when needed.

    Macros (of any variety) are not VBA. [Event Procedure] means VBA code.

    The Me is VBA shorthand reference for the form or report object the code is behind.

    [Last Date] requires brackets because of the space. This is why spaces and special characters/punctuation (underscore is exception) should be avoided in naming convention, as well as reserved words as names.
    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
    strangebiscuit is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    6
    Quote Originally Posted by June7 View Post
    Saving calculated data is usually a bad idea and there are few justifications for it. Your date value can probably be calculated when needed.

    Macros (of any variety) are not VBA. [Event Procedure] means VBA code.

    The Me is VBA shorthand reference for the form or report object the code is behind.

    [Last Date] requires brackets because of the space. This is why spaces and special characters/punctuation (underscore is exception) should be avoided in naming convention, as well as reserved words as names.
    Hmm...I find the terminology confusing because when I create macros in in Excel they are most certainly VBA.

    When you say saving calculated data is a bad idea and the date can probably be calculated when needed...what kind of change are you suggesting exactly? Is doing it with the on click event procedure not the proper way to go about it?

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    Yes, what Excel calls macro is really VBA code. The macro recorder actually creates a VBA procedure but the syntax of much of the generated code is often quite different.

    I just reread your first post and gained some new understanding. I originally thought the +60 was being added to a date field in db, such DateSubscribed or DateRenewed or some such initializing date in the record. I presume you are editing an existing record with this checkbox update? What is purpose of [Last Date]? Perhaps with your data structure saving the calculated date is the most convenient immediate solution. Would need to know more about db. Just keep in mind that saving calculated data has risk of data getting 'out of sync'.
    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
    strangebiscuit is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    6
    Quote Originally Posted by June7 View Post
    Yes, what Excel calls macro is really VBA code. The macro recorder actually creates a VBA procedure but the syntax of much of the generated code is often quite different.

    I just reread your first post and gained some new understanding. I originally thought the +60 was being added to a date field in db, such DateSubscribed or DateRenewed or some such initializing date in the record. I presume you are editing an existing record with this checkbox update? What is purpose of [Last Date]? Perhaps with your data structure saving the calculated date is the most convenient immediate solution. Would need to know more about db. Just keep in mind that saving calculated data has risk of data getting 'out of sync'.
    The purpose of the event between the checkbox and [Last Date] is simply that when the box is checked the [Last Date] field is set to 60 days from the current day. Like imagine if a contact has a subscription and when they cancel it's marked "inactive", but the subscription always remains technically active for 60 days from the date of the cancellation. So [Last Date] gets automatically populated with the last date of activity.

    I'm just kind of conceptualizing how I'd port certain aspects of a Salesforce customization into an Access database. How likely is it that this kind of data would go "out of sync"? I've not heard of that...

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    If someone managed to get to the table or query and did edits there, the code would not execute. Saved aggregate calcs (summary data) are especially at risk to get out-of-sync with the raw data.

    I should think a cancellation date would be more informative than a checkbox. The +60 could be added to the DateCancel field 'on-the-fly' as needed in textbox calculation.
    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.

  11. #11
    strangebiscuit is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    6
    Quote Originally Posted by June7 View Post
    If someone managed to get to the table or query and did edits there, the code would not execute. Saved aggregate calcs (summary data) are especially at risk to get out-of-sync with the raw data.

    I should think a cancellation date would be more informative than a checkbox. The +60 could be added to the DateCancel field 'on-the-fly' as needed in textbox calculation.
    Huh, interesting. I don't want to make this thread huge asking lots of questions and favors, but I do really appreciate the tips... These are not factors I ever had to worry about in Salesforce.

    The [Last Date] field is a requirement that the users requested...but do you mean it'd be safer to store the cancellation date in a field and use that date + 60 to populate [Last Date] rather than using today's date? I guess I'm not entirely sure what "on-the-fly" textbox calculation is and how it differs from what I'm trying to do.

    Again, in Salesforce when someone asked for a date to be populated/calculated when a checkbox is checked, I just create a workflow action and put in the formula and it's done. Obviously it's not really comparable to Access and they are quite different systems, but I assumed I could create similar functionality without too much trouble and risk of corruption.

  12. #12
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    On-the-fly means the calculation is performed whenever needed, such as when a report is opened. An expression in textbox or query would do the calc so it is always fresh (current). If the CancelDate is changed or removed (maybe subscriber comes back) the calculated LastDate would automatically adjust, without relying on user to force the recalc.

    A basic principle of relational database is to enter raw data and do calcs in reports - whenever possible. Saving calculated values (data dependent on other data) has risks.

    Having said all that, I will confess to having a database that violates all sorts of principles to accomplish what I want. It is for managing lab test data. The test calcs are so involved and complex we must save the results (much more complex than Quantity * Price or DateValue + 60) and we don't want existing results to be altered if a formula is changed. In essence, the calculated results become the 'raw data' used for reports and data analysis.
    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.

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

Similar Threads

  1. Automatically Update Field on Subform?
    By batowl in forum Forms
    Replies: 2
    Last Post: 02-08-2013, 08:16 AM
  2. Replies: 9
    Last Post: 04-13-2012, 10:10 AM
  3. Replies: 4
    Last Post: 04-18-2011, 07:18 AM
  4. Automatically update field
    By Top Fuel Friday in forum Forms
    Replies: 3
    Last Post: 02-12-2011, 12:14 PM
  5. Replies: 5
    Last Post: 08-20-2010, 06:40 AM

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