How to use Access DB through Excel VBA macro?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • J5lemonade
    New Member
    • May 2010
    • 4

    How to use Access DB through Excel VBA macro?

    I'm not very familiar with Access and I'm learning vba in =Excel.
    Here's what I want to do:

    Copy a column of text in Excel
    Open an access DB and replace existing items in table with copied data
    Open a query and copy and paste the results back into Excel
    I'm doing this through an ActiveX macro activated with a button click.
    I'm using office 07 but the DB is in 02-03 file format, if this makes a difference.
    My code stops running at the line: .DoCmd.RunComma nd acCmdSelectAll and from examples I've seen online I thought this would work. Could you tell me what I'm doing wrong? Thanks.

    Here's my code:

    ActiveSheet.Ran ge("A1:A" & ActiveCell.Row) .Copy
    ActiveSheet.Ran ge("A1").Selec t

    ' This section retrieves the Vendor Data from the Database
    Dim FilePath As String
    Dim AccDatabase As Object
    Dim counter As Integer
    FilePath = "C:\Personal\Ma cros\PartSuppli er.mdb"
    Set AccDatabase = CreateObject("A ccess.Applicati on")
    With AccDatabase
    .Visible = True 'Will change to False when macro works
    .OpenCurrentDat abase FilePath
    .DoCmd.OpenTabl e "tblFindPar ts"
    .DoCmd.SetWarni ngs False
    .DoCmd.RunComma nd acCmdSelectAll
    .DoCmd.RunComma nd acCmdDelete

    ' Other code goess here to open query and copy and paste data

    End With
    Set AccDatabase = Nothing
  • J5lemonade
    New Member
    • May 2010
    • 4

    #2
    Something I forgot

    I forgot something:

    After the .DoCmd.RunComma nd acCmdDelete line I would paste in the contents of the cells I originally copied. Doing the "delete" manually, I get a message asking if I want to delete XX number of records, and I select "yes". I don't know if I need extra code to bypass/affirm this prompt?

    Any help will be appreciated. Thanks.

    Comment

    • J5lemonade
      New Member
      • May 2010
      • 4

      #3
      I figured it out

      FYI, I figured out how to do it using SendKeys:

      With AccDatabase
      .Visible = True '//Needs to be visible for SendKey
      .OpenCurrentDat abase FilePath
      .DoCmd.OpenTabl e "tblFindPar ts"
      SendKeys "^a", True '//Selects all existing records
      SendKeys "{DEL}", True '//Deletes existing records
      SendKeys "^v", True '//Pastes items on clipboard
      .DoCmd.OpenQuer y "qrySupplierNam e"
      DoEvents '//Waits for query to populate results
      SendKeys "^a", True '//Selects all existing records
      SendKeys "^c", True '//Selects all query results
      .DoCmd.Quit
      End With

      Comment

      Working...