Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 15th, 2007, 05:10 AM
Authorized User
 
Join Date: Jun 2007
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default Excel to Access using ADO

I understand from what I have been told that to send data from Excel to Access automatically could be written in vba using ADO connection.

How could I write some code so that once the user clicks on a button that it takes a range of data and connects to a database and imports the data to the relevant table?
My data consists of columns of Text, Numbers and Currency. would it be best to change the Currency to Numbers for smooth transfer?

I am really stuck with this, can anyone help?

Thanks,
Jez

 
Old June 25th, 2007, 09:13 AM
Authorized User
 
Join Date: Jun 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi Jez,
It sounds a little bit as though you're jumping in at the deep-end. You might find this tutorial helpful..
www.justphukit.com/access/access-tutorials-1.php


Ed.
Ne auderis delere orbem rigidum meum!
 
Old July 22nd, 2007, 05:55 PM
Registered User
 
Join Date: Jul 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The following code works for me when I need to export data to Access. The example assumes the data is the a worksheet named Data and the data excluding the headings is in a range named TheDataRange. The excel data is appended to an Access table named Shipments. Also the Excel workbook and Access files are in the same folder. If your Access file is a different Folder, you will need to use the full path and name for the Access database file. Remember to add the ADO object to your reference in VBA.

   Dim ws As Worksheet
    Dim rngData As Range
    Dim cnn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Set cnn = New ADODB.Connection
    Set rst = New ADODB.Recordset

    Dim DBPathName As String
    Dim cnnstring As String

    DBPathName = ThisWorkbook.Path & "\" & "SHIPMENTS_WITH_FORECAST.mdb"
    cnnstring = "Provider=microsoft.Jet.OLEDB.4.0;Data Source = " & DBPathName
    cnn.Open cnnstring



    Dim TheTable As String
    TheTable = "Shipments"

    With rst
        .ActiveConnection = cnn
        .LockType = adLockOptimistic
        .CursorType = adOpenKeyset
        .Open TheTable, Options:=adCmdTableDirect
    End With

    Set ws = Sheets("Data")
    Set rngData = ws.Range("TheDataRange")


    Dim I As Long
    Dim J As Long

    Dim Imax As Long
    Dim Jmax As Long

    Imax = rngData.Rows.Count
    Jmax = rngData.Columns.Count


    Dim FirstRow As Integer

    FirstRow = rngData(1, 1).Row

    For I = FirstRow To FirstRow + Imax - 1
        rst.AddNew
        For J = 1 To Jmax
            If Len(ws.Cells(I, J).Value) <> 0 Then
                rst.Fields(J - 1) = ws.Cells(I, J).Value
            End If
        Next J
        rst.Update
    Next I

    rst.Close
    cnn.Close
    Set cnn = Nothing
    Set rst = Nothing








Similar Threads
Thread Thread Starter Forum Replies Last Post
ADO.NET and Excel Theone84 ADO.NET 0 July 7th, 2008 05:20 AM
Using ADO to Query Excel alex_w Excel VBA 1 October 19th, 2006 04:42 AM
Using ADO to Query Excel alex_w Access VBA 1 May 18th, 2005 11:59 AM
ado excel/access loane Excel VBA 5 August 17th, 2004 10:18 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.