How To Send, Copy Information To The Clipboard Using A Button. Access Vba Tutorial
Ever wanted to copy the contents of a control, i.e., a text box or similar to the clipboard from a MS Access form so the contents can be pasted into another field or application? Considering how pervasive the clipboard is on Windows computers you would think this is a simple task! Not so.
The usual solution of pressing the key combination, Ctrl-C, could not be used for fear of accidentally changing the text box contents. So I figured the easiest solution would be to use either a double click or button to do the copying. Simple! Not really, until you know the answer that is.
I don’t know about you but I write my own MS Access databases, only smallish programs usually but they get the job done and, like everyone else, I find problems which I’m unable to solve. Solution to the problem! A quick search of the internet.
Well the search went on and on and I was getting rather annoyed that such a simple task was taking so long. After all, with the advent of oops programming the problem should be as simple as writing a function to copying the object to the clipboard:
On Error GoTo Err_CopyToClipboard_btn_Click
‘Save any edits first.
If Me.Dirty Then
Me.Dirty = False
Clipboard = Me.TheTextBox
Unfortunately, this and many other scenarios did not work.
A move in the right direction was to use the function statement:
but this also proved a nightmare … at first.
The problem of using the, DoCmd.RunCommand acCmdCopy, statement is that my default setting for the “Behavior Entering Field” property of the database is set to “Go to start of field” and not “Select entire field”, (Options).
The solution is to select the entire field before trying to copy the data to the clipboard.
me.TheTextBox.SelStart = 0
me.TheTextBox.SelLength = Len(me. TheTextBox)
Simple, when you know how.
This solution was provided by, Dale.
Well, I hope you found something useful and enjoyed this session, please don’t forget to let me have your thoughts or even a better solution. See you soon.