Enabling a Button based on multiple Changes to Userform

Hitman51

New Member
Joined
Nov 11, 2022
Messages
16
Office Version
  1. 365
Platform
  1. Windows
0
I am trying to keep a button disabled until all required fields are completed. I have the button disabled at initialize and I can get it to enable with one field completed using the code below. How do I add the other fields that are required to be completed?

VBA Code:
`Private Sub Findings_Change()
    If Findings.Value <> "" Then
        btnOk.Enabled = True
        Else: btnOk.Enabled = False
    End If
End Sub`


I tried the following, but it enables the button after one field is completed and not all three. Note Findings and CompletionTime are textboxes in the userform, Failure is a listbox. Please HELP!

VBA Code:
Private Sub Findings_Change()
    If Findings.Value <> "" Then
        btnOk.Enabled = True
        Else: btnOk.Enabled = False
    End If
End Sub
Private Sub Failure_Change()
    If Failure.Value <> "" Then
        btnOk.Enabled = True
        Else: btnOk.Enabled = False
    End If
End Sub
Private Sub CompletionTime_Change()
    If CompletionTime.Value <> "" Then
        btnOk.Enabled = True
        Else: btnOk.Enabled = False
    End If
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Add a command button to the userform and copy/paste the macro below to the userform code module. Change the line in red to add the desired items to the ListBox.
Rich (BB code):
Private Sub CommandButton1_Click()
    If Findings.Value <> "" And Failure.Value <> "" And CompletionTime.Value <> "" Then
        btnOK.Enabled = True
    Else
        btnOK.Enabled = False
    End If
End Sub

Private Sub UserForm_Initialize()
    CompletionTime.AddItem "test"
    btnOK.Enabled = False
End Sub
After you enter values in Findings and CompletionTime and make a selection in Failure, just click the command button. You don't need the code you posted.
 
Upvote 0
Hi,
If you want to change the enabled status of your commandbutton based on completion of certain controls then just make a common code & have each of the controls events call it

Place all codes in your userforms code page

VBA Code:
Private Sub Findings_Change()
    AllComplete
End Sub
Private Sub Failure_Change()
    AllComplete
End Sub
Private Sub CompletionTime_Change()
    AllComplete
End Sub
Private Sub UserForm_Initialize()
    AllComplete
End Sub

Sub AllComplete()
    Me.btnOk.Enabled = CBool(Me.findings.Value <> "" And _
    Me.failure.Value <> "" And Me.completiontime.Value <> "")
End Sub

Dave
 
Upvote 0
Hi,
If you want to change the enabled status of your commandbutton based on completion of certain controls then just make a common code & have each of the controls events call it

Place all codes in your userforms code page

VBA Code:
Private Sub Findings_Change()
    AllComplete
End Sub
Private Sub Failure_Change()
    AllComplete
End Sub
Private Sub CompletionTime_Change()
    AllComplete
End Sub
Private Sub UserForm_Initialize()
    AllComplete
End Sub

Sub AllComplete()
    Me.btnOk.Enabled = CBool(Me.findings.Value <> "" And _
    Me.failure.Value <> "" And Me.completiontime.Value <> "")
End Sub

Dave
Thank you so much Dave. This worked perfectly!
 
Upvote 0
most welcome glad we were able to help & appreciate your feedback

Dave
Dave,

Although this is working, every once in a while I get "Runtime Error 94 - Invalid Use of Null" on the last piece of code.

VBA Code:
Sub AllComplete()
    RMAClose.btnOk.Enabled = CBool(RMAClose.Findings.Value <> "" And _
    RMAClose.Failure.Value <> "" And RMAClose.CompletionTime.Value <> "")
End Sub

Can you advise a way to prevent this?
 
Upvote 0
Dave,

Although this is working, every once in a while I get "Runtime Error 94 - Invalid Use of Null" on the last piece of code.

can only hazard a guess why but see if this update resolves

VBA Code:
Sub AllComplete()
    Me.btnOk.Enabled = Me.findings.Value <> "" And _
    Me.failure.Value <> "" And Me.completiontime.Value <> ""
End Sub

Note: Me keyword - if you have placed code in your userforms code page ME replaces need to hard code the controls with the userform name.

Dave
 
Upvote 0
can only hazard a guess why but see if this update resolves

VBA Code:
Sub AllComplete()
    Me.btnOk.Enabled = Me.findings.Value <> "" And _
    Me.failure.Value <> "" And Me.completiontime.Value <> ""
End Sub

Note: Me keyword - if you have placed code in your userforms code page ME replaces need to hard code the controls with the userform name.

Dave
 
Upvote 0
Unfortunately it is still happening. It is always when I start typing in the "Findings" textbox on the userform.

VBA Code:
    Me.btnOk.Enabled = Me.Findings.Value <> "" And _
    Me.Failure.Value <> "" And Me.CompletionTime.Value <> ""
 
Upvote 0
Unfortunately it is still happening. It is always when I start typing in the "Findings" textbox on the userform.

Try this update

VBA Code:
Sub AllComplete()
    Me.btnOk.Enabled = Len(Me.findings.Value) > 0 And _
    Len(Me.failure.Value) > 0 And Len(Me.completiontime.Value) > 0
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,214,847
Messages
6,121,911
Members
449,054
Latest member
luca142

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top