Hello.

Below is the code i use to connect using a recordset to an Access database, the code uses the Select statement to retrieve records which match the Select criteria and then displays the matching records in a Listview.

VB Code:
  1. Option Explicit
  2. Dim objCurrLi As ListItem
  3. Dim InsCoId As String
  4. Dim SumColumb As Double
  5.  
  6. Private Sub cboInsCo_GotFocus()
  7. ListView1.ListItems.Clear      'Clear existing ListView
  8. SumColumb = "0.00"
  9. lblBal.Caption = SumColumb
  10. lblBal.Caption = ""            'Clear Balance Display
  11. End Sub
  12.  
  13. Private Sub cmdLstAcc_Click()
  14.  
  15. Dim TotalCharges As Double
  16. Dim abc As Integer
  17. InsCoId = lblInsCo.Caption
  18.  
  19. ListView1.ListItems.Clear   'Clear existing ListView
  20.  
  21. If cboInsCo.Text = "" Then
  22.    MsgBox "No Insurance Company has been selected.", vbExclamation, "Data Entry Error"
  23.    GoTo abc     'Avoid opening Recordset if no Insurance Company is selected, jump to abc
  24.   Else
  25. End If
  26.  
  27. 'Set up and open Access connection and recordset
  28. Set cn = New ADODB.Connection
  29.     cn.ConnectionString = "Provider =Microsoft.Jet.OLEDB.4.0;" & "Data Source = C:\SKM_ICE Database\SKM_ICE DBase.mdb"
  30.     cn.Open
  31. Set rsAccounts = New ADODB.Recordset
  32.      
  33. rsAccounts.Open "SELECT OurRef, YourRef, Date, insured2, totalcharges" _
  34.      & " From tbl_master" _
  35.      & " Where InsCoId = " & InsCoId & " And PaymentReceived = 'NO'", cn
  36.      
  37. TotalCharges = rsAccounts!TotalCharges
  38.    
  39. 'Populate ListView
  40. Do While Not rsAccounts.EOF
  41.    Set objCurrLi = ListView1.ListItems.Add(, , rsAccounts!ourRef & "") 'Our Ref
  42.         objCurrLi.SubItems(1) = rsAccounts!YourRef & ""            'Your Ref
  43.         objCurrLi.SubItems(2) = rsAccounts!Date & ""                'Date
  44.         objCurrLi.SubItems(3) = rsAccounts!Insured2 & ""           'Insured2
  45.         objCurrLi.SubItems(4) = rsAccounts!TotalCharges & ""     'Total charges
  46.    SumColumb = SumColumb + rsAccounts!TotalCharges
  47.    rsAccounts.MoveNext
  48. Loop
  49.  
  50. 'Close Access connection and recordset
  51. rsAccounts.Close
  52. cn.Close
  53. Set rsAccounts = Nothing
  54. Set cn = Nothing
  55.    
  56. lblBal.Caption = SumColumb
  57. lblBal.Caption = Format(lblBal, "####0.00")
  58.  
  59. abc:  ' Jumps to here from GoTo statement if cboInsCo.Text = ""
  60. End Sub
  61.  
  62. Private Sub cmdClose_Click()   'Return to Report screen
  63. ListView1.ListItems.Clear         'Clear existing ListView
  64. frmOtstngAcc.Hide
  65. Unload Me
  66. End Sub

The above code works fine as long as there are matching records in the database, if no matching records are found the following error message appears, "Runtime error 3021: Either BOF or EOF is true, or the current record has been deleted".

What needs adding to the code or how does the Select statement need modifying to avoid this error, if no matching record is found i would like a Message box to state that no records were found.

Your help would be much appreciated as i have been working on this problem for a while with no success.

Thanks,
SKM.