PDA

View Full Version : Userform: How do I disallow "save & exit" if requried fields are not filled-in?



gonecoding
11-25-2016, 10:06 AM
Hello All,

On a userform I have 2 radio/option buttons ("No" and "Yes"), along with 2 textboxes ("FolderName" and "FileName"). I also have 2 command buttons ("Save & exit" and "Exit without saving").

If "Yes" is selected (checked on) then I would like to ensure that both of the textboxes have been filled-in, in order for the user to be able to successfully "Save & exit" the form. They should be allowed to select the "Exit without saving" button though.

What do I need to do in order to create the loop that will ensure that: as long as the "Yes" radio-button is selected, both foldername and filename values must be entered, in order to save and close the form?

I believe it might be something simple (and perhaps I've done this years back) but I just cannot remember how/what to do.

Here's the code that I have thus far:Code for the "Yes" radio-button clicked:

Code for the "Yes" radio-button clicked:


Private Sub optCreatePDFversionYES_Click()
Me.framePDFversionDetails.Enabled = True

Me.txtPDFversionFolderName.Enabled = True
Me.txtPDFversionFileName.Enabled = True

Me.lblPDFversionFolderName.Enabled = True
Me.lblPDFversionFileName.Enabled = True
End Sub


Code for the "Save & exit" command-button:


Private Sub cmdSaveAndExit_Click()
If Me.optCreatePDFversionYES.Value = True Then
If Me.txtPDFversionFolderName.Value = "" Or Me.txtPDFversionFileName.Value = "" Then

If Me.txtPDFversionFolderName.Value = "" Then
Beep
MsgBox "You must enter a folder name for the PDF file!", vbInformation, "Please note:"
Me.txtPDFversionFolderName.SetFocus
Else
Beep
MsgBox "You must enter a file name for the PDF file!", vbInformation, "Please note:"
Me.txtPDFversionFileName.SetFocus
End If
Exit Sub
End If
End If
End Sub


Thanks

mikerickson
11-25-2016, 10:12 AM
You could do something like


Private Sub OptionButtonYes_Change()
butSaveAndExit.Enabled = True
If OptionButtonYes.Value Then
If tbxFolderName = "" Or tbxFileName = "" Then
butSaveAndExit.Enabled = False
End If
End If
End Sub

Private Sub tbxFolderName_Change()
butSaveAndExit.Enabled = True
If OptionButtonYes.Value Then
If tbxFolderName = "" Or tbxFileName = "" Then
butSaveAndExit.Enabled = False
End If
End If
End Sub

Private Sub tbxFileName_Change()
butSaveAndExit.Enabled = True
If OptionButtonYes.Value Then
If tbxFolderName = "" Or tbxFileName = "" Then
butSaveAndExit.Enabled = False
End If
End If
End Sub

Private Sub Userform_Intialize
butSaveAndExit.Enabled = False
End Sub

SamT
11-25-2016, 12:02 PM
Yet another one of the many ways to do it:

Option Explicit

Private Sub Userform_Intialize()
butSaveAndExit.Enabled = False
End Sub

Private Sub OptionButtonYes_Change()
If OptionButtonYes = True Then
If tbxFolderName <> "" And tbxFileName <> "" Then butSaveAndExit.Enabled = True
End If
End Sub

Private Sub tbxFolderName_Change()
If OptionButtonYes= True Then
If tbxFolderName <> "" And tbxFileName <> "" Then butSaveAndExit.Enabled = True
End If
End Sub

Private Sub tbxFileName_Change()
If OptionButtonYes= True Then
If tbxFolderName <> "" And tbxFileName <> "" Then butSaveAndExit.Enabled = True
End If
End Sub

And as a final check, just to make sure the User did not delete the value in a TextBox:

Private Sub butSaveAndExit_Click()
If Not (OptionButtonYes = True And If tbxFolderName <> "" And tbxFileName <> "" ) Then
butSaveAndExit.Enabled = False
Exit Sub
End If

'Save and exit code here
End Sub

gonecoding
11-25-2016, 12:25 PM
...for providing me with the required code/solution to my problem.

I have been working with Mike's code (for the past few hours)...essentially trying to get it to be more precise for the users i.e. if they do not enter a value in the "folder" textbox then an appropriate msg is displayed and they are taken to that specific (empty) field. Mike's code works just great "as-is", but the user may not know where the problem lies...since I do have many more such radio-buttons and textboxes.

I guess the problem/complexity is that I am disabling the textboxes (in form design itself, as well as when the "No" radio-button is selected), and enabling them when the "Yes" radio-button is clicked...but that keeps throwing an error saying that "focus cannot be set for an item that's either disabled, not visible etc. etc."

I will post back (either later today, or perhaps on Monday) if I should have further questions/problems.

Thanks again.

mikerickson
11-25-2016, 03:06 PM
With my code, you shouldn't need to change the focus.
The Save&Exit button can't be clicked (i.e. is disabled) unless the conditions are right.
Clicking the No option button will enable that button.
I suspect that what you might want to do is, when they click the Yes option button, they can be taken to the text box that is empty.


Private Sub OptionButtonYes_Change()
butSaveAndExit.Enabled = True
If OptionButtonYes.Value Then
If tbxFolderName = "" Or tbxFileName = "" Then
butSaveAndExit.Enabled = False
End If
If tbxFolderName.Text = "" Then
tbxFolderName.SetFocus
ElseIf tbxFileName.Text = "" Then
tbxFileName.SetFocus
End If
End If
End Sub

You could add something similar to the AfterUpdate events for the textoboxes.