Results 1 to 24 of 24

defining Column Reference with ADO connections using VBA

Threaded View

  1. #1
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    defining Column Reference with ADO connections using VBA

    Hi, does anyone know about ADO connections to Databases using VBA?

    I have this code that works well.

    It extracts Employee ID's and there Names from our database and lists them in columns A:C

    What I want to do is list the ID's manually and get it to extract only the Names for the ID's I listed in Column A...

    I am not sure how to amend this code to look at ID's listed in column A instead of extracting the whole database.

    I know I need to add a "WHERE ID =" clause in the SQL but not sure how to say WHERE ID = A1, A2, etc...

    Thanks.

    Here's the code that works at extracting the whole database (it's not mine...I don't remember where on the web I found it):

    Option Explicit
    
    Sub Button1_Click()
    
    
    'Defining variables
    Dim cnOra As ADODB.Connection
    Dim rsOra As ADODB.Recordset
    Dim db_name As String
    Dim UserName As String
    Dim Password As String
    Dim i As Long
    
    Set cnOra = New ADODB.Connection
    Set rsOra = New ADODB.Recordset
    
    db_name = "SANDBOX_ODBC"
    UserName = "SYSADM"
    Password = "SYSADM"
    
    'Making an ODBC connection according to ADO
    cnOra.Open "DSN=" + db_name + ";UID=" + UserName + ";PWD=" & Password + ";"
    rsOra.CursorLocation = adUseServer
    
    'Running a query
    rsOra.Open "Select ID, FIRST_NAME, LAST_NAME from EMPLOYEE", cnOra, adOpenForwardOnly
    
    'Passing on data from the recordset to a variable or cell.
    'Notice that the column name or alias is used to address
    'data in the recordset.
    i = 1
    While Not rsOra.EOF
    Worksheets("sheet1").Range("A" & i) = rsOra![ID]
    Worksheets("sheet1").Range("B" & i) = rsOra![FIRST_NAME]
    Worksheets("sheet1").Range("C" & i) = rsOra![LAST_NAME]
    rsOra.MoveNext
    i = i + 1
    Wend
    
    rsOra.Close
    rsOra.Open "select sysdate from dual", cnOra, adOpenForwardOnly
    
    While Not rsOra.EOF
    Worksheets("Sheet1").Range("E2") = rsOra![sysdate]
    rsOra.MoveNext
    Wend
    
    'Forgetting to close your connection will sometimes result in
    'Dr.Watsons
    rsOra.Close
    cnOra.Close
    Set rsOra = Nothing
    
    End Sub
    Last edited by NBVC; 05-16-2008 at 10:02 AM.
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1