cut and paste from a combo box into a memo field

J

Jay

I have a combo box "cboReferences" and a memo field "message" (in a text
box) on a form. I would like to be able to do the following:

1. Make a selection from combo box
2. Place cursor at the appropriate point in memo field
3. Click an "Insert" button and have it copy the selection in the combo
box and paste it at what was the cursor location in the memo field.

Any suggestions.
Thanks.
 
J

Jay

Clarification: the copy should be the bound column not the displayed
column in the combo box.


_____

From: Jay [mailto:[email protected]]
Posted At: Friday, March 19, 2010 12:26 PM
Posted To: microsoft.public.access.modulesdaovba
Conversation: cut and paste from a combo box into a memo field
Subject: cut and paste from a combo box into a memo field


I have a combo box "cboReferences" and a memo field "message" (in a text
box) on a form. I would like to be able to do the following:

1. Make a selection from combo box
2. Place cursor at the appropriate point in memo field
3. Click an "Insert" button and have it copy the selection in the combo
box and paste it at what was the cursor location in the memo field.

Any suggestions.
Thanks.
 
L

Linq Adams via AccessMonster.com

I've seen this question a number of times over the years, in fact saw one
just last week, I think, and have never seen a solution to the problem.
Besides the hairier question of determining exactly where the cursor is in
the memo field, when you click on the "Insert" button the cursor is no
longer there, besides focus has switched to the command button.

I'd certainly like to see an answer to it myself!
 
J

John Spencer

It can be done in two ways that I can think of.

A) A form level variable that records the position of the cursor in the memo
field. Then you can set focus to the control and paste the information into
that point.

B) Have a menu-level button so that the focus is not lost by the control on
the form.

I have not attempted to code either solution, but I don't foresee any major
problem with either solution.

I do have something similar that I use to findNext and highlight a string
repeatedly in a text box. I just threw this together quickly one day. I'm
sure it could be cleaner, but it is not in any of my production databases -
personal use only.

Option Compare Database
Option Explicit

Dim LLastPosition As Long 'Store cursor position in control fText

Private Sub btnFindInText_Click()
Dim lStartPos As Long
Dim LshowLength As Long

If InStr(1, Me.fText, Me.txtFindPhrase) = 0 Or _
Len(Me.txtFindPhrase & "") = 0 Then

Beep
'Force any highlighted text to lose the highlight
Me.Visible = False
DoEvents
Me.Visible = True
Me.fText.SetFocus
Me.fText.SelStart = LLastPosition

Else

Me.fText.SetFocus

If LLastPosition = 0 Then
lStartPos = 1
Else
lStartPos = LLastPosition + Len(Me.txtFindPhrase)
End If

lStartPos = InStr(lStartPos, Me.fText, Me.txtFindPhrase)

'Phrase exists but earlier in string, so check from start
If lStartPos = 0 Then
lStartPos = 1
lStartPos = InStr(lStartPos, Me.fText, Me.txtFindPhrase)
End If

If lStartPos > 0 Then
lStartPos = lStartPos - 1
LshowLength = Len(Me.txtFindPhrase)
Else
LshowLength = 0
End If

Me.fText.SelStart = lStartPos
Me.fText.SelLength = LshowLength

End If

End Sub


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 
J

Jay

Thanks. "A" makes the most sense to me because I am using a Runtime app
and using forms for navigation not menus. Can you post the code to
record the cursor position in the memo field. Then, would it be better
to put this code in the BeforeUpdate or use a different event of the
memo field.

-----Original Message-----
From: John Spencer [mailto:[email protected]]
Posted At: Saturday, March 20, 2010 7:12 AM
Posted To: microsoft.public.access.modulesdaovba
Conversation: cut and paste from a combo box into a memo field
Subject: Re: Clarification: the copy should be the bound column not the
displayed column in the combo box.Re: cut and paste from a combo box
into a memo field


It can be done in two ways that I can think of.

A) A form level variable that records the position of the cursor in the
memo field. Then you can set focus to the control and paste the
information into that point.

B) Have a menu-level button so that the focus is not lost by the control
on the form.

I have not attempted to code either solution, but I don't foresee any
major problem with either solution.

I do have something similar that I use to findNext and highlight a
string repeatedly in a text box. I just threw this together quickly one
day. I'm sure it could be cleaner, but it is not in any of my
production databases - personal use only.

Option Compare Database
Option Explicit

Dim LLastPosition As Long 'Store cursor position in control fText

Private Sub btnFindInText_Click()
Dim lStartPos As Long
Dim LshowLength As Long

If InStr(1, Me.fText, Me.txtFindPhrase) = 0 Or _
Len(Me.txtFindPhrase & "") = 0 Then

Beep
'Force any highlighted text to lose the highlight
Me.Visible = False
DoEvents
Me.Visible = True
Me.fText.SetFocus
Me.fText.SelStart = LLastPosition

Else

Me.fText.SetFocus

If LLastPosition = 0 Then
lStartPos = 1
Else
lStartPos = LLastPosition + Len(Me.txtFindPhrase)
End If

lStartPos = InStr(lStartPos, Me.fText, Me.txtFindPhrase)

'Phrase exists but earlier in string, so check from start
If lStartPos = 0 Then
lStartPos = 1
lStartPos = InStr(lStartPos, Me.fText, Me.txtFindPhrase)
End If

If lStartPos > 0 Then
lStartPos = lStartPos - 1
LshowLength = Len(Me.txtFindPhrase)
Else
LshowLength = 0
End If

Me.fText.SelStart = lStartPos
Me.fText.SelLength = LshowLength

End If

End Sub


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 
J

John Spencer

In this example I used the Lost Focus event of the memo control.

Private Sub fText_LostFocus()
LLastPosition = Me.fText.SelStart
End Sub


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 
L

Linq Adams via AccessMonster.com

Simplicity itself, John! I never realized the SelStart property could be used
to retrieve the cursor position! Thought it was only used to set the position.


How are things in Bawlmer? Still got snow on the ground?
 
J

Jay

Hi John,

I need a little more help with this. Got the SelStart part just fine.
Even tried using the Mid function to parse the text box into two
sections (one before the SelStart and one after) and then insert my
combo box data. The problem is that the source for the text box is a
memo field and is formatted as rich text. When I parse the text using
MID into a variable I get the <div> html text included which throws off
the character count (the SelStart doesn't count the HTML code when
calculating the cursor position in the text box).

Therefore, I wonder if there is a way to Copy the combo box selection
and then Paste it at the SelStart position of the text box. VBA help
says there is a .Copy command but I cannot get it to work.

Thanks.

-----Original Message-----
From: John Spencer [mailto:[email protected]]
Posted At: Saturday, March 20, 2010 12:54 PM
Posted To: microsoft.public.access.modulesdaovba
Conversation: cut and paste from a combo box into a memo field
Subject: Re: cut and paste from a combo box into a memo field


In this example I used the Lost Focus event of the memo control.

Private Sub fText_LostFocus()
LLastPosition = Me.fText.SelStart
End Sub


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
Thanks. "A" makes the most sense to me because I am using a Runtime
app and using forms for navigation not menus. Can you post the code to
record the cursor position in the memo field. Then, would it be better
to put this code in the BeforeUpdate or use a different event of the
memo field.

-----Original Message-----
From: John Spencer [mailto:[email protected]] Posted At: Saturday,
March 20, 2010 7:12 AM Posted To:
microsoft.public.access.modulesdaovba
Conversation: cut and paste from a combo box into a memo field
Subject: Re: Clarification: the copy should be the bound column not
the displayed column in the combo box.Re: cut and paste from a combo
box into a memo field


It can be done in two ways that I can think of.

A) A form level variable that records the position of the cursor in
the memo field. Then you can set focus to the control and paste the
information into that point.

B) Have a menu-level button so that the focus is not lost by the
control on the form.

I have not attempted to code either solution, but I don't foresee any
major problem with either solution.

I do have something similar that I use to findNext and highlight a
string repeatedly in a text box. I just threw this together quickly
one day. I'm sure it could be cleaner, but it is not in any of my
production databases - personal use only.


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 
J

Jay

Never mind. I got it. Here is the code.

Field to have item inserted into (txtMessage)
Private Sub fText_LostFocus()
LLastPosition = Me.fText.SelStart
End Sub

Code on command button (cmdInsert)
Private Sub cmdInsert_Click()
Me.cboReferences.SetFocus
DoCmd.RunCommand acCmdCopy

Me.txtMessage.SetFocus
Me.txtMessage.SelStart = lLastPosition
DoCmd.RunCommand acCmdPaste

Me.Form.Dirty = False
Nd

-----Original Message-----
From: Jay [mailto:[email protected]]
Posted At: Saturday, March 20, 2010 6:27 PM
Posted To: microsoft.public.access.modulesdaovba
Conversation: cut and paste from a combo box into a memo field
Subject: Re: cut and paste from a combo box into a memo field


Hi John,

I need a little more help with this. Got the SelStart part just fine.
Even tried using the Mid function to parse the text box into two
sections (one before the SelStart and one after) and then insert my
combo box data. The problem is that the source for the text box is a
memo field and is formatted as rich text. When I parse the text using
MID into a variable I get the <div> html text included which throws off
the character count (the SelStart doesn't count the HTML code when
calculating the cursor position in the text box).

Therefore, I wonder if there is a way to Copy the combo box selection
and then Paste it at the SelStart position of the text box. VBA help
says there is a .Copy command but I cannot get it to work.

Thanks.

-----Original Message-----
From: John Spencer [mailto:[email protected]] Posted At: Saturday, March
20, 2010 12:54 PM Posted To: microsoft.public.access.modulesdaovba
Conversation: cut and paste from a combo box into a memo field
Subject: Re: cut and paste from a combo box into a memo field


In this example I used the Lost Focus event of the memo control.

Private Sub fText_LostFocus()
LLastPosition = Me.fText.SelStart
End Sub


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
Thanks. "A" makes the most sense to me because I am using a Runtime
app and using forms for navigation not menus. Can you post the code to
record the cursor position in the memo field. Then, would it be better
to put this code in the BeforeUpdate or use a different event of the
memo field.

-----Original Message-----
From: John Spencer [mailto:[email protected]] Posted At: Saturday,
March 20, 2010 7:12 AM Posted To:
microsoft.public.access.modulesdaovba
Conversation: cut and paste from a combo box into a memo field
Subject: Re: Clarification: the copy should be the bound column not
the displayed column in the combo box.Re: cut and paste from a combo
box into a memo field


It can be done in two ways that I can think of.

A) A form level variable that records the position of the cursor in
the memo field. Then you can set focus to the control and paste the
information into that point.

B) Have a menu-level button so that the focus is not lost by the
control on the form.

I have not attempted to code either solution, but I don't foresee any
major problem with either solution.

I do have something similar that I use to findNext and highlight a
string repeatedly in a text box. I just threw this together quickly
one day. I'm sure it could be cleaner, but it is not in any of my
production databases - personal use only.


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 
J

John Spencer

Snow is long gone (well the last of the piles in the parking lots melted about
a week ago).

Last two days have been sunny and in the low 70's.

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 
J

John Spencer

Good to see that you worked that out and that it works for you

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
Never mind. I got it. Here is the code.

Field to have item inserted into (txtMessage)
Private Sub fText_LostFocus()
LLastPosition = Me.fText.SelStart
End Sub

Code on command button (cmdInsert)
Private Sub cmdInsert_Click()
Me.cboReferences.SetFocus
DoCmd.RunCommand acCmdCopy

Me.txtMessage.SetFocus
Me.txtMessage.SelStart = lLastPosition
DoCmd.RunCommand acCmdPaste

Me.Form.Dirty = False
Nd

-----Original Message-----
From: Jay [mailto:[email protected]]
Posted At: Saturday, March 20, 2010 6:27 PM
Posted To: microsoft.public.access.modulesdaovba
Conversation: cut and paste from a combo box into a memo field
Subject: Re: cut and paste from a combo box into a memo field


Hi John,

I need a little more help with this. Got the SelStart part just fine.
Even tried using the Mid function to parse the text box into two
sections (one before the SelStart and one after) and then insert my
combo box data. The problem is that the source for the text box is a
memo field and is formatted as rich text. When I parse the text using
MID into a variable I get the <div> html text included which throws off
the character count (the SelStart doesn't count the HTML code when
calculating the cursor position in the text box).

Therefore, I wonder if there is a way to Copy the combo box selection
and then Paste it at the SelStart position of the text box. VBA help
says there is a .Copy command but I cannot get it to work.

Thanks.

-----Original Message-----
From: John Spencer [mailto:[email protected]] Posted At: Saturday, March
20, 2010 12:54 PM Posted To: microsoft.public.access.modulesdaovba
Conversation: cut and paste from a combo box into a memo field
Subject: Re: cut and paste from a combo box into a memo field


In this example I used the Lost Focus event of the memo control.

Private Sub fText_LostFocus()
LLastPosition = Me.fText.SelStart
End Sub


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
Thanks. "A" makes the most sense to me because I am using a Runtime
app and using forms for navigation not menus. Can you post the code to
record the cursor position in the memo field. Then, would it be better
to put this code in the BeforeUpdate or use a different event of the
memo field.

-----Original Message-----
From: John Spencer [mailto:[email protected]] Posted At: Saturday,
March 20, 2010 7:12 AM Posted To:
microsoft.public.access.modulesdaovba
Conversation: cut and paste from a combo box into a memo field
Subject: Re: Clarification: the copy should be the bound column not
the displayed column in the combo box.Re: cut and paste from a combo
box into a memo field


It can be done in two ways that I can think of.

A) A form level variable that records the position of the cursor in
the memo field. Then you can set focus to the control and paste the
information into that point.

B) Have a menu-level button so that the focus is not lost by the
control on the form.

I have not attempted to code either solution, but I don't foresee any
major problem with either solution.

I do have something similar that I use to findNext and highlight a
string repeatedly in a text box. I just threw this together quickly
one day. I'm sure it could be cleaner, but it is not in any of my
production databases - personal use only.


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top