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
 
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
Dave, I greatly appreciate the continued help, however the error is still popping up on this portion of the code.
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Dave, I greatly appreciate the continued help, however the error is still popping up on this portion of the code.

A bit messy but see if this resolves the issue but if not, helpful if could place copy of your workbook with dummy data in a file sharing site like dropbox & provide a link to it.

VBA Code:
Sub AllComplete()
    Dim ctrl As Variant
    For Each ctrl In Array(Me.findings, Me.failure, Me.completiontime)
     With Me.btnOk
        .Enabled = Len(ctrl.Value) > 0
        If Not .Enabled Then Exit For
     End With
    Next ctrl
End Sub

Dave
 
Upvote 0
A bit messy but see if this resolves the issue but if not, helpful if could place copy of your workbook with dummy data in a file sharing site like dropbox & provide a link to it.

VBA Code:
Sub AllComplete()
    Dim ctrl As Variant
    For Each ctrl In Array(Me.findings, Me.failure, Me.completiontime)
     With Me.btnOk
        .Enabled = Len(ctrl.Value) > 0
        If Not .Enabled Then Exit For
     End With
    Next ctrl
End Sub

Dave
Thanks Dave. The problem still persists. The link to the file at Dropbox is below. Please see my notes below the link for details.

2023 RMA V2.7.14_Dummy File.xlsm

On the Dashboard, please enter 22-432 into the box at the top of the page. Once entered, if you click on the button labeled "Complete RMA" it will pop up the userform. I begin entering values and usually the message pops up on my VB screen when I get to the field called "Findings". If it doesn't pop up, I hit cancel and try again. It sometimes takes a few tries to get the error to pop up. DO NOT PRESS OK button or it will change a field in the spreadsheet and prevent you from getting back to the userform. Again, I usually just hit cancel and try again. If it doesn't pop up within about 8-12 tries I would assume it is fixed.
 
Upvote 0
Hi,
Your control Failure included in the AllComplete code is a listbox - solutions provided were based on your original post suggesting all were textboxes.

See if this update now resolves

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

As you can see, control Failure requires a different test to the textboxes to establish if a selection / entry has been made.

Hope Helpful

Dave
 
Upvote 0
Solution
Hi,
Your control Failure included in the AllComplete code is a listbox - solutions provided were based on your original post suggesting all were textboxes.

See if this update now resolves

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

As you can see, control Failure requires a different test to the textboxes to establish if a selection / entry has been made.

Hope Helpful

Dave
Dave, Thank you. This resolved the issue.

Just for reference, I did mention on the original post that one of them was a listbox, however I may not have done it clearly enough. Thank you again for your help, it was GREATLY Appreciated.

I have one other Item I am not sure how to set up so I will do a new post on that topic. Thanks again!
 
Upvote 0
Dave, Thank you. This resolved the issue.
I did mention on the original post that one of them was a listbox

so you did - I should have read the whole post - sorry about that!

As a tip, it's good that you have given your controls appropriate names but may want to consider applying a naming convention that identifies the control type

for example a you would name the control lstFailure which indicates that it is a listbox.

You can read more here: User Form object naming conventions

Glad now resolved & appreciate feedback

Dave
 
Upvote 0
so you did - I should have read the whole post - sorry about that!

As a tip, it's good that you have given your controls appropriate names but may want to consider applying a naming convention that identifies the control type

for example a you would name the control lstFailure which indicates that it is a listbox.

You can read more here: User Form object naming conventions

Glad now resolved & appreciate feedback

Dave
That is great feedback. I will use that going forward. thank you!
 
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,509
Members
449,089
Latest member
RandomExceller01

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