Link to home
Start Free TrialLog in
Avatar of BFanguy
BFanguyFlag for United States of America

asked on

Access 2010 looping through controls and disabling / locking / enabling / unlocking

I have this code below on a Forms On Current event called from a select

Select Case Me.Level
Case "DRL1"
   ProtectFields ("01")
Case "DRL2"
   ProtectFields ("02")
end select

Public Sub ProtectFields(ColumnNum As String)
Dim Ctl As Control
For Each Ctl In Forms!Sales_Contract_Review.Controls
' make sure i am looking at the correct textboxes
   If Ctl.Parent.Name = "Sales_Contract_Review" And IsNumeric(Mid(Ctl.Name, 1, 2)) And Mid(Ctl.Name, 3, 1) = "-" And IsNumeric(Mid(Ctl.Name, 4, 2)) Then
' protect all fields except ones with the passed variable in column 4-5
      If Mid(Ctl.Name, 4, 2) <> ColumnNum Then
 '        msgbox "lock:" & Ctl.Name
         Ctl.Enabled = False
         Ctl.Locked = True
      Else
 '        msgbox "unlock: " & Ctl.Name
         If Ctl.Value <> "M" Then
            Ctl.Enabled = True
            Ctl.Locked = False
         End If
      End If
   End If
Next
End Sub

Open in new window


it works as expected, all textboxes are locked except the ones i want.

i add the same code to an on afterupdate of an option box:
Select Case LevelOptions
Case 1
   ProtectFields ("01")
   Me.Ctl04_01_PC.SetFocus
Case 2
   ProtectFields ("02")
   Me.Ctl02_02_Nace.SetFocus
End Select

But this gives me unexpected results.  all fields stay protected AND the labels assocated to the textboxes gets grayed out (disabled) AND it fails on the SetFocus

any ideas?
SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
can you post your access file with this form. the expectation is confusing
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BFanguy

ASKER

I am enabling and unlocking before I set focus  the code works fine on current but does not unlock or enable the fields when the on after update of the field fires
Did you try the modification I suggested?

Also, are you sure the code is actually running?  Try adding a message box at the beginning of the Option Group's after update event procedure, just as a test to verify that the code is running:

Private Sub LevelOptions_AfterUpdate()
          MsgBox "The code reached the After Update Event"

Select Case LevelOptions
Case 1
   ProtectFields ("01")
   Me.Ctl04_01_PC.SetFocus
Case 2

Open in new window


If the message box is not displayed, verify that the words [Event Procedure] are displayed in the box next to After Update in the option group's property sheet.  If they are not, click the ... next to After Update in the property sheet to 'connect' the event procedure to it's code.
Avatar of BFanguy

ASKER

thanks, I added your code to make sure only textboxes were selected.
I uncommented my two message boxes and when the form opens ("on current") I can see which ones it disables and which ones it enables,
same for "afterUpdate", it loops through and shows me the fields it is enabling but it does not actually enable the fields, after looping through the fields.

Ok, I just noticed it ONLY works for "On current" for the first record opened on the form (which is filtered for 1 record), if I remove the filter it does the same thing as the afterupdate, it loops through the fields Supposedly enabling some of them and disabling the rest, but they all are disabled.
Avatar of BFanguy

ASKER

I uncommented out the Ctl.Enabled  = true and Ctl.Enabled  = false and it only works when the form is opened for the first "OnCurrent" event.
Avatar of BFanguy

ASKER

Evidently there is something wrong with my code for "unlocking" fields using the Control.
i.e.  it lockes but wont unlock the textboxes.

do I need to do something else before the Ctl.Locked = False like setfocus???
<do I need to do something else before the Ctl.Locked = False like setfocus??? >

no.. you only need to set focus to other enabled controls when you are disabling a certain control
Avatar of BFanguy

ASKER

looks like it is Null values, I changed the code from comparing .value <> "M" to the following:

Public Sub ProtectFields(ColumnNum As String)
Dim Ctl As Control
For Each Ctl In Forms!Sales_Contract_Review.Controls
   If Ctl.ControlType = acTextBox And Ctl.Parent.Name = "Sales_Contract_Review" And IsNumeric(Mid(Ctl.Name, 1, 2)) And Mid(Ctl.Name, 3, 1) = "-" And IsNumeric(Mid(Ctl.Name, 4, 2)) Then
      If Mid(Ctl.Name, 4, 2) <> ColumnNum Then
'        msgbox Ctl.Name
         Ctl.Enabled = False
         Ctl.Locked = True
      Else
'         msgbox "unlock: " & Ctl.Name
         If Ctl.Value = "M" Then
            Ctl.Enabled = False
            Ctl.Locked = True
         Else
           Ctl.Enabled = True
           Ctl.Locked = False
         End If
      End If
   End If
Next
End Sub

Open in new window

Avatar of BFanguy

ASKER

I gave you guys the points, thanks for the help.