|
Question : Refresh contents of a list box displayed on form, using VBA
|
|
I have a simple set of routines that allows me to select multiple MS Project files saved in an Access DB format and delete them. On loading the form (all of this is done in Access) a list box is loaded showing the name of all the MS Project files saved in the DB. I can select multiple ones and delete them from the DB. The problem I having is getting the list box on the form to reload and refresh showing the updated list without the ones I just deleted. Below are the two functions I use - 1st is the procedure called when clicking the "Delete" button; next is the routine I call to reload the list box:
[as you should be able to see, I try the repaint and refresh actions, but neither of these work. I have actually put a debug stop in the coding right after the point where the selected MS Projects are actually deleted from the table and I can confirm that they are deleted. When I do this, then once the load list box routine runs, I get the proper list in the list box. This leads me to believe that there is some time delay between when the deletions are "committed" in the DB that is longer than it takes to requery for the list box info. Thus, for some reason, I'm getting "ghost" records. Don't know if this is the case though. Also, I tried using a Table/Query binding and using the refresh method and got the same result. Plus, I'd like to stick to VBA manipulation of these controls. Any ideas, help are welcome.]
Private Sub cmdDeleteProj_Click() Dim intCount As Integer 'counter Dim varItem As Variant 'for loopping through the items selected in the listbox Dim rtnDelete As Variant 'returns 3 options from the delete dialog box Dim astrDeleteProjects() As String 'array to hold names of selected items If lstDeleteProject.ItemsSelected.Count > 0 Then 'Establish 1) the number of selected items, 2) size the array to the number of selected items, _ 3) fill the array with the MSP_PROJECT!PROJ_NAME value from the selected items intCount = lstDeleteProject.ItemsSelected.Count ReDim astrDeleteProjects(intCount) intCount = 0 For Each varItem In lstDeleteProject.ItemsSelected astrDeleteProjects(intCount) = lstDeleteProject.Column(0) intCount = intCount + 1 Next varItem 'Loop through the array of selected items: 1) ask to delete, if No, ignore and go to next, 2) if Yes, delete _ 3) if Cancel, exit sub and re-load the listbox For intCount = 0 To lstDeleteProject.ListIndex - 1 strFileName = astrDeleteProjects(intCount) strFilePath = Application.CurrentProject.FullName rtnDelete = MsgBox("Are you sure you wish to delete the MS Project file " & vbCr & _ "'" & strFileName & "' from the PMA 202 database", vbYesNoCancel, "Delete Project?") Select Case rtnDelete Case 6 'Yes for Delete DeleteFromDatabase Name:="<" & strFilePath & ">\" & strFileName & "", FormatID:="MSProject.MDB8" Case 2 Call LoadDeleteList btnExit.SetFocus Exit Sub End Select Next intCount Else MsgBox "Please select an MS Project to delete from the database.", vbOKOnly, "Select Project" lstDeleteProject.SetFocus Exit Sub End If Call LoadDeleteList Me.Repaint Me.Refresh btnExit.SetFocus End Sub
Private Sub LoadDeleteList() Dim cnConnection As ADODB.Connection Dim strConnection As String 'Set connection info Set cnConnection = New ADODB.Connection strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & CurrentProject.Path & "\PMA202_Test.mdb;User Id=admin;Password=;" cnConnection.Open strConnection cnConnection.CursorLocation = adUseClient 'set recordset Info Dim rsRecordset As ADODB.Recordset Set rsRecordset = New ADODB.Recordset lstDeleteProject.RowSource = "" 'clear any previous items in the listbox 'get the recordset data With rsRecordset .CursorType = adOpenDynamic .LockType = adLockOptimistic .Open "Select PROJ_ID, PROJ_NAME From MSP_PROJECTS Order By MSP_PROJECTS.PROJ_NAME", CurrentProject.Connection End With rsRecordset.MoveFirst 'load the returned data in the listbox Do While Not rsRecordset.EOF lstDeleteProject.AddItem (rsRecordset!PROJ_NAME) rsRecordset.MoveNext Loop lstDeleteProject.Requery rsRecordset.Close cnConnection.Close Set rsRecordset = Nothing Set cnConnection = Nothing End Sub
|
|
Answer : Refresh contents of a list box displayed on form, using VBA
|
|
What about doing this then:
Me!lstDeleteProject.RowSource = "" Me!lstDeleteProject.Refresh Me!lstDeleteProject.RowSource = "Select PROJ_ID, PROJ_NAME From MSP_PROJECTS Order By MSP_PROJECTS.PROJ_NAME;" Me!lstDeleteProject.Refresh
This should technicaly set the Listbox to blank, refresh, then re-pull in the records and refresh again.
|
|
|
|