+ Reply to Thread
Results 1 to 8 of 8

Loop Statement : Use of string to refer userform object

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-09-2010
    Location
    Constanta
    MS-Off Ver
    Excel 2007
    Posts
    128

    Loop Statement : Use of string to refer userform object

    Greetings,

    In my userform I have several frames (Frames 2-6) that are enabled as per the optionbutton selection (enclosed in Frame1). By default the frames (2-6) are disabled. I tried to write a code where if the optionbutton selection changes any frames that have been previously enabled should be disabled before enabling the frame for the current optionbutton selection.

    I approached the problem by first identifying the name of the enabled frame using the below procedure.

    Private Sub NameEnabledFrame()
        CountFrames
        For i = 2 To iCountFra
            Set Fr = Me.Controls("Frame" & i)
                If Fr.Enabled = True Then
                    sFraName = Fr.Name
                End If
            'Disable all frames to clear memory
            Fr.Enabled = False
        Next
        MsgBox (sFraName)
    End Sub
    The correct frame name is returned irrespective of the order in which I click the optionbuttons. However when I add the line

    Me.Controls(sFraName).Enabled = True
    after the msgbox statement, the correct frame name is returned & hence the enabling takes place only if I click the optionbuttons in ascending order, ie, 1, 2, 5 etc. If I click optionbutton2 after clicking 5, the message box returns frame6 (corresponding to optionbutton5) with frame6 enabled. Frame3 (corresponding to optionbutton2) does not get enabled.

    Why is this happening? Please help.
    Asha


    N.B. Frame1 also has a combobox and textbox whose values will not change with a change in optionbutton selection.
    Last edited by asha3010; 07-10-2010 at 02:27 PM.

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

    Re: Loop Statement : Use of string to refer userform object

    To best describe or illustrate your problem you would be better off attaching a dummy workbook, the workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook.

    If needed supply a before and after sheet in the workbook so the person helping you can see what you are trying to achieve.

    Doing this will ensure you get the result you need!
    Hope that helps.

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

    Free DataBaseForm example

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Loop Statement : Use of string to refer userform object

    your code will return the name of the last frame that was enabled.

    if more than 1 frame was enabled only the last one is stored.
    Cheers
    Andy
    www.andypope.info

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Loop Statement : Use of string to refer userform object

    Why don't you link a checkbox to each specific frame ?

    Private Sub Checkbox1_click()
        Frame1.enabled=checkbox1.value
    End Sub
    
    Private Sub Checkbox2_click()
        Frame2.enabled=checkbox2.value
    End Sub
    If you want only 1 frame enabled at the time you can use optionbuttons


    Private Sub Optionbutton1_Click()
      framecheck
    End Sub
    
    Private Sub Optionbutton2_Click()
      framecheck
    End Sub
    
    Private Sub Optionbutton3_Click()
      framecheck
    End Sub
    
    Private Sub framecheck()
      For j=2 to 6
        Me("Frame" & j).enabled= Me("Optionbutton" & j).value
      Next
    End Sub
    Last edited by snb; 07-09-2010 at 07:53 AM.

  5. #5
    Forum Contributor
    Join Date
    02-09-2010
    Location
    Constanta
    MS-Off Ver
    Excel 2007
    Posts
    128

    Re: Loop Statement : Use of string to refer userform object

    Thanks for your response royUK.

    As recommended I have attached two sample files.

    SampleTestUserform - does not contain the statement

    Me.Controls(sFraName).Enabled = True
    SampleTestUserform2 contains this statement. The code will return the correct frame number and enable the correct frame as long as the optiionbuttons are clicked in ascending order.

    My question is if the messagebox can return the correct name, why will the enabled function not work in the same manner when both are referencing the same variable, ie sFraName.

    Thanks
    Asha
    Attached Files Attached Files
    Last edited by asha3010; 07-09-2010 at 08:13 AM.

  6. #6
    Forum Contributor
    Join Date
    02-09-2010
    Location
    Constanta
    MS-Off Ver
    Excel 2007
    Posts
    128

    Re: Loop Statement : Use of string to refer userform object

    Thanks to all for your response.

    Andy : The code seems to work if optionbuttons are clicked in ascending order. Also, if the messagebox can return the correct name, why will the enabled function not work in the same manner when both are referencing the same variable, ie sFraName.

    snb : Thanks for the alternative approach. I will have a look at it. At the same time I would like to know what is wrong with the current code so that I can learn from it.

    Many thanks
    Asha

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Loop Statement : Use of string to refer userform object

    Disable all frames then set the one associated with the option

    'This code will enable the frame applicable to the option button selection
    Private Sub OptionButton1_Click()
        NameEnabledFrame
        Frame2.Enabled = True
        
    End Sub
    
    Private Sub OptionButton2_Click()
        NameEnabledFrame
        Frame3.Enabled = True
         
    End Sub
    
    Private Sub OptionButton3_Click()
        NameEnabledFrame
        Frame4.Enabled = True
         
    End Sub
    
    Private Sub OptionButton4_Click()
        NameEnabledFrame
        Frame5.Enabled = True
    End Sub
    
    Private Sub OptionButton5_Click()
        NameEnabledFrame
        Frame6.Enabled = True
    End Sub
    Private Sub NameEnabledFrame()
        CountFrames
        For i = 2 To iCountFra
            Me.Controls("Frame" & i).Enabled = False
        Next
    End Sub

  8. #8
    Forum Contributor
    Join Date
    02-09-2010
    Location
    Constanta
    MS-Off Ver
    Excel 2007
    Posts
    128

    Re: Loop Statement : Use of string to refer userform object

    Thanks Andy for your response. Though it took me a while, I finally understood what was wrong with my code.
    Asha

+ 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