Pro VB 6For advanced Visual Basic coders working in version 6 (not .NET). Beginning-level questions will be redirected to other forums, including Beginning VB 6.
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Pro VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
I have run into a problem. I select a user from a listview by double clicking and it displays a small form. Within this form you can change the details in text boxes. When the user clicks OK I want to update the database and then close the window and update the listview on the original page.
I have written the code and it works except one little detail. The listview does not refresh. The refresh command works but it doesn't display the new updated data. However after several seconds, my manual refresh shows the new data. What I want to do is make it instantaneous, so as soon as the OK is clicked the new information is displayed.
Code:
Private Sub Command2_Click()
Call UpdateDBGroup
frmDirectory.ListView1.ListItems.Clear
Unload Me
End Sub
Private Sub UpdateDBGroup()
Dim UpdateGroup As Database
Dim strSQL As String
strSQL = "SELECT * FROM Groups WHERE Name = '" & rsGroupSelected & "';"
Set UpdateGroup = OpenDatabase(App.Path & "\dispatcher.bdp", False, False, ";Pwd=1354513")
strSQL = "UPDATE Groups SET" _
& " [FullName] = '" & Trim(Text1.Text) & "'," _
& " [Channel] = " & Trim(Text2.Text) & "," _
& " [Description] = '" & Trim(Text3.Text) & "'" _
& " WHERE Name = '" & rsGroupSelected & "';"
UpdateGroup.Execute (strSQL)
' Dim strUpdCn As ADODB.Connection
' Set strUpdCn = New ADODB.Connection
' strUpdCn.Open strCn
'
' Dim strSQL As String
' strSQL = "UPDATE Groups SET" _
' & " [FullName] = '" & Trim(Text1.Text) & "'," _
' & " [Channel] = " & Trim(Text2.Text) & "," _
' & " [Description] = '" & Trim(Text3.Text) & "'" _
' & " WHERE Name = '" & rsGroupSelected & "';"
' strUpdCn.Execute (strSQL)
End Sub
as you can see I have been playing with two different methods both with the same result.
The user details form is opened modally and upon closing it runs a function to refresh the listview.
I have toyed with ideas of DBEngine.Idle to delay the listview refresh until the database engine has finished the operation but I still want the change instantly.
Any suggestions you might have will be greatfully received
I Paul have had similar problems where new records that have been added do not show up straight away.
The only way that I ever found to beat this was that when you have added the new item, before refreshing the list, close the current database connection and re open another one. This will then include the new record.
Thanks for that tip. However, I can't seem to get it to work. Is it possible that you could send me a bit of example code on how you achieved the correct result.
A function is called as soon as the form is closed, and that function is as follows.
Code:
Private Sub LoadGroups()
Dim strSQL2 As String
Dim rsPopulate As New ADODB.Recordset
Dim li As ListItem
strSQL2 = "SELECT * FROM Groups ORDER BY Name"
rsPopulate.Open strSQL2, strCn, adOpenStatic, adLockBatchOptimistic
Do Until rsPopulate.EOF
Set li = ListView1.ListItems.Add(, , rsPopulate!Name & "", 1, 5)
li.SubItems(1) = rsPopulate!FullName
li.SubItems(2) = rsPopulate!Description
rsPopulate.MoveNext
Loop
rsPopulate.Close
Set rsPopulate = Nothing
End Sub
If strCn is your current open connection. Is that the connection that was used to update the record details.
if is is when you close the form try closing strCn and re-opening it before opening the recordset. The newly opened connection shuld then pick up the changes.
OR define a new connection as below
Private Sub LoadGroups()
Dim strSQL2 As String
Dim rsPopulate As New ADODB.Recordset
Dim li As ListItem
Dim cnn as new ADODB.Connection
cnn.open (connection string)
strSQL2 = "SELECT * FROM Groups ORDER BY Name"
rsPopulate.Open strSQL2, cnn, adOpenStatic, adLockBatchOptimistic
Do Until rsPopulate.EOF
Set li = ListView1.ListItems.Add(, , rsPopulate!Name & "", 1, 5)
li.SubItems(1) = rsPopulate!FullName
li.SubItems(2) = rsPopulate!Description
rsPopulate.MoveNext
Loop
rsPopulate.Close
Set rsPopulate = Nothing
cnn.close
set cnn=nothing
This might be the solution you are looking for in regards to getting your listview control to reload the data after you have delete/add. Took me awhile to find it. Basically you have to fill the table adapter again with the data from your dataset.