+ Reply to Thread
Results 1 to 5 of 5

VBA - if control is not locked

Hybrid View

GregJG VBA - if control is not locked 12-16-2008, 07:01 AM
royUK Try amending your code like... 12-16-2008, 07:20 AM
GregJG Thanks again for the help... 12-16-2008, 08:21 AM
royUK If you are satisfied with the... 12-16-2008, 08:25 AM
romperstomper If you can only unlock the... 12-16-2008, 09:21 AM
  1. #1
    Registered User
    Join Date
    06-24-2004
    Posts
    78

    VBA - if control is not locked

    I have a userform that when initialized each textbox is locked
    the form has a commandbutton(edit) to unlock all textboxes
    when the user closes the userform if the textboxes are unlocked i want it ask to save any changes.

    I am getting a typemismatch on this line
        For Each oCtrl In Me.Controls
    the whole code is below. does anyone know why?

    Private Sub CommandButton1_Click()
    Dim oCtrl As MSForms.TextBox
    For Each oCtrl In Me.Controls
        If oCtrl.Locked = False Then
            If MsgBox("You have the form in EDIT MODE, do you want to save any changes?", vbYesNo, Title:="SAVE CHANGES?") = vbYes Then
                
                
                
                
            ElseIf vbNo Then
                Unload Me
                Set Me = Nothing
            End If
        ElseIf oCtrl.Locked = True Then
            Unload Me
            Set Me = Nothing
        End If
    Next oCtrl
    End Sub

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Try amending your code like this

    Dim oCtrl As MSForms.Control
    For Each oCtrl In Me.Controls
      If TypeOf oCtrl Is MSForms.TextBox And oCtrl.Locked = False Then
    'etc
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    06-24-2004
    Posts
    78
    Thanks again for the help Roy!!

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,166
    If you can only unlock the textboxes via the button, why not just have a module level boolean variable that you set to True when the Unlock button is pushed? You can then simply check that value when the form closes instead of looping the controls.
    Just a thought.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1