+ Reply to Thread
Results 1 to 6 of 6

Thread: Use of Excel to pull data from database daily, posting new data below older data

  1. #1
    Registered User
    Join Date
    08-06-2011
    Location
    Dayton, OH
    MS-Off Ver
    Excel 2003
    Posts
    4

    Exclamation Use of Excel to pull data from database daily, posting new data below older data

    Okay. So here's what I am trying to do (obviously ficticious names):

    There is a database ("Motherload") that houses some records (29 fields in each record) that is updated on a daily bases (except for weekends, holidays, and the first/last business day of the month). I want to have excel go into this database, pull all of the information in the database and paste it in a single excel spreadsheet (ResourceEater). I will be doing this daily, and I want the information I am pulling to go directly below the information that was pulled the day prior. The rest of it (sorting, etc.) is easy and I can handle that macro. I don't know if I need to go the DAO route, or if there is a simpler way. The problem doesn't seem so difficult, but I the data I am going to attempt to pull is extremely important and if I mess up, my boss will kill me.

    I haven't drafted any code yet, and would greatly appreciate any direction you can steer me in.

  2. #2
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    703

    Re: Use of Excel to pull data from database daily, posting new data below older data

    Hi,

    Wich kind of database are you connecting to, good thing to know when you choose method to connect with.

    Steffen Thomsen

  3. #3
    Registered User
    Join Date
    08-06-2011
    Location
    Dayton, OH
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Use of Excel to pull data from database daily, posting new data below older data

    My apologies. I am pulling this from a Microsoft Access 2003 database. Is that what you were wanting to know?

  4. #4
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    703

    Re: Use of Excel to pull data from database daily, posting new data below older data

    Hi again,

    Then im afraid i cant help, havent worked with acces databases, only mysql.

    But i think you should look into the DAO thing.

    Steffen Thomsen

  5. #5
    Forum Guru Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    Excel 2010
    Posts
    1,249

    Re: Use of Excel to pull data from database daily, posting new data below older data

    Go to Data -> Import and set up a data connection.

    http://office.microsoft.com/en-us/ex...001183132.aspx

  6. #6
    Forum Contributor
    Join Date
    12-26-2010
    Location
    Kansas City, Kansas
    MS-Off Ver
    Excel 2007
    Posts
    110

    Re: Use of Excel to pull data from database daily, posting new data below older data

    Hi
    .
    This seems to work on my box.
    .
    Notes:
    . Uses ADO not DAO
    . Tested under Excel 2007 but modifies Provider if not Excel 2007
    . Should be able to pop the code below into Excel 2003 but may have
    missed something
    . Some code needs work to make more industrial strength
    HTH
    regards
    John
    Public Function ExcelToAccessToExcel() As Boolean
    
        ' * Save work book as Macro Workbook
        ' * Add General Module Alt-F11 - Insert Module
        ' * Tools -> References -> Microsoft ActiveX Data Objects 2.8
        ' * Tools -> References -> Microsoft ActiveX Data Recordset 2.8 Library
        ' * Tools -> References -> Microsoft ADO Ext 2.8 for DDL and Security
        ' * Paste this code into Module
        ' * Code should work in 2007/2003
        ' * USES ADO and not DAO
        ' * __ONLY__ tested as 2007 Excel
    
        On Error GoTo EH_ExcelToAccessToExcel
    
        '------------------------------
        '   Objects
        '------------------------------
        Dim oCN As ADODB.Connection
        Dim oRS As ADODB.Recordset
        
        '------------------------------
        '   Properties
        '------------------------------
        Dim sProvider As String
        Dim sPath As String
        Dim sACCDBName As String
        Dim sWBName As String
        Dim sExtender As String
        Dim sDataSource As String
        Dim sCNString As String
        
        '------------------------------
        '   Counters
        '------------------------------
        Dim mlngRSRecCNT As Long
        Dim mlngRSFieldCNT As Long
        
        '------------------------------
        '   Misc
        '------------------------------
        Dim i As Integer
        Dim lng As Long
        
        '------------------------------
        '   SQL variables
        '------------------------------
        Dim sSQL As String
    
        '------------------------------
        '   Part 1 - Excel To Access
        '------------------------------
        If Application.Version = 12# Then
            sProvider = "Microsoft.ACE.OLEDB.12.0;"
        Else
            'Not Tested
            sProvider = "Microsoft.Jet.OLEDB.4.0"
        End If
        sPath = ThisWorkbook.Path & "\"         'Change to yours
        sACCDBName = "DBTarget.accdb"           'Change to yours E.G. ABC.MDB etc, Change variable to sMDBName
        sExtender = vbNullString
        sDataSource = "Data Source = " & sPath & sACCDBName
    
        '------------------------------
        '   Connect To Access
        '------------------------------
        Set oCN = Nothing
        Set oCN = New ADODB.Connection
        With oCN
            .Provider = sProvider
            .ConnectionString = sDataSource
            .Open
        End With
    
        '------------------------------
        '   SQL Statement
        '------------------------------
        sSQL = "SELECT "
        sSQL = sSQL & " * "
        sSQL = sSQL & " FROM "
        sSQL = sSQL & " [TargetTable] " 'Change
        '------------------------------
        '   OPTIONAL
        '------------------------------
        If 1 = 2 Then
            sSQL = sSQL & " ORDER BY "
            sSQL = sSQL & " [lastref] "  'Change
        End If
    
        '------------------------------
        '   WB - Recordset
        '------------------------------
        Set oRS = Nothing
        Set oRS = New ADODB.Recordset
        With oRS
            .ActiveConnection = oCN
            .CursorLocation = adUseClient
            .CursorType = adOpenDynamic
            .LockType = adLockBatchOptimistic
            .Source = sSQL
            .Open
        End With
        If Not oRS.BOF And Not oRS.EOF Then
            oRS.MoveLast
            oRS.MoveFirst
            mlngRSRecCNT = oRS.RecordCount
            mlngRSFieldCNT = oRS.Fields.Count
        Else
            'Add MSG
            ExcelToAccessToExcel = False
            Exit Function
        End If
    
        '------------------------------
        '   Part 2 - Excel To Excel
        '------------------------------
        Dim oWSTarget As Excel.Worksheet
        Dim oRngCurReg As Excel.Range
        Dim oRngDummy As Excel.Range
        Dim sWSTargetName As String
        Dim lngRowCnt As Long
        Dim lngColCnt As Long
        Dim bFirstTime As Boolean
    
        '------------------------------
        '   Init
        '------------------------------
        bFirstTime = False
        sWSTargetName = "Target"    'Change to yours
        Set oWSTarget = Sheets(sWSTargetName)
        
        '------------------------------
        '   Range Object
        '------------------------------
        With oWSTarget
            Set oRngCurReg = .Range("a1").CurrentRegion
            lngRowCnt = oRngCurReg.Rows.Count
            lngColCnt = oRngCurReg.Columns.Count
        End With
    
        '------------------------------
        '   Column vs Fields Check
        '------------------------------
        '   Improve this
        '   Write your own message
        '------------------------------
        If lngRowCnt > 1 Then
            If mlngRSFieldCNT <> lngColCnt Then
                MsgBox "Field Cnt not equal Range Column Count - BAD", vbCritical, "Public Function ExcelToAccessToExcel()"
                ExcelToAccessToExcel = False
                Exit Function
            End If
        End If
    
        '------------------------------
        '   Anything in Range
        '   Improve this
        '------------------------------
        If (lngRowCnt = 1 And lngColCnt = 1) Then
            bFirstTime = True
        End If
    
        '------------------------------
        '   Improve this
        '------------------------------
        Select Case bFirstTime
        Case True
            '-------------------------
            '   Write Headings to WS Target
            '   Write Data To WS Target
            '-------------------------
            For lng = 0 To mlngRSFieldCNT - 1
                oWSTarget.Rows(1).Columns(lng + 1) = oRS.Fields(lng).Name
            Next
            With Sheets("Target").Range("A1").Offset(1)
                .CopyFromRecordset oRS
            End With
        Case False
            With oWSTarget
                Set oRngDummy = .Range(oRngCurReg.Address).Offset(oRngCurReg.Rows.Count, 0)
            End With
            oRngDummy.CopyFromRecordset oRS
        End Select
    
        '------------------------
        '   Kill Objects
        '------------------------
        On Error Resume Next
        If oRS.State > 0 Then
            oRS.Close
            Set oRS = Nothing
        Else
            Set oRS = Nothing
        End If
        If oCN.State > 0 Then
            oCN.Close
            Set oCN = Nothing
        Else
            Set oCN = Nothing
        End If
    
        '------------------------
        '   Exit
        '------------------------
        ExcelToAccessToExcel = True
        Exit Function
    EH_ExcelToAccessToExcel:
        MsgBox Err.Number & " " & Err.Description, vbCritical, "Public Function ExcelToAccessToExcel()"
        ExcelToAccessToExcel = False
        Exit Function
    
    
    End Function
    Note:
    Change This line
    sProvider = "Microsoft.Jet.OLEDB.4.0"
    To this line
    sProvider = "Microsoft.Jet.OLEDB.4.0;"
    .
    Should work either way but have not tested in Excel 2003
    Last edited by JohnM3; 08-09-2011 at 10:22 AM. Reason: Adding Semicolon

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0