Results 1 to 32 of 32

Thread: SQL in VB

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    76

    Exclamation SQL in VB

    Hi,

    In my program i select a value from a database i.e

    select name_f from owner where owner_id=text10.text

    What i need to do is:

    for each name_f
    select employed from employment where name=name_f(name from previous sq query)

    Does anyone know how to implement this in vb? I am new to VB and using VB5.

    What i have so far is :


    Dim sqll As String
    DimRecords As Recordset

    sqll = "select name_f from owner where owner_id='" & Text10.Text & "'"

    Set Records = aphiscon.OpenRecordset(sSQL, dbOpenSnapshot)






    I would appreciate any help with this.

    Thanks

  2. #2
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: SQL in VB

    I would think you would get the results you want by just updating your query with a join.

    sql = "select emp.employed "
    sql = sql & "from employment emp "
    sql = sql & "inner join owner own on own.name_f = emp.name "
    sql = sql & "where own.owner_id='" & text10.text & "'"

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    76

    Re: SQL in VB

    Thanks for your help but this is not returning the correct result.

    How can i use the result of an SQL query in a for each loop?

  4. #4
    Member
    Join Date
    Oct 2012
    Posts
    37

    Re: SQL in VB

    Quote Originally Posted by CM670 View Post
    Thanks for your help but this is not returning the correct result.

    How can i use the result of an SQL query in a for each loop?
    You mean to say you want to browse through a file using the sql statement?
    Like say:
    strsql="Select * From Customers"
    and you want to browse through the recordset?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    76

    Re: SQL in VB

    yes,

    eg-
    strsql="select name from owner"

    I want to browse through all the names.

  6. #6
    Member
    Join Date
    Oct 2012
    Posts
    37

    Re: SQL in VB

    Quote Originally Posted by CM670 View Post
    yes,

    eg-
    strsql="select name from owner"

    I want to browse through all the names.
    This is one of the basic database techniques one learns in the begining
    Suppose you have all the records in a recordset Called RstOwners
    Then do this:
    Do while Not RstOwners.EOF

    'Your code for example
    debug. print RstOwner!OwnerName
    'Assuming OwnerName is a field in the table
    Loop

  7. #7
    Member
    Join Date
    Oct 2012
    Posts
    37

    Re: SQL in VB

    Quote Originally Posted by CM670 View Post
    yes,

    eg-
    strsql="select name from owner"

    I want to browse through all the names.
    This is one of the basic database techniques one learns in the begining
    Suppose you have all the records in a recordset Called RstOwners
    Then do this:
    Do while Not RstOwners.EOF

    'Your code for example
    debug. print RstOwner!OwnerName
    'Assuming OwnerName is a field in the table
    Loop

  8. #8
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: SQL in VB

    Quote Originally Posted by naveed217 View Post
    This is one of the basic database techniques one learns in the begining
    Suppose you have all the records in a recordset Called RstOwners
    Then do this:
    Do while Not RstOwners.EOF

    'Your code for example
    debug. print RstOwner!OwnerName
    'Assuming OwnerName is a field in the table
    RstOwner.MoveNext
    Loop
    Make sure you add MoveNext to your loop

  9. #9
    Member
    Join Date
    Oct 2012
    Posts
    37

    Re: SQL in VB

    Quote Originally Posted by MarkT View Post
    Make sure you add MoveNext to your loop
    OOPS! I forgot that. Thanks for correcting Mark

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

    Re: SQL in VB

    Quote Originally Posted by MarkT View Post
    I would think you would get the results you want by just updating your query with a join.

    sql = "select emp.employed "
    sql = sql & "from employment emp "
    sql = sql & "inner join owner own on own.name_f = emp.name "
    sql = sql & "where own.owner_id='" & text10.text & "'"
    Quote Originally Posted by CM670 View Post
    Thanks for your help but this is not returning the correct result.

    How can i use the result of an SQL query in a for each loop?
    Probably because it's an inner join, so it only returns rows in both tables...

    This on the other hand,

    Code:
    sql = "select emp.employed "
    sql = sql & "from owner own "
    sql = sql & "left join  employment emp on own.name_f = emp.name "
    sql = sql & "where own.owner_id='" & text10.text & "'"
    might produce the results you're looking for... it starts with the owner table and does a left join to the employment table... if there is a record in the employment table, it will be returned, if not, you'll get NULL back.

    -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??? *

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: SQL in VB

    Code from the OP has a bit of an issue
    Code:
    Dim sqll As String
    DimRecords As Recordset
    
    sqll = "select name_f from owner where owner_id='" & Text10.Text & "'"
    
    Set Records = aphiscon.OpenRecordset(sSQL, dbOpenSnapshot)

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

    Re: SQL in VB

    That will do it too...

    -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??? *

  13. #13
    New Member
    Join Date
    Dec 2012
    Posts
    6

    Re: SQL in VB

    Quote Originally Posted by DataMiser View Post
    Code from the OP has a bit of an issue
    Code:
    Dim sqll As String
    DimRecords As Recordset
    
    sqll = "select name_f from owner where owner_id='" & Text10.Text & "'"
    
    Set Records = aphiscon.OpenRecordset(sSQL, dbOpenSnapshot)
    Yeah, that may be the problem

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    76

    Re: SQL in VB

    Nothing seems to be working for me :

    I have the tables owner and employment

    The code i need is

    if not EOF and text10.text is not null
    Select name_f from owner where owner_id=text10.text

    For each name (name_f from owner table)
    select employed from employment where name_f=name (name_f from owner table)

    if not EOF
    if employment is null then
    add name_f to grid

    any ideas on how to write this in VB5??

  15. #15
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: SQL in VB

    try this:

    sSql= "select * from employment INNER JOIN owner "
    sSql = sSql + "ON employment.name_f=owner.name_f where owner.id = " & Text1.Text

    run your RS

    this should return all your values, then

    REM set grid column here
    do while NOT rs.EOF
    if ISNULL rs!employment then
    grid1.row = grid1.row + 1 'assuming you have a header row
    grid1.text = rs!name_f
    rs.movenext
    loop

    SOMETHING like that....

  16. #16
    New Member
    Join Date
    Feb 2012
    Location
    SK, Canada
    Posts
    10

    Re: SQL in VB

    To check if the SQL statement is correct design it in Access 97 to get the results you need. Then switch to the SQL view. Copy and paste from Access into VB6 with some modification if necessary.

  17. #17
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: SQL in VB

    You might also find the FAQ in the database development forum an interesting read.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    76

    Re: SQL in VB

    Quote Originally Posted by SamOscarBrown View Post
    try this:

    sSql= "select * from employment INNER JOIN owner "
    sSql = sSql + "ON employment.name_f=owner.name_f where owner.id = " & Text1.Text

    run your RS

    this should return all your values, then

    REM set grid column here
    do while NOT rs.EOF
    if ISNULL rs!employment then
    grid1.row = grid1.row + 1 'assuming you have a header row
    grid1.text = rs!name_f
    rs.movenext
    loop

    SOMETHING like that....


    Thanks this has helped me a lot. I have done it exactly like this however it keeps throwing an error at the line where i run the recordset. The recordset is empty would this be the problem?

  19. #19
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: SQL in VB

    Show the code you are using now.

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    76

    Re: SQL in VB

    Code:
    Dim sSQL as String 
    Dim Records As Recordset
    
    sSql= "select * from employment INNER JOIN owner "
    sSql = sSql + "ON employment.name_f=owner.name_f where owner.id = " & Text1.Text
    
    set Records. dbconn.openrecordset(sSql, dbOpenSnapShot)
    
    grdMyGrid.Row(0)
    grdMyGrid.Col(0)
    
    Do While not Records.EOF
    if ISNULL rs!employment then
    grdMyGrid.row = GridMyGrid.row + 1 
    grdMyGrid.text = rs!name_f
    Records.movenext
    loop
    When i run my db query is says name_f is an ambiguous column?

  21. #21
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: SQL in VB

    WELL, for one, your grdMyGrid.row = GridMyGrid.row + 1 is not correct.....you probably meant grdMyGrid.row = grdMyGrid.row + 1
    And TWO, MY example used 'name_f' as a field in both tables (I though you had the same). Mine brought back a number greater than zero (5 in MY testing example).

    You MAY need grdMyGrid.text = rs!employment.name_f --- I am not on the computer I did the example, so can't check that for you. I didn't test WHAT was brought back, I just counted num of records.

    Check out ACCESS to see how to identify what was returned on that query...I will check later when I get the other machine.....

  22. #22
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: SQL in VB

    Well, did a quick test.

    Instead of using "select *.....", use "select employment.name_f, emplyment.XXXX1, employment.XXXX2...." where XXXX# is the name of the fields in the employment table you want to have returned.

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    76

    Re: SQL in VB

    sorry was trying to remember form memory what i had written as i was not on the machine where my project is stored when i replied.

    yes i did mean grdMyGrid=grdMyGrid+1, thats what i have
    name_f is a field in both tables.

    I tried your example :
    "Instead of using "select *.....", use "select employment.name_f, emplyment.XXXX1, employment.XXXX2...." where XXXX# is the name of the fields in the employment table you want to have returned."

    This seems to be working as it is no longer throwing an error however the name is not showing in the grid. Do you know why this is?

  24. #24

    Thread Starter
    Lively Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    76

    Re: SQL in VB

    When i run the query again, it is still saying that name_f is an ambigous column.

  25. #25
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: SQL in VB

    Here's mine.....works just fine....make yours LIKE mine: Make sure you have returned at least one row....(msgbox rs.recordcount)

    Code:
    Private Sub Command4_Click()
     Dim rs As New Recordset
        dbConnection
        Set cmd = New ADODB.Command
        Set cmd.ActiveConnection = cnn
    
        cmd.CommandText = "select employment.name_f, employment.firstname from employment INNER JOIN owner "
        cmd.CommandText = cmd.CommandText + "ON employment.name_f=owner.name_f where owner.id = " & Text1.Text
        
        Set rs = cmd.Execute
    
    REM   use next line just for testing.....   if zero, then your query returned nothing (probably text1.text value)
    
       msgbox rs.recordcount
    
        Do While Not rs.EOF
              MsgBox rs!name_f + ", " + rs!firstname
        rs.MoveNext
        Loop
    End Sub

  26. #26

    Thread Starter
    Lively Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    76

    Re: SQL in VB

    thanks have done this. Still cant get it to put name_f in the grid though!

  27. #27
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: SQL in VB

    post latest code....

  28. #28

    Thread Starter
    Lively Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    76

    Re: SQL in VB

    Private Sub Form_Load()

    With grdMyGrid

    .row = 0

    .col = 0
    .ColWidth(.col) = 1600
    .Text = "Name "
    .ColAlignment(.col) = 2
    .col = 1
    .ColWidth(.col) = 1500
    .Text = "address"
    .ColAlignment(.col) = 2
    .col = 2
    .ColWidth(.col) = 1000
    .Text = "Employed?"
    .ColAlignment(.col) = 2

    .Rows = 1


    Dim sSQL as String
    Dim Records As Recordset

    sSql= "select * from employment INNER JOIN owner "
    sSql = sSql + "ON employment.name_f=owner.name_f where owner.id = " & Text1.Text

    set Records. dbconn.openrecordset(sSql, dbOpenSnapShot)

    grdMyGrid.Row(0)
    grdMyGrid.Col(0)

    Do While not Records.EOF
    if ISNULL rs!employment then
    grdMyGrid.row = GridMyGrid.row + 1
    grdMyGrid.text = rs!name_f
    Records.movenext
    loop

    End if

    End sub

  29. #29
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: SQL in VB

    Ah,....you missed what I said earlier....instead of using:

    sSql= "select * from employment INNER JOIN owner "

    use:

    sSql= "select employment.name_f, employment.XXXXX, employment.XXXX (ETC) from employment INNER JOIN owner " (where the XXXXs are the field names you want to get back....

    For some reason, the select * doesn't give me rs!name_f, but when you do the select like above, it does.....Understand?

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

    Re: SQL in VB

    select * WILL give you name_f .... BUT it appears in BOTH tables... so when you try to access rs!name_f ... it doesn't know which one you mean... do you want the one from the one table? Or the other table? It doesn't know... so it calls it ambiguous. To solve that, you HAVE to specify the fields you want... which is simply a good idea in the first place...

    Next problem....
    this code:
    Code:
    Do While not Records.EOF
    if ISNULL rs!employment then
    grdMyGrid.row = GridMyGrid.row + 1 
    grdMyGrid.text = rs!name_f
    Records.movenext
    loop
    All that does is set the text of the grid... and add rows to it... doesn't actually put the data in the grid... it's been a while since I've done VB6 coding... but ... if you replace your entire do while loop with this:

    Code:
    set grdMyGrid.DataSource = Records
    That will assign your Records recordset to your grid... that should be all you need to to.

    -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??? *

  31. #31
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: SQL in VB

    As OP wrote the loop, you are correct, tg. However, it all depends on what he wants in the grid, and where he wants it put.

    This following example puts name_f (from employment table) into the first column of my MSFlexGrid, starting with row # 1 (I placed a header row on my grid):
    Code:
        cmd.CommandText = "select employment.name_f, employment.firstname from employment INNER JOIN owner "
        cmd.CommandText = cmd.CommandText + "ON employment.name_f=owner.name_f where owner.id = " & Text1.Text
        
        Set rs = cmd.Execute
        Grid1.Col = 0
        Grid1.Row = 0
        Do While Not rs.EOF
               Grid1.Row = Grid1.Row + 1
               Grid1.Text = rs!name_f
           rs.MoveNext
        Loop
    If OP wants other fields in more columns, simple to modify the loop.

    SO many ways of doing this stuff---mine is just one. As he was planning a for-loop in the OP, I just stayed in that vein, that's all. And I am not sure if he is using datagrids or flexgrids.....and that makes even more ways of displaying DB data.....

    The last posting by CM definitely has an incorrect loop, as I told him how to change that earlier on, but he kept his original (even misspelling his grid name).

  32. #32

    Thread Starter
    Lively Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    76

    Re: SQL in VB

    Quote Originally Posted by SamOscarBrown View Post
    As OP wrote the loop, you are correct, tg. However, it all depends on what he wants in the grid, and where he wants it put.

    This following example puts name_f (from employment table) into the first column of my MSFlexGrid, starting with row # 1 (I placed a header row on my grid):
    Code:
        cmd.CommandText = "select employment.name_f, employment.firstname from employment INNER JOIN owner "
        cmd.CommandText = cmd.CommandText + "ON employment.name_f=owner.name_f where owner.id = " & Text1.Text
        
        Set rs = cmd.Execute
        Grid1.Col = 0
        Grid1.Row = 0
        Do While Not rs.EOF
               Grid1.Row = Grid1.Row + 1
               Grid1.Text = rs!name_f
           rs.MoveNext
        Loop
    If OP wants other fields in more columns, simple to modify the loop.

    SO many ways of doing this stuff---mine is just one. As he was planning a for-loop in the OP, I just stayed in that vein, that's all. And I am not sure if he is using datagrids or flexgrids.....and that makes even more ways of displaying DB data.....

    The last posting by CM definitely has an incorrect loop, as I told him how to change that earlier on, but he kept his original (even misspelling his grid name).

    Couldnt give out company code so was making up my own version similar to what i was doing. That explains the typos and confusion, And im a girl btw.

    Got it now, thanks techgnome.

Tags for this 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