Solved

Paste Append data from clipboard to table

Posted on 2011-09-26
7
2,609 Views
Last Modified: 2012-05-12
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
Comment
Question by:JSSenior
7 Comments
 
LVL 7

Expert Comment

by:BusyMama
ID: 36599336
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
 

Author Comment

by:JSSenior
ID: 36599488
Copied from Excel.  Command button is on Access form and table is already in Access.

Thanks
0
 
LVL 7

Accepted Solution

by:
BusyMama earned 250 total points
ID: 36599702
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Access MVP)
DatabaseMX (Joe Anderson - Access MVP) earned 250 total points
ID: 36600215

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
 
LVL 7

Expert Comment

by:BusyMama
ID: 36600516
Good to know ... since I'm still using it the other way!  Thanks, mx!
0
 
LVL 75
ID: 36600617
Hey ... you are welcome.

mx
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 36627860
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question