• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3424
  • Last Modified:

Paste Append data from clipboard to table

I have a command button and am looking for a macro action or vba to trigger data to be copied from the clipboard onto a table.  Any idea's?
0
JSSenior
Asked:
JSSenior
2 Solutions
 
BusyMamaCommented:
I think we need more information ... is the command button on a form in Access, or on an Excel sheet?  How does the data get on the clipboard?  I am assuming the table is in Access already.
0
 
JSSeniorAuthor Commented:
Copied from Excel.  Command button is on Access form and table is already in Access.

Thanks
0
 
BusyMamaCommented:
Excel and Access play so nicely together that there are a couple of different ways I would do this, it depends on how firm you are on wanting to copy & paste.

You can turn your Excel document into a type of "submission" form, with a button in Excel that will send the data to the correct table in Access instead of copying then going to Access & pasting.  A brief, generic code sample is attached at the end of this message.

Or, you could also link the Excel document to Access via a linked table.  If you did that, you could create an Append query in Access and use your existing button on your existing for to just run the Append query.

But, I think if you want to just paste, something like this on the existing button should work:

DoCmd.GoToRecord , , acNewRec

DoCmd.DoMenuItem acFormBar, acEditMenu, acPaste, , acMenuVer70

'This is sample code for if you want to insert FROM Excel instead of in Access
Private Sub CommandButton1_Click()

   Dim db As Database, rs As Recordset, r As Long
   Set db = OpenDatabase("DriveLetter:\DirectoryName\DirectoryName\DatabaseName.mdb")
     'open the database
    Set rs = db.OpenRecordset("TABLENAME", dbOpenTable)
        With rs
           .AddNew ' create a new record
             'add values to each field in the record
            .Fields("TableFieldName1") = Range("B" & 4).Value
            .Fields("TableFieldName2") = Range("B" & 5).Value
            .Fields("TableFieldName3") = Range("B" & 6).Value
            .Fields("TableFieldName4") = Range("B" & 7).Value
            .Fields("TableFieldName5") = Range("B" & 8).Value
            .Fields("TableFieldName6") = Range("B" & 9).Value
            .Fields("TableFieldName7") = Range("B" & 10).Value
            .Update ' stores the new record
        End With
    rs.Close
    Set rs = Nothing
    db.Close
    Set db = Nothing
    
End Sub

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:

re: DoCmd.DoMenuItem acFormBar, acEditMenu, acPaste, , acMenuVer70
I might just note the Microsoft deprecated the DoMenuItem commands more than 10 years ago.

It's better to use the DoCmd.RunCommand equivalent which is

Docmd.RunCommand acCmdPaste

because there is no guarantee the Microsoft will continue to provide backward compatibility for those commands in the future.

mx


0
 
BusyMamaCommented:
Good to know ... since I'm still using it the other way!  Thanks, mx!
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Hey ... you are welcome.

mx
0
 
Jeffrey CoachmanCommented:
Also note that there are more structured ways to do this that are built into Access.
Using the clipboard can cause issues if other programs (that you may not be aware of) are also accessing it.

Can you explain a bit more about this "data"?
Where is it from?
How are you selecting/copying it?
Are these ad hoc imports or, fairly repetitive?
...etc

Typically you can transfer data to Access in any number of ways that will allow you to "Paste" the data.
TransferText
TranferSpreadsheet
...etc

JeffCoachman
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now