Solved

Paste Append data from clipboard to table

Posted on 2011-09-26
7
2,801 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

732 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