CBC_NS
CBC_NS

Reputation: 1957

Check to see if a control is locked - VBA Excel

I am trying to loop through all of my controls on a single multipage. If a control is locked, I don't want anything to happen to it. if it isn't locked, it's content is to be cleared. I used the following technique in the past, but it won't be effective this time around.

Dim ctl As Control
For Each ctl In Me.MultiPage1.Pages(Me.MultiPage1.Value).Controls
    If TypeName(ctl) = "TextBox" Or TypeName(ctl) = "ComboBox" Then ctl.Value = ""
Next

I know I can check single items and do the following...

If TextBox1.Locked = True Then
    //Set value to ""
End If

That wouldn't do the trick either as I have a lot of input's on this page. Any thoughts as to how I can modify my original technique to accomplish this task?

Upvotes: 1

Views: 1777

Answers (1)

David Zemens
David Zemens

Reputation: 53663

Try this. Just break out your logic a bit more to handle the nuances of different control types. If this also raises an error, let me know which line raises the error and what is the error message.

Dim ctl As Control
For Each ctl In Me.MultiPage1.Pages(Me.MultiPage1.Value).Controls
    Select Case TypeName(ctl) 
        Case "TextBox" 
            If Not ctl.Locked Then ctl.Value = ""
        Case "ComboBox"
            ctl.Value = ""
        Case Else
            'ignore other controls, or modify as necessary
    End Select
Next

Upvotes: 2

Related Questions