Results 1 to 7 of 7
  1. #1
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    136

    Action based off of checkbox


    Hi all,
    I have 2 forms in my DB. I have a simple checkbox on Form1 with values of 0,-1 in the table depending on whether it's checked or not. What I want to do is populate Field2 in Form2 with data from Field1 in Form1 when the box is checked. Then take the existing data in Field2 in Form2 and move it to Field3 in Form2. Is there any possible way to do this?

  2. #2
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Yes, it should be pretty straight forward VBA code.

    Do you want this to happen as soon as the checkbox is checked or after some sort of confirmation button is clicked? What happens if it's unchecked, do you want to reverse it?

  3. #3
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    136
    Quote Originally Posted by kd2017 View Post
    Yes, it should be pretty straight forward VBA code.

    Do you want this to happen as soon as the checkbox is checked or after some sort of confirmation button is clicked? What happens if it's unchecked, do you want to reverse it?
    Thanks for the reply. Now that you mention it, it might be nice to pop up a warning that it's going to change data. Same thing if it's unchecked. If it's unchecked it could be reversed. How do I do it?

  4. #4
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    1st you'll need to decide after what user triggered event you want all this to happen. It could be after the checkbox is updated or on a button click. In your form's design view select the control (your chosen checkbox or button) and in the properties window go to the 'Event' tab, click the '...' button on the event you want to use, then select code builder.

    Here's some pseudo code
    Code:
    If Me.checkbox_name = True then
        'Code to be executed if the checkbox is checked
        
        if MsgBox('Are you sure you want to do the thing?", vbYesNo, "Confirm") = vbYes Then
          'user has confirmed, do work...
          Forms!Form2.Field3 = Forms!Form2.Field2
          Forms!Form2.Field2 = Me.Field1  ' Note: [Me] is a shortcut that refers to the form object from which the code has been executed, in this case Form1
        end if
    Else
        'Code to be executed if the checkbox is not checked
    
    End if
    How to refer to objects on different forms:
    http://access.mvps.org/access/forms/frm0031.htm

    Here's a site I just found on google that explains how to use a message box to get user confirmation:
    https://www.excel-pratique.com/en/vba/dialog_boxes

    (EDIT)
    The undo action might be tricky as whatever was in the Form2.Field3 control was presumable previously overwritten. You'll need to save that somewhere, maybe some temporary variable.

  5. #5
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    136
    I got the message box to pop up. Still working on the fields. What if I want nothing done if it's unchecked? How do I code that?

  6. #6
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    I'm of the school of thought that the best way to code something that does nothing is no code at all

    Show us what you got so far and I'm sure we can give tips.

  7. #7
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    136
    Quote Originally Posted by kd2017 View Post
    I'm of the school of thought that the best way to code something that does nothing is no code at all

    Show us what you got so far and I'm sure we can give tips.
    Here's what I have so far. I left your tips in there.

    Private Sub Option201_Click()
    If Me.Option201 = False Then
    'Code to be executed if the checkbox is checked
    Else


    End If


    If MsgBox("This will replace the NVA# on the LAS and CSLT. Do you wish to continue?", vbYesNo, "Confirm") = vbYes Then
    'user has confirmed, do work...

    Forms!frm_Current_State_Labels_Tracking.All_States _Printable_NVA_No = Forms!frm_Current_State_Labels_Tracking.Current_Pr intable_Label_NVA_No
    Forms!frm_Current_State_Labels_Tracking.Current_Pr intable_Label_NVA_No = Me.NVA_Number
    'Note: [Me] is a shortcut that refers to the form object from which the code has been executed
    Else
    'Code to be executed if the checkbox is not checked
    End If


    End Sub

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

Similar Threads

  1. Calculation based on checkbox
    By Athar Khan in forum Programming
    Replies: 11
    Last Post: 06-17-2021, 01:21 PM
  2. Replies: 4
    Last Post: 12-04-2019, 04:30 AM
  3. Replies: 8
    Last Post: 10-07-2018, 05:24 PM
  4. Checkbox based on Combo Box
    By roarcrm in forum Forms
    Replies: 2
    Last Post: 04-07-2015, 10:23 AM
  5. Take action based on open tab
    By swavemeisterg in forum Programming
    Replies: 1
    Last Post: 08-07-2013, 01:35 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