Results 1 to 15 of 15

Thread: Access SQL /VB6 connection help needed

  1. #1

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

    Access SQL /VB6 connection help needed

    Hi.
    Im having a problem with an SQL statement.
    Using VB6 and connecting to an Access 2000 database using an ADO Recordset i keep getting errors.
    The database has 1 table named tbl_Master, from that table i need to select th following fields, ourref, yourref, date, insured2, totalcharges, insurancecompany and paymentreceived.
    From these fields i need to return the data that matches 2 conditions,
    a) the insurance company name in cboInsCo and
    b) when paymentreceived = NO

    Below is the SQL statement that i am using,


    Private Sub cmdLst_Click()
    Dim InsList As String
    InsList = cboInsCo.Text

    Set cn = New ADODB.Connection
    cn.ConnectionString = "Provider =Microsoft.Jet.OLEDB.4.0;" & "Data Source = C:\SKM_ICE Database\SKM_ICE DBase.mdb"
    cn.Open
    Set rsAccounts = New ADODB.Recordset
    rsAccounts.Open "SELECT OurRef, YourRef, Date, insured2, totalcharges"
    From tbl_master
    Where insurancecompany = InsList And PaymentReceived = "NO"

    rsAccounts.Close
    cn.Close
    Set rsAccounts = Nothing
    Set cn = Nothing

    End Sub

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Access SQL /VB6 connection help needed

    is insuracncecompany stored as text in the DB? If so then the where statement would be: Where insuracnecompany = '" & text looking for here & "' And Paymentrecieved = "

    again is Paymentrecieved a text field of boolean (Yes/NO) if Yes/No then Paymentrecieved = False (or Not Paymentrecieved)
    if Paymentrecieved is text then PaymentRecieved = 'NO'"
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

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

    Re: Access SQL /VB6 connection help needed

    Hi, sorry its took me so long to reply to your post, i havnt been able to get online for a while.

    I tried the code you posted but got an error message while coding ,as shown below. sorry, no image as i cant work out how to paste it here, the option is not available.
    Anyway i get a messagebox stating "Compile error: Expected: Expression" and ' is highlighted.
    if i alter the code slightly to this

    rsAccounts.Open "SELECT OurRef, YourRef, Date, insured2, totalcharges"
    From tbl_master
    Where insurancecompany = " & InsList & " And PaymentReceived = "NO"

    I get another message box error when i try to run the code stating "Compile error: sub or function not defined" and the word "WHERE" is highlighted.

    any help would be appreciated.

  4. #4

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

    Need HELP with SQL Select Where query.

    Hello.
    I urgently need help with the above SQL statement, my SELECT WHERE just returns errors.
    Help Please.

    SKM.

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Access SQL /VB6 connection help needed

    That is not coded properly.

    Should be:

    Code:
    rsAccounts.Open "SELECT OurRef, YourRef, Date, insured2, totalcharges" _
       & " From tbl_master" _
       & " Where insurancecompany = " & InsList & " And PaymentReceived = 'NO'"
    Note the line continuation characters - the proper "-quote use.

    The spaces in front of the FROM and WHERE and the single-quotes around the NO.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

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

    Question Re: Access SQL /VB6 connection help needed

    Thanks szlamany,
    I have tried your code and dont get any errors untill i run the code then i get the error
    Runtime-error '3709'
    Operation is not allowed on an object referencing
    a closed or invalid connection.

    When you debug the SELECT statement is highlighted.
    The code i am using is shown below.


    VB Code:
    1. Dim cn As ADODB.Connection
    2. Dim rsAccounts As ADODB.Recordset
    3.  
    4. Private Sub cmdLst_Click()
    5. Dim InsList As String
    6. InsList = cboInsCo.Text
    7.  
    8. Set cn = New ADODB.Connection
    9.       cn.ConnectionString = "Provider =Microsoft.Jet.OLEDB.4.0;" & "Data Source = C:\SKM_ICE Database\SKM_ICE DBase.mdb"
    10.       cn.Open
    11. Set rsAccounts = New ADODB.Recordset
    12.  
    13. Set cn = New ADODB.Connection
    14.       cn.ConnectionString = "Provider =Microsoft.Jet.OLEDB.4.0;" & "Data Source = C:\SKM_ICE Database\SKM_ICE DBase.mdb"
    15.       cn.Open
    16.   Set rsAccounts = New ADODB.Recordset
    17.  
    18.  rsAccounts.Open "SELECT OurRef, YourRef, Date, insured2, totalcharges" _
    19.   & " From tbl_master" _
    20.   & " Where insurancecompany = " & InsList & " And PaymentReceived = 'NO'"
    21.  
    22. rsAccounts.Close
    23.     cn.Close
    24.     Set rsAccounts = Nothing
    25.     Set cn = Nothing
    26.  
    27. End Sub


    I use the same connection and recordset for other statements and have no problems so this error message is confusing me.

    Hope you can help.

    SKM
    Thanks.

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Access SQL /VB6 connection help needed

    Do you really have the connection code appearing twice in that routine?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8

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

    Re: Access SQL /VB6 connection help needed

    Sorry, that was a pasting mistake,
    the code is as below.

    VB Code:
    1. Dim cn As ADODB.Connection
    2. Dim rsAccounts As ADODB.Recordset
    3.  
    4. Private Sub cmdLst_Click()
    5. Dim InsList As String
    6. InsList = cboInsCo.Text
    7.  
    8. Set cn = New ADODB.Connection
    9.       cn.ConnectionString = "Provider =Microsoft.Jet.OLEDB.4.0;" & "Data Source = C:\SKM_ICE Database\SKM_ICE DBase.mdb"
    10.       cn.Open
    11. Set rsAccounts = New ADODB.Recordset
    12.  
    13.  rsAccounts.Open "SELECT OurRef, YourRef, Date, insured2, totalcharges" _
    14.   & " From tbl_master" _
    15.   & " Where insurancecompany = " & InsList & " And PaymentReceived = 'NO'"
    16.  
    17. rsAccounts.Close
    18.     cn.Close
    19.     Set rsAccounts = Nothing
    20.     Set cn = Nothing
    21.  
    22. End Sub

    One more question, How do you get your code in the forum displayed in a box as in your posts?

    SKM.
    Thanks

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Access SQL /VB6 connection help needed

    Instead of [vbcode] tags I use [code] tags - I go back and forth...

    The [code] tags will put scroll bars if the text is too large - sometimes that's good and sometimes I don't like it.

    But back to your problem - that code looks pretty standard to me. I don't do ACCESS - so I cannot tell if the connection string is good or not.

    Did you cut out code from your post that uses the recordset? I guessing you did.

    Maybe change the select to this just for testing:

    Code:
    rsAccounts.Open "SELECT * from tbl_master"

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  10. #10

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

    Re: Access SQL /VB6 connection help needed

    Hi,
    I changed the code from

    Code:
    rsAccounts.Open "SELECT OurRef, YourRef, Date, insured2, totalcharges" _
    & " From tbl_master" _
    & " Where insurancecompany = " & InsList & " And PaymentReceived = 'NO'"
    which at runtime returned the error

    Run time error ‘3709’:
    Operation is not allowed on an object referencing a closed or invalid connection.

    When debugged the above SELECT statement was highlighted.

    I changed the code as advised for testing to

    Code:
    rsAccounts.Open "SELECT * from tbl_master"
    This returned the same Run time error 3709 and the SELECT statement was highlighted when debugged.

    If I use the statement below I get no error but everything in the database is displayed in the ListView and not just the records I am after. The connection and recordset work ok until I use a SELECT statement.
    Below is the full code I am using to connect to the Access database and display the returned fields in a Listview.

    Code:
    Option Explicit
    Dim objCurrLi As ListItem
    Dim InsList As String
    
    Private Sub cmdLst_Click()
    InsList = cboInsCo.Text
    ListView1.ListItems.Clear   'Clear existing ListView
        
      Set cn = New ADODB.Connection
          cn.ConnectionString = "Provider =Microsoft.Jet.OLEDB.4.0;" & "Data Source = C:\SKM_ICE Database\SKM_ICE DBase.mdb"
          cn.Open
      Set rsAccounts = New ADODB.Recordset
      
         rsAccounts.Open "SELECT OurRef, YourRef, Date, insured2, totalcharges" _
         & " From tbl_master" _
         & " Where insurancecompany = " & InsList & " And PaymentReceived = 'NO'"
    
    Do While Not rsAccounts.EOF
         Set objCurrLi = ListView1.ListItems.Add(, , rsAccounts!ourRef & "") 'Our Ref
             objCurrLi.SubItems(1) = rsAccounts!YourRef & ""                 'Your Ref
             objCurrLi.SubItems(2) = rsAccounts!Date & ""                    'Date
             objCurrLi.SubItems(3) = rsAccounts!Insured2 & ""                'Insured2
             objCurrLi.SubItems(4) = rsAccounts!TotalCharges & ""            'Total charges
         rsAccounts.MoveNext
        Loop
    
        rsAccounts.Close
        cn.Close
        Set rsAccounts = Nothing
        Set cn = Nothing
    
    End Sub

    Any help would be much appreciated.

    SKM.
    Thanks

  11. #11
    Addicted Member
    Join Date
    Jun 2002
    Location
    Brugge, Belgium
    Posts
    208

    Re: Access SQL /VB6 connection help needed

    Hello,

    If you open an adodb.recordset you need to specify the connection as well.

    VB Code:
    1. rs.open "select * from table1",cn

  12. #12

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

    Question Re: Access SQL /VB6 connection help needed

    Hello, I changed the code to specify the connection by adding cn as shown below.

    Code:
    rsAccounts.Open "SELECT OurRef, YourRef, Date, insured2, totalcharges", cn _
         & " From tbl_master" _
         & " Where insurancecompany = " & InsList & " And PaymentReceived = 'NO'"
    Now when i run the code i get the following error message but im not sure what its trying to tell me, i think it means the SELECT statement is wrong

    Run-Time error '-2147217805 (80040e73)':
    Format of the initialization string does not conform to the OLE DB specification.


    If anybody knows where i have gone wrong would you please spell it out to me.


    SKM.
    Thanks

  13. #13
    Addicted Member
    Join Date
    Jun 2002
    Location
    Brugge, Belgium
    Posts
    208

    Re: Access SQL /VB6 connection help needed

    Hello,

    I've you've read my message correctly you would have seen that cn needs to be at the end of the open statement after your sql statement.

    It's the second parameter.

    so your code would be
    Code:
    rsAccounts.Open "SELECT OurRef, YourRef, Date, insured2, totalcharges" _
         & " From tbl_master" _
         & " Where insurancecompany = " & InsList & " And PaymentReceived = 'NO'" , cn
    Jason

  14. #14

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

    Re: Access SQL /VB6 connection help needed

    Thank you Jason for your reply.
    Im fairly new to programming and SQL statements so please forgive any mistakes i make.
    I havn't tried your last code yet and will post the results as soon as i do.
    Thanks,

    SKM

  15. #15
    Addicted Member
    Join Date
    Jun 2002
    Location
    Brugge, Belgium
    Posts
    208

    Re: Access SQL /VB6 connection help needed

    Did it work ?

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