Results 1 to 5 of 5

Thread: Connecting to a SQL Server database using ADODB

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    7

    Connecting to a SQL Server database using ADODB

    Hi guys,
    here's the code example
    Code:
    01	Private Sub Command1_Click()
    02	 
    03	    Dim MyConnObj As New ADODB.Connection 'ADODB Connection Object
    04	    Dim myRecSet As New ADODB.Recordset 'Recordset Object
    05	    Dim sqlStr As String ' String variable to store sql command
    06	     
    07	    MyConnObj.Open _
    08	        "Provider = sqloledb;" & _
    09	        "Data Source=172.16.1.60;" & _
    10	        "Initial Catalog=TESTATV;" & _
    11	        "User ID=sa;" & _
    12	        "Password=p@ssW0rd;"
    13	     
    14	     sqlStr = "select * from employee"
    15	     
    16	    myRecSet.Open sqlStr, MyConnObj, adOpenKeyset
    17	         
    18	    MsgBox "Total Number of records = " & myRecSet.RecordCount
    19	     
    20	    Dim i As Integer 'variable to keep count
    21	    i = 1
    22	     
    23	    Print "#"; Tab; "ID"; Tab; "Name"; Tab; "Salary"
    24	    Print ""
    25	    
    26	    While Not myRecSet.EOF ' Loop until endd fo file is reached
    27	     
    28	        Print i; Tab; myRecSet(0); Tab; myRecSet(1); Tab; myRecSet(2)
    29	            '0- 1st filed, 1- 2nd Field and so on...
    30	             
    31	        myRecSet.MoveNext 'Moves the RecordSet pointer to the next position
    32	         
    33	        i = i + 1
    34	    Wend
    35	         
    36	    MyConnObj.Close
    37	     
    38	End Sub
    This code will only select data from a table from one database. Do you guys know what can I do if I want to select data from multiple database? Thanks

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Connecting to a SQL Server database using ADODB

    Moved To Database Development
    Last edited by Hack; Jun 28th, 2010 at 08:42 AM.

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

    Re: Connecting to a SQL Server database using ADODB

    you could make multiple connections to the different databases and run your select.

    If we're talking about SQL Server (and based on sqloledb, that would be reasonable)... and all of the databases are on the same server, you can do a Union query to pull the data, as long as all of the tables have the same structure:

    Code:
    Select Field1, Field2, Field3
    FROM tbl1
      UNION
    Select Field1, Field2, Field3
    FROM secondDB.tbl1
      UNION
    Select Field1, Field2, Field3
    FROM thirdDB.tbl1
    If they are on different servers and you have linked servers set up (so that each server can see the others)...
    Code:
    Select Field1, Field2, Field3
    FROM tbl1
      UNION
    Select Field1, Field2, Field3
    FROM server2.secondDB.tbl1
      UNION
    Select Field1, Field2, Field3
    FROM Server2.thirdDB.tbl1
      UNION
    Select Field1, Field2, Field3
    FROM Server3.fourthDB.tbl1
    If none of that applies, then you'll need individual connections for each one.

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

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    7

    Re: Connecting to a SQL Server database using ADODB

    " myRecSet.Open sqlStr, MyConnObj, adOpenKeyset "

    notice that it opens only one database.... even if I use UNION, it's still not selecting values from other databases. I'm trying to figure out how to open more than one database....

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

    Re: Connecting to a SQL Server database using ADODB

    Did you read what I posted.... LOOK very hard at the SQL I posted... you'll see that each part of the union is different slightly....

    And it can be done in one connection... depending on how it's setup...

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

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