+ Reply to Thread
Results 1 to 3 of 3

Refreshing data in listbox on userform fires it's afterupdate event a second time

Hybrid View

  1. #1
    Registered User
    Join Date
    09-24-2016
    Location
    SoCal
    MS-Off Ver
    2013
    Posts
    2

    Refreshing data in listbox on userform fires it's afterupdate event a second time

    Caveat - this is my first post after my introduction post.

    Excel 2013. (VBA) I open a user form from a custom ribbon button. Form has listbox which is populated with service order data from an access database. On clicking on a listbox line item - the listbox_afterupdate event fires running code that in essence updates the selected service order status to "Scheduled" among other stuff. This all works fine.

    What I want to happen is for the listbox data to be refreshed since the criteria for filling the listbox is the service order status is "Unscheduled". I.e., I don't want to have to close and re-open the form to update the data. This used to work, but while working with the code I did something to break it. I get the following error when the after update event runs again - "Runtime error 381...Could not get the column property. Invalid property array index." I understand why the error is there, I just don't want the event to run twice. Here is my code...

    Private Sub listOpenSOUnsched_AfterUpdate()
            '***********************************************************
            'Purpose:'1) enter the SO data into the selected cell ranges,
                    '2) update SO Detail table, 3) update status of SO
                    ' and 4) refresh unscheduled list
                    
                    'AM fixed errors 9/25/16
            '***********************************************************
    
        'On Error GoTo listOpenSOUnsched_AfterUpdate_Err
    
            Dim addr As String, Ct As String
            Dim rg As Range, col As String, rw As String, s As String, strCriteria As String
            Dim cnn As New ADODB.Connection
            Dim rst As New ADODB.Recordset
            Dim rstReset As New ADODB.Recordset
            Dim rstDetail As New ADODB.Recordset
            Dim rstEmp As New ADODB.Recordset
                
                addr = Selection.Address
                Ct = Selection.Areas.Count
    
        'GCOUNT = GCOUNT + 1
        'MsgBox GCOUNT
        
            For Each rg In Range(Selection.Address)
            
               'add the SO data to the Service Order Detail table, including the TechID
                Call Detail_Update(Me.listOpenSOUnsched.Column(0), rg.Row)
                
                'update selected cells data
                If Cells(rg.Row, rg.Column).Value = "" Then  'if cell is empty
                    With Me.listOpenSOUnsched
                        If .Column(9) = "Scheduled Maintenance" Then
                            Cells(rg.Row, rg.Column).Value = .Column(0) & ": SM, " & .Column(1) & "," & .Column(3)
                        Else
                            Cells(rg.Row, rg.Column).Value = .Column(0) & ":" & .Column(9) & ", " & .Column(1) & "," & .Column(3)
                        End If
                    End With
                Else
                    With Me.listOpenSOUnsched   ''if cell is not empty
                        If .Column(9) = "Scheduled Maintenance" Then
                            Cells(rg.Row, rg.Column).Value = Cells(rg.Row, rg.Column).Value & vbNewLine & .Column(0) & ": SM, " & .Column(1) & "," & .Column(3)
                        Else
                            Cells(rg.Row, rg.Column).Value = Cells(rg.Row, rg.Column).Value & vbNewLine & .Column(0) & ": " & .Column(9) & ", " & .Column(1) & "," & .Column(3)
                        End If
                    End With
                End If
            Next rg
    
            'update the status of the scheduled SO
            '*************************************
            strCriteria = "[SO#] = " & listOpenSOUnsched.Column(0)
            cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                     "Data Source=S:\\FE\XXX\Prod\XXXXX.mdb;" & _
                     "Persist Security Info=False;"
            
            rst.Open "SELECT * FROM qryOpenSOs_Update WHERE [SO#] = " & CLng(listOpenSOUnsched.Column(0)), cnn, adOpenDynamic, adLockOptimistic, adCmdText
            With rst
                .Fields("StatusID").Value = 2
                .Update
            End With
            
            MsgBox "Status of SO# " & listOpenSOUnsched.Column(0) & " has been changed to SCHEDULED."
    
            'reset the unscheduled list
            '*************************************
        rstReset.Open "SELECT * FROM qryOpenSOs_NotSched;", cnn, adOpenStatic
        If rstReset.EOF Or rstReset.BOF Then
            MsgBox "There are no more Unscheduled Service orders for the month."
            
            GoTo listOpenSOUnsched_AfterUpdate_Exit
        End If
        
         rstReset.MoveFirst
         i = 0
         With Me.listOpenSOUnsched
             .Clear
             Do
                .AddItem
                
                .List(i, 0) = rstReset![SO#]
                .List(i, 1) = rstReset![Customer]
                
                If Not IsNull(rstReset![MoName]) Then
                    .List(i, 2) = rstReset![MoName]
                Else
                    .List(i, 2) = ""
                End If
                
                If Not IsNull(rstReset![City]) Then
                    .List(i, 3) = rstReset![City]
                Else
                    .List(i, 3) = ""
                End If
                
                If Not IsNull(rstReset![Status]) Then
                    .List(i, 4) = rstReset![Status]
                Else
                    .List(i, 4) = ""
                End If
                
                If Not IsNull(rstReset![MaintType]) Then
                    .List(i, 5) = rstReset![MaintType]
                Else
                    .List(i, 5) = ""
                End If
                
                If Not IsNull(rstReset![Period]) Then
                    .List(i, 6) = rstReset![Period]
                Else
                    .List(i, 6) = ""
                End If
                            
                If Not IsNull(rstReset![# Batts]) Then
                    .List(i, 7) = rstReset![# Batts]
                Else
                    .List(i, 7) = ""
                End If
                
                If Not IsNull(rstReset![EstTechDays]) Then
                    .List(i, 8) = rstReset![EstTechDays]
                Else
                    .List(i, 8) = ""
                End If
                
                If Not IsNull(rstReset![Service Type]) Then
                    .List(i, 9) = rstReset![Service Type]
                Else
                    .List(i, 9) = ""
                End If
                
                i = i + 1
                rstReset.MoveNext
             Loop Until rstReset.EOF
         End With
         
    listOpenSOUnsched_AfterUpdate_Exit:
         On Error Resume Next
         rst.Close
         rstrest.Close
         'rstEmp.Close
         cnn.Close
         Set rst = Nothing
         Set rstDetail = Nothing
         Set rstEmp = Nothing
         Set rstReset = Nothing
         Set cnn = Nothing
         Exit Sub
    listOpenSOUnsched_AfterUpdate_Err:
         MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
         Resume listOpenSOUnsched_AfterUpdate_Exit
    
    End Sub
    Thanks very much.

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

    Re: Refreshing data in listbox on userform fires it's afterupdate event a second time

    Hi and welcome to the forum

    Quote Originally Posted by progom View Post
    On clicking on a listbox line item - the listbox_afterupdate event fires
    Not sure I follow what you are doing.
    Clicking an item in a listbox doesn't trigger its own AfterUpdate event.
    Why are you using the AfterUpdate procedure to update instead of the Click event procedure?

    If you must use the AfterUpdate procedure to update the listbox and only want it to trigger once, you could toggle a Boolean variable and wrap the code in an IF code block so the 2nd time it runs, the update part of the code is ignored. Something like this...
    Private Sub listOpenSOUnsched_AfterUpdate()
        Static bIsUpdated As Boolean
        If Not bIsUpdated Then
            'update list here
            bIsUpdated = True
        Else
            bIsUpdated = False
        End If
    End Sub
    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
    Registered User
    Join Date
    09-24-2016
    Location
    SoCal
    MS-Off Ver
    2013
    Posts
    2

    Re: Refreshing data in listbox on userform fires it's afterupdate event a second time

    AlphaFrog: Click event works great. I knew I was making some simple mistake but couldn't see through the fog.

    I appreciate your help.

    progom

+ 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. [SOLVED] Userform runs Initialize sub @ beginning of textbox AfterUpdate event w/out being called
    By chirp08 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-18-2016, 01:02 PM
  2. VBA refreshing listbox in userform
    By devatu in forum Excel Programming / VBA / Macros
    Replies: 25
    Last Post: 08-12-2015, 04:16 AM
  3. Exiting Excel fires Change Event on ListBox
    By 24601 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-18-2014, 01:14 PM
  4. 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
  5. Refreshing a userform/listbox
    By toocold in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-03-2006, 04:10 PM
  6. PivotField AfterUpdate event?
    By Snowsride in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-11-2006, 09:15 AM
  7. AfterUpdate Event not Running
    By Craig in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 11-06-2005, 06:35 PM

Tags for this Thread

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