Results 1 to 10 of 10

Thread: [RESOLVED] ADO VB6 Access DB

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Resolved [RESOLVED] ADO VB6 Access DB

    ok VB 6 with a Access Database using ADO

    I have an autonumber fields which puts my records in the order i require them.

    I just need pointing in the right direction, wot function to look at etc. What i need to do it, make sure that a few fields are the same and then give me whats in another field and then check the whole database.

    Example.

    Field "Type" to be "Turbo NL"
    Field "Entry Fee" to be "6.00"

    then for all the records that have them two (both that is) i want to know whats in the field called "Placed"

    Hope this is clear.
    Last edited by Ricky1; Jul 10th, 2006 at 05:47 PM.
    Im Learning !!!!

  2. #2
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: ADO VB6 Access DB

    Not to me.
    If you like to Post it in dutch (seems your tongue is dutch-> turbo NL)and I will translate it in English for you. Be sure to be clear and keep in mind your readers have no clue what you are up to so be precise and rather write a bit to much as skipping some vital info.
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: ADO VB6 Access DB

    Basically, i need to find all the records that contain two specific critea. If that record does meet the two criteas then i want to take a value from another field that wasn't part of the critea.

    Say my table had 4 fields "Name", "Age", "Location", "Gender"

    I want all the records that have the "Name" smith, and are "Age" 15 then from them records that have both them. I want to take the value from the field "Location"
    Im Learning !!!!

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: ADO VB6 Access DB

    You would use an SQL statement like this:
    Code:
    SELECT Location
    FROM tablename
    WHERE [Name] = 'Smith'
    AND Age = 15
    (name needs to be in square brackets as it is a reserved word)

    How you would run this from VB depends on exactly what you are using.. as there are two forms of ADO (the dreaded Data Control, and code).

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: ADO VB6 Access DB

    i used Beacons ADO Tuturial for the basics so that form of ADO i have this at the top of my code

    Private cn As ADODB.Connection
    Private rs As ADODB.Recordset

    as you can tell i am new to this.
    Im Learning !!!!

  6. #6
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: ADO VB6 Access DB

    Basicly it could look like this.

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3. Dim Cn As ADODB.Connection
    4. Dim Rs As ADODB.Recordset
    5.  
    6.  
    7. Set Cn = New ADODB.Connection
    8. 'The path will differ so you need to dajust it
    9. Cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Test\YourDatabase.mdb Persist Security Info=False"
    10. 'You need to fill in the correct table name as well, and maybe opt for another cursor
    11. Rs.Open "SELECT Location From tablename WHERE [Name] = 'Smith' AND Age = 15", Cn, adOpenForwardOnly, adLockReadOnly
    12.  
    13. While Not Rs.EOF
    14.    
    15.     Me.text1 = Me.text1 & Rs.Fields("Location") & vbCrLf
    16.     Rs.MoveNext
    17.    
    18. Loop
    19.  
    20.  
    21. End Sub
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: ADO VB6 Access DB

    ok how do i see how many times something appears?

    Such as the name "Smith" and gender "male" just want to see how many records have them two in them.
    Im Learning !!!!

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: ADO VB6 Access DB

    Code:
    Private Sub Command1_Click()
    
    Dim Count As Integer
    
    rs.Close
    rs.Open "SELECT Placed from tblData where Type = 'NL Holdem Turbo'AND Entry = '6.00'", cn, adOpenForwardOnly, adLockReadOnly
    
    Do While Not rs.EOF
    
        If rs.Fields("Placed") = 1 Then
            Count = Count + 1
        End If
    
    Loop
    
    Text1.Text = Count
    
    End Sub
    tried this to tell me how many times placed has been 1 with the given critea but if freezes
    Im Learning !!!!

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: ADO VB6 Access DB

    The problem with that code is that you do not move on to the next record (so it just repeatedly reads the first one). To correct that, just put in Rs.MoveNext before the Loop line.

    ..however, there is a better way - get the database to do the work instead (it is quicker, and the code is simpler). To do this, just change your code to this:
    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3. rs.Close
    4. rs.Open "SELECT [u]Count(*)[/u] from tblData where Type = 'NL Holdem Turbo'AND Entry = '6.00'", cn, adOpenForwardOnly, adLockReadOnly
    5.  
    6. 'the recordset now only returns a single value (the count) so put it straight into the textbox
    7. If Not rs.EOF Then  
    8.   Text1.Text = rs.Fields(0).Value
    9. Else
    10.   '(this should never happen - the database should always give a count)
    11.   MsgBox "oops... there was an error!"  
    12. End If
    13.  
    14. End Sub
    Note that if Entry only ever contains numbers (or blanks) it should be a Numeric data type, instead of Text. (if it is already a numeric data type, remove the ' marks from around the value in your SQL statement).

  10. #10
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: [RESOLVED] ADO VB6 Access DB

    I suspect this line needs to be altered
    Code:
    rs.Open "SELECT Count(*) from tblData where Type = 'NL Holdem Turbo'AND Entry = '6.00'", cn, adOpenForwardOnly
    in:
    VB Code:
    1. rs.Open "SELECT Count(*) from tblData where Type = 'NL Holdem Turbo AND Entry = '6.00' And Placed = 1" ', cn, adOpenForwardOnly

    To count the number of placed items with the value 1

    edit removed syntax error
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

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