+ Reply to Thread
Results 1 to 2 of 2

MS Excel Macro: Deleting rows in MS Access

  1. #1
    Registered User
    Join Date
    10-19-2008
    Location
    Singapore
    Posts
    3

    MS Excel Macro: Deleting rows in MS Access

    Hello

    This macro exports a column from Excel to Access.
    I need to set it so that each time it exports, it clears the Access table's ROWS of all data before pasting. Column headers NEED to remain unchanged.

    How can I do this?



    Sub EXPORT_ADO_ExcelToAccess()
    ' exports data from the active worksheet to a table in an Access database
    ' this procedure must be edited before use
    Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
    ' connect to the Access database
    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
    "Data Source=C:\Documents and Settings\HowAlex\Desktop\db1.mdb;"
    ' open a recordset
    Set rs = New ADODB.Recordset
    rs.Open "TestEquities", cn, adOpenKeyset, adLockOptimistic, adCmdTable
    ' all records in a table
    r = 2 ' the start row in the worksheet
    Do While Len(Range("A" & r).Formula) > 0
    ' repeat until first empty cell in column A
    With rs
    .AddNew ' create a new record
    ' add values to each field in the record
    .Fields("Security (ISIN)") = Range("A" & r).Value
    .Fields("Designation") = Range("B" & r).Value
    .Fields("BBG Format") = Range("C" & r).Value
    .Fields("Security Name") = Range("D" & r).Value
    .Fields("Asset Type") = Range("E" & r).Value
    .Fields("Ticker") = Range("F" & r).Value
    .Fields("Industry / Sector") = Range("G" & r).Value
    .Fields("Last Price") = Range("H" & r).Value
    .Fields("Current Market Cap") = Range("I" & r).Value
    ' add more fields if necessary...
    .Update ' stores the new record
    End With
    r = r + 1 ' next row
    Loop
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing


    ' The macro example assumes that your VBA project has added a reference to the ADO object library.




    Thank you very much

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Please take a few minutes to read the Forum Rules, and then edit your post to wrap your code with Code Tags.
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Macro that fills in cells needs to skip 2 rows
    By westonkw in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-03-2008, 05:36 PM
  2. Deleting both duplicate rows in Excel.
    By mdub72 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-06-2008, 08:35 AM
  3. linking excel pivot tables to access 2000
    By nataliem in forum Excel General
    Replies: 8
    Last Post: 10-18-2007, 07:06 PM
  4. Macro To Add Rows in Excel
    By floridagunner in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-16-2007, 04:30 PM
  5. using vba from Access to change Excel worksheet
    By qwertyjjj in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-15-2006, 07:13 PM

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