Results 1 to 12 of 12

Thread: using SQL in VB

  1. #1
    Guest
    How does one use SQL statements inside of VB? I am using VB6 and Access 2000 databases.

    thanks yall

  2. #2
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Lightbulb

    It all depands on what type of information you need tend to retrieve from the database. the SQl statement can be:

    • SELECT ...FROM...WHERE
    • INSERT INTO ... SELECT ...FROM... WHERE ...ORDER BY
    • SELECT DISTINCTROW ... FROM ...
    • SELECT DISTINCT ... FROM...
    • and many many more


    So, may be you can just describe more about your objective, before we can give you some guide. Or you can take a look on the Database tutorial on this forum Tutorial

    [Edited by Chris on 06-17-2000 at 03:56 PM]

  3. #3
    Guest
    i would like to use a selcet statement but i dont know how that is my question

  4. #4
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Lightbulb

    May be you can take the Biblio.mdb as an example:

    Let said, I want to select all the Titles information from Table Titles with PubID = 175, then the SQl will look like:

    Code:
    SELECT * FROM Titles WHERE PubID='175' ORDER BY ISBN ASC;
    Hope this will help or you can take a look on the DAO2.6 Help file.


  5. #5
    Guest
    ok i know how to write SQL i know how to use it what i don't know is how to apply it in VB. how do i call the select state ment? where do i get the results? thats what i need to do.

    can i just type in select * from table in the code and it works?

    thasnk

  6. #6
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Lightbulb

    Take a look on this code & hope this will help you.

    Assume you have reference your project to Microsoft DAO 3.5 Object Library

    Goto Project|Reference

    Code:
    Option Explicit
    Private Db As DAO.Database
    Private Rs As DAO.Recordset
    Private xSQL As String
    Private Sub Form_Load()
    'Open the Biblio.mdb database
    Set Db = DBEngine.Workspaces(0).OpenDatabase(App.Path & "\Biblio.mdb", False, False)
    
    'Build the SQL string
    xSQL = "SELECT * FROM Titles WHERE PubID=175 ORDER BY Title ASC;"
    
    'Open the recordset
    Set Rs = Db.OpenRecordset(xSQL, dbOpenSnapshot)
    
    DoEvents
    While Not Rs.EOF
        Debug.Print Rs.Fields(0) & Space(10) & Rs.Fields(1) & Space(10) & _
                    Rs.Fields(2) & Space(10) & Rs.Fields(3) & Space(10) & _
                    Rs.Fields(4) & Space(10) & Rs.Fields(5) & Space(10) & _
                    Rs.Fields(6) & Space(10) & Rs.Fields(7)
        Rs.MoveNext
    Wend
    
    'Close the recordset & database
    Rs.Close
    Db.Close
    
    'Close the DAO Object
    Set Rs = Nothing
    Set Db = Nothing
    End Sub

  7. #7
    Fanatic Member Wen Lie's Avatar
    Join Date
    Jul 1999
    Location
    Singapore
    Posts
    524

    Wink okay....

    Hi dMartin17,

    I've got your point, and I think Chris has explained clearly to you. I just want to add something, hope will usefull for you Martin...

    I always use SQL Statement at my VB project. If you only use SQL statement to make a view virtual table (such as select statement), you can use it everywhere at VB code to open a view table (a recordset).

    Here is my example.

    Public Sub Open_Table()
    Dim myConn as ADODB.Connection
    Dim myRec as ADODB.Recordset

    myConn.Open "DSN=yourdsnname"
    myRec.Open "select * from author inner join books on author.codes = books.codes where author.id = 'AR001'", myConn, adOpenForwardOnly, adLockReadOnly, adCmdText

    if (myRec.BOF<>True) and (myRec.EOF<>True) then
    'your code goes here
    end if

    myRec.Close
    myConn.Close
    End Sub


    If you want to call a SQL Server Stored Procedure from your vb, you can use the statement below :

    myConn.Execute "your_stored_procedure_name"

    ok, hope that's all will help you,
    Cheers,
    Wen Lie
    Regards,
    [-w-]

  8. #8
    Guest
    thanks guys i think this is what i needed:

    Set Rs = Db.OpenRecordset(xSQL, dbOpenSnapshot)

    assuming xsql=select something from sometable

    and that makes the recordset a databses of what was returned in the select statement, correct?

    ill have to try it soon so I can see if i do have a handle on it.


    thanks again
    doug

  9. #9
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Lightbulb

    Yes, you're rite & hope you can get whatever you wish from the database. But you also can create some more power SQL statement that will make your life easy.

  10. #10
    Guest
    what do you mean i can creat more powerful stements? do you mean with where betweens, etc? i know those. after i do the SQL stastment, should i just use the recordset as i would before with .eof and .bof etc?

  11. #11
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    Joins, Unions, Aggregate functions are some power features of SQL...

  12. #12
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Lightbulb

    Exactly like what JHausmann have said and you also can create a double SELECT statement as well.

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