LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default UserForms, mandatory completion of fields

Thank you very much Dave!

"Dave Peterson" wrote:

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

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
mandatory fields dawie Excel Worksheet Functions 1 December 27th 06 06:49 PM
Excel Spreadsheet - Mandatory fields Mike in Philly Excel Worksheet Functions 1 April 7th 06 03:45 PM
UserForms - making responses mandatory Zani Excel Programming 5 March 9th 06 04:42 PM
Creating mandatory fields(cells)... SeattleKurt Excel Worksheet Functions 1 August 31st 05 09:53 PM
Can I make cell completion mandatory in excel? Peter Green Excel Discussion (Misc queries) 3 December 23rd 04 03:29 PM


All times are GMT +1. The time now is 02:35 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"