+ Reply to Thread
Results 1 to 3 of 3

Import to excel Access database directly to a variable (not using sheets)

  1. #1
    Registered User
    Join Date
    01-30-2006
    Posts
    10

    Import to excel Access database directly to a variable (not using sheets)

    Hi all,

    I would like to know if itīs possible to import data from access using directly a variable in VBA excel instead of pasting all values in my worksheet.

    Thanks a lot.

  2. #2
    Patrick Molloy
    Guest

    RE: Import to excel Access database directly to a variable (not using

    sure is -- help covers it.
    In the IDE set a reference to the Microsoft ActiveX Data Obects Library
    you probably have v2.7

    Adapt the code below, which should be pasted into a standatd module.
    The variable MyFile is assigned the Access Databas enaem that I'm using. You
    should replace this with the full path etc of the Access database that you
    want to use
    You should also edit the SQL script accordingly...as my demo is pretty
    simple, so is my sql
    run the code and a connection is made to the database, the recordset in
    memory gets populated according to the sql script. From the recordset fields,
    the field names are dropped intio the sheet as table headers and finally the
    data is copied directlyto the sheet



    Option Explicit

    Sub LoadDataFromAccess()
    Dim MyFile As String
    Dim con As New ADODB.Connection
    Dim rst As New ADODB.Recordset
    Dim SQL As String
    Dim i As Long

    MyFile = "Risk.mdb"
    SQL = "SELECT * FROM BondTable"

    con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & MyFile

    rst.Open SQL, con, adOpenStatic

    Cells.Clear

    For i = 0 To rst.Fields.Count - 1
    Cells(1, i + 1).Value = rst.Fields(i).Name
    Next

    Range("A2").CopyFromRecordset rst

    rst.Close
    con.Close

    Set rst = Nothing
    Set con = Nothing

    End Sub

    "internacio" wrote:

    >
    > Hi all,
    >
    > I would like to know if itÂīs possible to import data from access using
    > directly a variable in VBA excel instead of pasting all values in my
    > worksheet.
    >
    > Thanks a lot.
    >
    >
    > --
    > internacio
    > ------------------------------------------------------------------------
    > internacio's Profile: http://www.excelforum.com/member.php...o&userid=30960
    > View this thread: http://www.excelforum.com/showthread...hreadid=506308
    >
    >


  3. #3
    Tom Ogilvy
    Guest

    Re: Import to excel Access database directly to a variable (not using

    In Patrick's code, he is writing the information to the worksheet. You
    could put it in an array with something like

    Dim v as Variant

    v = Application.Transpose(rst)

    or work with rst directly.

    --
    Regards,
    Tom Ogilvy

    "Patrick Molloy" <[email protected]> wrote in message
    news:[email protected]...
    > sure is -- help covers it.
    > In the IDE set a reference to the Microsoft ActiveX Data Obects Library
    > you probably have v2.7
    >
    > Adapt the code below, which should be pasted into a standatd module.
    > The variable MyFile is assigned the Access Databas enaem that I'm using.

    You
    > should replace this with the full path etc of the Access database that you
    > want to use
    > You should also edit the SQL script accordingly...as my demo is pretty
    > simple, so is my sql
    > run the code and a connection is made to the database, the recordset in
    > memory gets populated according to the sql script. From the recordset

    fields,
    > the field names are dropped intio the sheet as table headers and finally

    the
    > data is copied directlyto the sheet
    >
    >
    >
    > Option Explicit
    >
    > Sub LoadDataFromAccess()
    > Dim MyFile As String
    > Dim con As New ADODB.Connection
    > Dim rst As New ADODB.Recordset
    > Dim SQL As String
    > Dim i As Long
    >
    > MyFile = "Risk.mdb"
    > SQL = "SELECT * FROM BondTable"
    >
    > con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & MyFile
    >
    > rst.Open SQL, con, adOpenStatic
    >
    > Cells.Clear
    >
    > For i = 0 To rst.Fields.Count - 1
    > Cells(1, i + 1).Value = rst.Fields(i).Name
    > Next
    >
    > Range("A2").CopyFromRecordset rst
    >
    > rst.Close
    > con.Close
    >
    > Set rst = Nothing
    > Set con = Nothing
    >
    > End Sub
    >
    > "internacio" wrote:
    >
    > >
    > > Hi all,
    > >
    > > I would like to know if itīs possible to import data from access using
    > > directly a variable in VBA excel instead of pasting all values in my
    > > worksheet.
    > >
    > > Thanks a lot.
    > >
    > >
    > > --
    > > internacio
    > > ------------------------------------------------------------------------
    > > internacio's Profile:

    http://www.excelforum.com/member.php...o&userid=30960
    > > View this thread:

    http://www.excelforum.com/showthread...hreadid=506308
    > >
    > >




+ Reply to Thread

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