Solved

Paste Append data from clipboard to table

Posted on 2011-09-26
7
2,432 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Access MVP)
DatabaseMX (Joe Anderson - Access MVP) earned 250 total points
Comment Utility

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
Comment Utility
Good to know ... since I'm still using it the other way!  Thanks, mx!
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
Hey ... you are welcome.

mx
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now