I have the following code in a VB6 project, which is used to update certain fields within selected rows within a datagrid tied to an ADO recordset. We are running into issues where this code seems to run very slowly, taking several seconds just to update one of the records. Does anyone see any issues with this code that would cause it to not run efficiently? Or any suggestions on how to make it run more quickly? One more thing to add, it seems to run slower when there are more results in the data grid, but even when it runs slowly, there are not that many more records. For example it will run very fast when a couple records are updated in a data grid with 50 rows, but will run very slowly in a data grid with 140 rows. Here is the code:
Thank you,
Kevin
Code:
Private Sub cmdMarkSelected_Click()
10 On Error GoTo cmdMarkSelected_Click_Error
20 If cboMarkAs.Text = "" Then
30 MsgBox "You must select the item to be marked as either Paid or Unpaid.", vbCritical, "Paid or Unpaid?"
40 cboMarkAs.SetFocus
50 Exit Sub
60 End If
Dim i As Integer ' Counter
Dim cID As Long
Dim intCount As Integer
Dim iRet As String
70 intCount = DataGrid2.SelBookmarks.Count - 1
80 If intCount > -1 Then
90 iRet = MsgBox("Mark selected item commissions as " & UCase(cboMarkAs.Text) & "?", vbYesNo, "Mark as " & UCase(cboMarkAs.Text))
100 If iRet = vbYes Then 'Yes mark as either Paid or Unpaid depending on the selection made in the form
110 ReDim arrselbk(intCount) 'Declare array to hold bookmarks.
120 For i = 0 To intCount
130 arrselbk(i) = DataGrid2.SelBookmarks(i)
'MsgBox ("array bookmark " + Str(i))
140 Next i
'MsgBox ("Intcount " + Str(intCount))
150 i = i - 1
160 Do While i > -1
'MsgBox (Str(i))
'MsgBox (Str(arrselbk(i)))
170 cID = DataGrid2.Columns(7).CellValue(arrselbk(i))
'MsgBox (Str(cID)) '
180 Adodc3.ConnectionString = glbStrConnect
190 Adodc3.RecordSource = "SELECT paid, datePaid, comment FROM c_CommPaid WHERE ID = " + Str(cID)
200 Adodc3.Refresh
210 If Adodc3.Recordset.RecordCount > 0 Then
220 If cboMarkAs.Text = "Paid" Then
230 Adodc3.Recordset.Fields(2).Value = Trim$(txtComment.Text)
240 Adodc3.Recordset.Fields(1).Value = dtSetPaidDate.Value
250 Adodc3.Recordset.Fields(0).Value = 1
260 Adodc3.Recordset.Update
270 Else
280 Adodc3.Recordset.Fields(2).Value = Trim$(txtComment.Text)
290 Adodc3.Recordset.Fields(1).Value = "1905-06-22 00:00:00.000"
300 Adodc3.Recordset.Fields(0).Value = 0
310 Adodc3.Recordset.Update
320 End If
330 Adodc2.Refresh
340 End If
350 i = i - 1
360 Loop
370 Call Format_DataGrid2
380 End If
390 Else
400 MsgBox "There Are No Items Marked.", vbCritical, "No Items Marked"
410 End If
420 On Error GoTo 0
430 Exit Sub
cmdMarkSelected_Click_Error:
440 MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdMarkSelected_Click of Form frmCommPaid - Error on Line number: " & Erl
End Sub
Thank you,
Kevin