Results 1 to 6 of 6

Thread: [RESOLVED] VB/Access SELECT statement problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2003
    Location
    Stourbridge, England, UK
    Posts
    171

    Resolved [RESOLVED] VB/Access SELECT statement problem

    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.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: VB/Access SELECT statement problem

    It's this line: TotalCharges = rsAccounts!TotalCharges that is your problem... when there are no records, the field will not exist....

    You'll need to check to make sure it isn't EOF and BOF before trying to get the data

    VB Code:
    1. If Not (rsAccounts.EOF And rsAccounts.BOF) Then
    2.   TotalCharges = rsAccounts!TotalCharges
    3. Else
    4.   TotalCharges = 0
    5. End If


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2003
    Location
    Stourbridge, England, UK
    Posts
    171

    Re: VB/Access SELECT statement problem

    Hello,
    Sorry its took me this long to get back with a reply.

    I altered the code as you suggested and it worked fine, thanks.

    I also have a problem with the ListView, when i use the code posted on the first question the ListView populates with the correct data but it does not seem to be in any order. The first column "OurRef" needs to be in ascending number order where as at present it is in no specific order.

    Any ideas,

    Thanks again for the original reply,
    SKM.

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: VB/Access SELECT statement problem

    You need to add an ORDER BY clause to the SQL statement for your recordset. This clause will sort the rs by the specified field.
    VB Code:
    1. rsAccounts.Open "SELECT OurRef, YourRef, Date, insured2, totalcharges" _
    2.      & " From tbl_master" _
    3.      & " Where InsCoId = " & InsCoId & " And PaymentReceived = 'NO'"
    4.      & " Order By OurRef", cn
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2003
    Location
    Stourbridge, England, UK
    Posts
    171

    Re: VB/Access SELECT statement problem

    Hello,

    Thanks for your reply, I tried it and it works great.

    Thanks again, it is much appreciated,

    SKM.

  6. #6
    Junior Member
    Join Date
    Mar 2006
    Location
    India
    Posts
    23

    Thumbs up Re: [RESOLVED] VB/Access SELECT statement problem

    Well you can also use the RecordCount Property. The code is like this....

    If rsAccounts.RecordCount=0 Then
    Msgbox("No such records")
    exit sub()
    else
    continue rest of the code here

    ..............Hope this will work for you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width