View Single Post
  #12   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default UserForms, mandatory completion of fields

This code:

OkToEnable = True
For Each myCtrl In Me.Controls
If TypeOf myCtrl Is MSForms.TextBox Then
If myCtrl.Object.Value = "" Then
OkToEnable = False
Exit For
End If
End If
Next myCtrl

is checking all the textboxes on the form--not just the two you want checked.

Just replace it with code that looks at those two textboxes:

if me.txtprojectname.value = "" _
or me.txtprojectdescription.value = "" then
oktoenable = false
else
oktoenable = true
end if

And after you do the work in the cmdAdd procedure, disable the button and change
at least one (both) of those textboxes to "".



mikeg710 wrote:

I am trying to accomplish this task in a user form I created. There are
three text boxes (txtProjectName, txtProjectDescription, txtProcess) and two
command buttons (cmdAdd, cmdCancel).

I want the cmdAdd button enabled only when the user has successfully entered
data into txtProjectName AND txtProjectDescription (required fields). Once
the cmdAdd button is clicked, it is disabled again.

The cmdAdd button never becomes enabled regardless if text is entered or
missing from either of the two required fields. Any thoughts would be
greatly appreciated!

Here is the code:

Option Explicit

Private Sub cmdAdd_Click()
' Disable the Add button after clicking it
cmdAdd.Enabled = False
End Sub

Private Sub cmdCancelButton_Click()
Unload Me
End Sub

Private Sub txtProjectDescription_Change()
Call CheckRequiredFields
End Sub

Private Sub txtProjectName_Change()
Call CheckRequiredFields
End Sub

Private Sub UserForm_Initialize()
' Disable the Add button before form displays
Me.cmdAdd.Enabled = False
End Sub

Private Sub CheckRequiredFields()
Dim myCtrl As Control
Dim OkToEnable As Boolean

OkToEnable = True
For Each myCtrl In Me.Controls
If TypeOf myCtrl Is MSForms.TextBox Then
If myCtrl.Object.Value = "" Then
OkToEnable = False
Exit For
End If
End If
Next myCtrl

Me.cmdAdd.Enabled = OkToEnable

End Sub

"Dave Peterson" wrote:

Another option would be to keep the ok button disabled until all your fields are
ok:

Option Explicit
Private Sub TextBox1_Change()
Call CheckAllRules
End Sub
Private Sub TextBox2_Change()
Call CheckAllRules
End Sub
Private Sub TextBox4_Change()
Call CheckAllRules
End Sub
Private Sub UserForm_Initialize()
Me.CommandButton1.Enabled = False
End Sub
Private Sub CheckAllRules()
Dim myCtrl As Control
Dim OkToEnable As Boolean

OkToEnable = True
For Each myCtrl In Me.Controls
If TypeOf myCtrl Is MSForms.TextBox Then
If myCtrl.Object.Value = "" Then
OkToEnable = False
Exit For
End If
End If
Next myCtrl

Me.CommandButton1.Enabled = OkToEnable

End Sub

Zani wrote:

I have a series of userforms collecting user information. I now want to make
some of the fields on UserForm1 mandatory for completion so I am running the
following code:

Private Sub Userform_queryclose(Cancel As Integer, CloseMode As Integer)

If Len(TextBox1) = 0 Then
MsgBox "please complete all mandatory fields"
Cancel = True

End If

End Sub

However when a mandatory field is missed in userform1 and this code kicks
in, it still shows next Userform that would have been shown had all the
information been collected correctly. At the end of Userform1 I have the
following:

Unload UserForm1
Load UserForm5
UserForm5.Show

How do I stop this next userform from appearing until after all the
mandatory fields have been completed?

--
Zani
(if I have posted here, I really am stuck!)


--

Dave Peterson


--

Dave Peterson