Results 1 to 6 of 6
  1. #1
    Ant_Snell is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2021
    Location
    UK
    Posts
    118

    VBA to change command button state

    I have a form with a number of command buttons (let’s call them command1, 2, 3) that need to be clicked in order. This will open another form (for some checks to be done) and close the current form. When the checks have been done ‘On Close’ of that form will re-open the form containing the buttons so that the user can move onto the next button and so on until all checks have been done.
    In order to prevent the user clicking the buttons out of order, I would like to make all bar ‘command1’ inactive, after clicking command2, command2 becomes active command1 remains active but the back colour changes to indicate it as been clicked. This would continue until all buttons have been clicked but then return to the default of all bar command1 inactive ready for the next time the process is worked through.
    My questions are:
    What is the VBA code to make the button active/inactive?
    What is the VBA code to change the back colour of the button?
    What is the best place to put the code e.g. on click of button, on open of new form, of close of form etc. so that at the end of the process it will go back to the default?



    Thanks for any help

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,001
    In VBA you can disable/enable a button simply using

    Code:
    Me.MyCmdButton1.Enabled = False  ' (or True)
    So in your form design set all but button 1 to enabled = No in it's properties and save the form.
    You will notice that a disabled button is greyed out by default.

    Now in you form code enable the other buttons when you need when the appropriate conditions are met based on your process.

    Me.MyCmdButton1.BackColor = VbGreen

    Will change the back colour.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,861
    I would like to make all bar ‘command1’ inactive, after clicking command2, command2 becomes active
    I hope you mean
    I would like to make all bar ‘command1’ inactive, after clicking command1, command2 becomes active
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    Ant_Snell is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2021
    Location
    UK
    Posts
    118
    Welshgasman: Well spotted, sorry my error.

    Minty: Thankyou the enabled=True & BackColour = VBGreen is now working independently however because I am then closing the form then reopening it after doing the checks on the other form. It resets its self back to all buttons disabled bar Command1. I am thinking that maybe I need to hide or make the forms properties visible=False rather than closing it, then unhide when checks on other form are done. But I cant find a visible property for the form

  5. #5
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,001
    There isn't form property for that, however in VBA you can use

    Me.Form.Visible = False
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  6. #6
    Ant_Snell is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2021
    Location
    UK
    Posts
    118
    Perfect Thankyou!

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

Similar Threads

  1. Replies: 3
    Last Post: 01-26-2018, 03:25 AM
  2. Replies: 3
    Last Post: 06-03-2015, 10:16 AM
  3. Command button to change value of other field
    By teirrah1995 in forum Programming
    Replies: 8
    Last Post: 09-09-2010, 10:23 AM
  4. Replies: 1
    Last Post: 07-27-2010, 02:27 PM
  5. Replies: 2
    Last Post: 03-10-2009, 05:14 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