+ Reply to Thread
Results 1 to 5 of 5

Retrieve access data into excel

  1. #1
    Registered User
    Join Date
    04-25-2007
    Posts
    3

    Retrieve access data into excel

    I have a VBA code that retrieves data from an Access database and populate the excel spreadsheet. However, I am only to retrieve one field at a time. I am sure there is a more efficient way of doing this. I would like to retrieve multiple fields from the database and populate several excel cells with appropriate data. The data expands across two tables. Any ideas would be appreciated. Here is what I have that populates one cell.

    Const stDB As String = "\effort.mdb"

    Sub GetData()
    Dim cnt As ADODB.Connection
    Dim stCon As String, strDB As String, stSQL As String
    Dim cmd As ADODB.Command, rs As ADODB.Recordset
    Dim Parm1 As ADODB.Parameter, Parm2 As ADODB.Parameter
    Dim ID As Long
    Dim section As ADODB.Parameter
    ID = Range("tuid").Value
    Range("c5").Value = ""
    'database path - currently same as this workbook
    strDB = ThisWorkbook.Path & "\EFFORT.mdb"

    stCon = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & strDB & ";"

    stSQL = "select Name from Physicians where Code = " & ID & " "

    'set connection variable
    Set cnt = New ADODB.Connection
    Set cmd = New ADODB.Command

    'open connection to Access db and run the SQL
    cnt.Open stCon
    If cmd.CommandText = "" Then
    cmd.ActiveConnection = stCon
    With cmd
    .CommandText = stSQL
    .CommandType = adCmdText
    .CommandTimeout = 15
    End With
    End If

    '// here's a simple way to specify the date params
    ' cmd.Parameters(0).Value = [A1].Text ' i.e. [Start Date:]
    'cmd.Parameters(1).Value = [B1].Text ' i.e. [End Date:]

    Set rs = cmd.Execute()
    [c5].CopyFromRecordset rs

    'close connection
    cnt.Close
    'release object from memory
    Set cnt = Nothing
    End Sub


    Thanks.

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Have you tried putting the extra fields you require into the SQL string?

    stSQL = "select Name from Physicians where Code = " & ID & " "
    to (as an example)
    stSQL = "select Name, Address, Phone from Physicians where Code = " & ID & " "

    rylo

  3. #3
    Registered User
    Join Date
    04-25-2007
    Posts
    3
    Yes. I have tried including all the fields, but get an error Method "Execute" of Command Object failed on the following line.. 'Set rs = cmd.Execute()".

    I will need each field to go in a designated cell.

  4. #4
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628
    I tried to use your code and it seems to run fine.

    Microsoft says the fast 'CopyFromRecordset' instruction will fail if the recordset
    contains an OLE object field or array data such as hierarchical recordsets.

    For more info: http://support.microsoft.com/kb/246335

    Regards,
    Antonio

  5. #5
    Registered User
    Join Date
    04-25-2007
    Posts
    3
    ok.. i got it to work mostly. The error pops up when I use a field in the select statement that has "NULL" for some of its data.

    Thanks

+ 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