+ Reply to Thread
Results 1 to 3 of 3

Trying to return userform after changing source - triggers TextBox Change event

Hybrid View

  1. #1
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,286

    Trying to return userform after changing source - triggers TextBox Change event

    Userform F14_Risk1 has a command button that changes the format to view a different source of data. Everything works as should except when I try to reset the form to its original state. The Select Case event works using the CommandButton3 Caption as choice, but try as I might I cannot get form to revert. In the first case argument why isn't TextBox1_Change event but is if I try later in code.

    Basically I want the form to revert to the first Initialized state but ListBox1.Clear is throwing an error

    Any pointers appreciated


    Private Sub CommandButton3_Click()
    Select Case F14_Risk1.CommandButton3.Caption
    Case Is = "View Risk Assessments"
    
    
        With F14_Risk1
            .ListBox1.Clear
            .TextBox1.Value = "Risk Assessments"
            .ListBox1.RowSource = "RA"
            .CommandButton1.Visible = False
            .CommandButton2.Visible = False
            .CommandButton3.Caption = "Return to Search"
        End With
    
    Case Is = "Return to Search"
    'Call UserForm_Initialize
    Unload Me
    
    
        'With F14_Risk1
            '.CommandButton3.Caption = "Risk Assessments"
            '.CommandButton1.Visible = True
            '.CommandButton2.Visible = True
            'TextBox1.Value = ""
           'End With
        'F14_Risk1.TextBox1.Value = ""
    
        '.Value = "Search Records Here"
        '.SetFocus
        '.SelStart = 0
        '.SelLength = Len(.Text)
    'End With
        'F14_Risk1.ListBox1.Clear ''''''''THIS IS HIGHLIGHTED ON ERROR 
            'With R14_Risk1
                '.CommandButton1.Visible = True
                '.CommandButton2.Visible = True
                '.CommandButton3.Caption = "Risk Assessments"
            'End With
            
    End Select
    
    End Sub
    Private Sub UserForm_Initialize()
    With F14_Risk1.TextBox1
        .Value = "Search Records Here"
        .SetFocus
        .SelStart = 0
        .SelLength = Len(.Text)
    End With
    End Sub
    Private Sub TextBox1_Change()
        Dim a, i As Long, w(), n As Long, strExclude As String
        F14_Risk1.ListBox1.Clear ''''''''THIS IS HIGHLIGHTED ON ERROR 
    
    
        a = ThisWorkbook.Sheets("RISKS").Cells(1).CurrentRegion.Value
        For i = 2 To UBound(a, 1)
                If (UCase$(a(i, 2)) Like "*" & UCase$(F14_Risk1.TextBox1.Value) & "*" Or _
                    UCase$(a(i, 3)) Like "*" & UCase$(F14_Risk1.TextBox1.Value) & "*" Or _
                        UCase$(a(i, 4)) Like "*" & UCase$(F14_Risk1.TextBox1.Value) & "*") Then
    
                n = n + 1
                ReDim Preserve w(1 To UBound(a, 2), 1 To n)
                For ii = 1 To UBound(a, 2)
                    w(ii, n) = a(i, ii)
                Next
            End If
        Next
    
        If n > 0 Then F14_Risk1.ListBox1.Column = w
    
    End Sub
    Attached Files Attached Files
    Last edited by nigelog; 08-09-2017 at 10:47 AM.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Trying to return userform after changing source - triggers TextBox Change event

    The listbox cannot be cleared using .Clear when a RowSource is assigned to it. The code assigns a RowSource when "View Risk Assessment" is first clicked. To remove a RowSource, set it to a nullstring.

    Try this...

        With Me.ListBox1
            If .RowSource = "" Then .Clear Else .RowSource = ""
        End With
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,286

    Re: Trying to return userform after changing source - triggers TextBox Change event

    Perfect AlphaFrog, first time I populated a listbox with a rowsource.....Thought because two column list "easy"

    Caught me for a long time

    Finished now...Many Thanks
    Private Sub CommandButton3_Click()
    Select Case F14_Risk1.CommandButton3.Caption
    Case Is = "View Risk Assessments"
    
    
        With F14_Risk1
            .ListBox1.Clear
            .TextBox1.Value = "Risk Assessments"
            .ListBox1.RowSource = "RA"
            .CommandButton1.Visible = False
            .CommandButton2.Visible = False
            .CommandButton3.Caption = "Return to Search"
        End With
    
    Case Is = "Return to Search"
    
        With Me.ListBox1
            If .RowSource = "" Then .Clear Else .RowSource = ""
        End With
            With Me
                .CommandButton1.Visible = True
                .CommandButton2.Visible = True
                .CommandButton3.Caption = "Risk Assessments"
                    .TextBox1.Value = "Search Records Here"
                    .TextBox1.SetFocus
                    .TextBox1.SelStart = 0
                    .TextBox1.SelLength = Len(TextBox1.Text)
                        
            End With
    End Select
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. cant keep typing in userform textbox after change event
    By jed38 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-01-2014, 09:13 AM
  2. [SOLVED] Dynamic Range triggers Combobox Change event 3 times
    By natefarm in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-25-2013, 10:51 AM
  3. Replies: 3
    Last Post: 05-07-2012, 09:46 PM
  4. Userform Textbox Exit Event to Run Application
    By baisty182 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-29-2010, 12:30 PM
  5. Userform textbox event that fires after I exit the textbox
    By jerseyguy1996 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 02-04-2010, 08:08 AM
  6. Filter change triggers event?
    By ckiraly in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-26-2007, 03:54 PM
  7. Textbox Change event
    By Kerno in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-03-2007, 05:08 AM

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