PDA

View Full Version : Enable textboxes



paddy69
02-13-2007, 07:50 AM
Whenever a user wants to change his/her personal information, a userform is opened with a listbox containing all fields that can be changed. Based on the user selection in this listbox I want to enable textboxes on a different userform.
Below you will find the values for the listbox. How is it possible to, for instance, enable multiple textboxes when a user selects more than one option in the listbox (lstChangesToEmployee)?

Sub LoadChanges()
Dim aryChanges
Dim i As Long

aryChanges = Array("Title", "Department", "Country", "Persoonsnr", "Domain", _
"Username", "Contract", "Availability (%)", "Nr hours 100%", "Assigned (Yes/No)", _
"Email", "Phone", "Fax", "Mobile")
With frmChangeToEmployee.lstChangeToEmployee
For i = 0 To UBound(aryChanges)
.AddItem aryChanges(i)
Next i
.ListIndex = 0
End With
End Sub

Edited 14-Feb-07 by geekgirlau. Reason: insert line breaks

fumei
02-13-2007, 08:54 AM
1. Could you please redo your post and move the line breaks a bit? Just redo the underscore a bit more to the left. The code windows is way too wide.

2. I am not quite following. Your code populates the listbox. What has that got to do with the user making selections from that list?

In any case, there are a couple of ways to go about this. here is one.
Private Sub lstChangeToEmployee_Exit( _
ByVal Cancel As MSForms.ReturnBoolean)
Dim ControlToChange()
Dim j As Long
Dim var
On Error Resume Next
For var = 0 To lstChangeToEmployee.ListCount
If lstChangeToEmployee.Selected(var) = True Then
ReDim Preserve ControlToChange(j)
ControlToChange(j) = lstChangeToEmployee.List(var)
j = j + 1
End If
Next
For var = 0 To UBound(ControlToChange())
UserForm1.Controls("txt" & ControlToChange(var)) _
.Enabled = False
Next
UserForm1.Show
End SubI put three textboxes - just for testing - on Userform1. txtTitle, txtEmail, txtPhone.

On the userform frmChangeToEmployee the listbox (lstChangeToEmployee) gets populated as per your code.

After the user makes their multi-selection and leaves the listbox the _Exit event of the listbox fires. It:

1. loops through all the items in the listbox checking it each is selected.

2. if it is selected, the item is added to an array (ControlToChange)

3. as the control name of the other userform is "txtxxxxxx", (eg txtTitle), the array item (eg. "Title - from the listbox) can be appended to "txt" to be able to reference THAT control on the other userform.

In this case, the code makes each of the textboxes named in the array .Enabled = False.

It is all in the naming.

NOTE: notice that the procedure that changes the .Enabled on the other userform also calls that userform. The textboxes WILL be False (in this case, but you can do what you want). However, they are NOT permanently set. They are dynamically set.

In other words, if those textboxes are .Enabled = True, the procedure will make them False for an instance called by the procedure. If that other userform is called (.Show) from another procedure, and those textboxes are originally Enabled = True, then they are still True.

The changes are made to the instance called by the changing procedure.