Insert a string into a specified location in another string

G

Guest

To facilitate form data entry, I want users to be able to click on items
within a list box and have that text be inserted into a specific area of a
text box. I have adapted a code to highlight "<>" with in the text box to
serve as the insertion target. For example, the text box reads, "The customer
ordered a <> basket". The list box choices are "small", "medium", and
"large". I would like the user to be able to click one of these options and
have that text inserted into the string at the "<>" marker: "The customer
ordered a large basket". Any help would be greatly appreciated. Thanks.

Option Compare Database

Private Sub Form_Load()

Dim ctlTextToSearch As Control
Set ctlTextToSearch = Forms!Form1!Textbox1

' SetFocus to text box.
ctlTextToSearch.SetFocus
ctlTextToSearch.Text = "The customer ordered a <> basket"
Set ctlTextToSearch = Nothing

End Sub

Public Sub Find_Click()

Dim strSearch As String
Dim intWhere As Integer
Dim ctlTextToSearch As Control

' find the first "<>".
With Me!Textbox1
strSearch = "<>"

' Find string in text.
intWhere = InStr(.Value, strSearch)
If intWhere Then
'If found.
.SetFocus
.SelStart = intWhere - 1
.SelLength = Len(strSearch)
Else
' Notify user.
MsgBox "String not found."
End If
End With

End Sub
 
A

Al Camp

Steve,
If the text is always "The customer ordered a X basket" just send the
whole string, instead of a complicated insertion technique.

First, the code should fire on the the AfterUpdate event of your list
(ex. lstSize)

Private Sub lstSize_AfterUpdate()
[YourTextFieldName] = "The customer ordered a " & lstSize & " basket."
End Sub

Not sure why you're doing this on a form. Usually the listbox itself
would be "bound" to the Size field in your table, and just display as Small,
Medium, or Large. Usually phrase concatenations are only necessary in
reports.
 
J

Jeff Boyce

Steve

It may not be relevant in your situation, but the first thing that occurred
to me when I read your post was to use a concatenated string, rather than
the select/insert method you described.

I imagined a query that has a field made up of something like:

Test: "The customer ordered the " & [YourItemSizeField] & " size."

Or maybe I just don't get it...<g>
 
G

Guest

I understand what you are saying. I've simplified my example. I am actually
trying to facilitate the completion of a templated paragraph that will
ultimately be used in a generating a medical letter. Currently, I am using a
conatenation that adds text on to the end of the field (i.e., textfield =
textfield & list box choice). But this only allows me to add text to the end
of the section. I would like to be able to insert text at selected areas. For
example:

"The patient first noted the symptom <>. The problem lasts <> minutes. The
problem is relieved by <>..."

I will make the record source for the list box will change at the user works
through the template to make the options relevant to that particular portion
of the text. I just need a way to target the insertion to the selected marker
<>. Any ideas? Thanks.

Jeff Boyce said:
Steve

It may not be relevant in your situation, but the first thing that occurred
to me when I read your post was to use a concatenated string, rather than
the select/insert method you described.

I imagined a query that has a field made up of something like:

Test: "The customer ordered the " & [YourItemSizeField] & " size."

Or maybe I just don't get it...<g>

--
Regards

Jeff Boyce
<Office/Access MVP>

Steve said:
To facilitate form data entry, I want users to be able to click on items
within a list box and have that text be inserted into a specific area of a
text box. I have adapted a code to highlight "<>" with in the text box to
serve as the insertion target. For example, the text box reads, "The customer
ordered a <> basket". The list box choices are "small", "medium", and
"large". I would like the user to be able to click one of these options and
have that text inserted into the string at the "<>" marker: "The customer
ordered a large basket". Any help would be greatly appreciated. Thanks.

Option Compare Database

Private Sub Form_Load()

Dim ctlTextToSearch As Control
Set ctlTextToSearch = Forms!Form1!Textbox1

' SetFocus to text box.
ctlTextToSearch.SetFocus
ctlTextToSearch.Text = "The customer ordered a <> basket"
Set ctlTextToSearch = Nothing

End Sub

Public Sub Find_Click()

Dim strSearch As String
Dim intWhere As Integer
Dim ctlTextToSearch As Control

' find the first "<>".
With Me!Textbox1
strSearch = "<>"

' Find string in text.
intWhere = InStr(.Value, strSearch)
If intWhere Then
'If found.
.SetFocus
.SelStart = intWhere - 1
.SelLength = Len(strSearch)
Else
' Notify user.
MsgBox "String not found."
End If
End With

End Sub
 
G

Guest

I understand what you are saying. I've simplified my example. I am actually
trying to facilitate the completion of a templated paragraph that will
ultimately be used in a generating a medical letter. Currently, I am using a
conatenation that adds text on to the end of the field (i.e., textfield =
textfield & list box choice). But this only allows me to add text to the end
of the section. I would like to be able to insert text at selected areas. For
example:

"The patient first noted the symptom <>. The problem lasts <> minutes. The
problem is relieved by <>..."

I will make the record source for the list box will change at the user works
through the template to make the options relevant to that particular portion
of the text. I just need a way to target the insertion to the selected marker
<>. Any ideas? Thanks.




Al Camp said:
Steve,
If the text is always "The customer ordered a X basket" just send the
whole string, instead of a complicated insertion technique.

First, the code should fire on the the AfterUpdate event of your list
(ex. lstSize)

Private Sub lstSize_AfterUpdate()
[YourTextFieldName] = "The customer ordered a " & lstSize & " basket."
End Sub

Not sure why you're doing this on a form. Usually the listbox itself
would be "bound" to the Size field in your table, and just display as Small,
Medium, or Large. Usually phrase concatenations are only necessary in
reports.
--
hth
Al Camp
Candia Computer Consulting - Candia NH
http://home.comcast.net/~cccsolutions


Steve said:
To facilitate form data entry, I want users to be able to click on items
within a list box and have that text be inserted into a specific area of a
text box. I have adapted a code to highlight "<>" with in the text box to
serve as the insertion target. For example, the text box reads, "The
customer
ordered a <> basket". The list box choices are "small", "medium", and
"large". I would like the user to be able to click one of these options
and
have that text inserted into the string at the "<>" marker: "The customer
ordered a large basket". Any help would be greatly appreciated. Thanks.

Option Compare Database

Private Sub Form_Load()

Dim ctlTextToSearch As Control
Set ctlTextToSearch = Forms!Form1!Textbox1

' SetFocus to text box.
ctlTextToSearch.SetFocus
ctlTextToSearch.Text = "The customer ordered a <> basket"
Set ctlTextToSearch = Nothing

End Sub

Public Sub Find_Click()

Dim strSearch As String
Dim intWhere As Integer
Dim ctlTextToSearch As Control

' find the first "<>".
With Me!Textbox1
strSearch = "<>"

' Find string in text.
intWhere = InStr(.Value, strSearch)
If intWhere Then
'If found.
.SetFocus
.SelStart = intWhere - 1
.SelLength = Len(strSearch)
Else
' Notify user.
MsgBox "String not found."
End If
End With

End Sub
 
M

MtnWindow

It seems like you could do it with the Control Source of the text box
set to something like this:

="The patient first noted the symptom " & [ComboWhen] & ". The problem
lasts " & [ComboHowLong] & " minutes."

Or if the text box is bound to a field (Paragraph1), then in the After
Update events for the combo boxes:

Paragraph1 = "The patient first noted the symptom " & [ComboWhen] & ".
The problem lasts " & [ComboHowLong] & " minutes."
 
J

Jeff Boyce

Steve

If you are using a letter, consider combining Access and Word. You could
create a merge document in Word, a query in Access, and write the output of
the query to a Word merge data doc (to be used by your letter/merge
document).

This approach gives you MUCH more control over layout/formatting than trying
to embed large chunks of text in an Access report.

Regards

Jeff Boyce
<Office/Access MVP>

Steve said:
I understand what you are saying. I've simplified my example. I am actually
trying to facilitate the completion of a templated paragraph that will
ultimately be used in a generating a medical letter. Currently, I am
using a
conatenation that adds text on to the end of the field (i.e., textfield =
textfield & list box choice). But this only allows me to add text to the
end
of the section. I would like to be able to insert text at selected areas.
For
example:

"The patient first noted the symptom <>. The problem lasts <> minutes. The
problem is relieved by <>..."

I will make the record source for the list box will change at the user
works
through the template to make the options relevant to that particular
portion
of the text. I just need a way to target the insertion to the selected
marker
<>. Any ideas? Thanks.

Jeff Boyce said:
Steve

It may not be relevant in your situation, but the first thing that
occurred
to me when I read your post was to use a concatenated string, rather than
the select/insert method you described.

I imagined a query that has a field made up of something like:

Test: "The customer ordered the " & [YourItemSizeField] & " size."

Or maybe I just don't get it...<g>

--
Regards

Jeff Boyce
<Office/Access MVP>

Steve said:
To facilitate form data entry, I want users to be able to click on
items
within a list box and have that text be inserted into a specific area
of a
text box. I have adapted a code to highlight "<>" with in the text box
to
serve as the insertion target. For example, the text box reads, "The customer
ordered a <> basket". The list box choices are "small", "medium", and
"large". I would like the user to be able to click one of these options and
have that text inserted into the string at the "<>" marker: "The
customer
ordered a large basket". Any help would be greatly appreciated. Thanks.

Option Compare Database

Private Sub Form_Load()

Dim ctlTextToSearch As Control
Set ctlTextToSearch = Forms!Form1!Textbox1

' SetFocus to text box.
ctlTextToSearch.SetFocus
ctlTextToSearch.Text = "The customer ordered a <> basket"
Set ctlTextToSearch = Nothing

End Sub

Public Sub Find_Click()

Dim strSearch As String
Dim intWhere As Integer
Dim ctlTextToSearch As Control

' find the first "<>".
With Me!Textbox1
strSearch = "<>"

' Find string in text.
intWhere = InStr(.Value, strSearch)
If intWhere Then
'If found.
.SetFocus
.SelStart = intWhere - 1
.SelLength = Len(strSearch)
Else
' Notify user.
MsgBox "String not found."
End If
End With

End Sub
 
R

rkc

Steve said:
I understand what you are saying. I've simplified my example. I am actually
trying to facilitate the completion of a templated paragraph that will
ultimately be used in a generating a medical letter. Currently, I am using a
conatenation that adds text on to the end of the field (i.e., textfield =
textfield & list box choice). But this only allows me to add text to the end
of the section. I would like to be able to insert text at selected areas. For
example:

"The patient first noted the symptom <>. The problem lasts <> minutes. The
problem is relieved by <>..."

I will make the record source for the list box will change at the user works
through the template to make the options relevant to that particular portion
of the text. I just need a way to target the insertion to the selected marker
<>. Any ideas? Thanks.

If you can change your place holders to something like <1>, <2>,
<3>, etc. and track which one you are on, then the Replace function
makes this a pretty simple task.
 
G

Guest

The InStr Function is supposed to specify the position of the FIRST
occurrence of one string within another. When I run the following code, it
replaces every "<>" with the specified text. Why does that happen?

The text field - "The customer ordered the <> size. The customer chose the
<> color"

is changed to- "The customer ordered the Large size. The customer chose the
Large color"




Public Sub Replace_Click()


Dim strSearch As String
Dim intWhere As Integer

strSearch = "<>"

' find the first "<>".
intWhere = InStr(Textbox1, strSearch)
Textbox1 = Replace(Textbox1, Mid(Textbox1, intWhere, 2), Mid(Field1,
intWhere, 5) & "Large")

End Sub

Steve
 
D

Douglas J Steele

Replace works on all occurrances in the string.

All the InStr serves to do in your code is find the first occurrence of <>.

Your code is no different than:

Textbox1 = Replace(Textbox1, "<>", Mid(Field1, intWhere, 5) & "Large")

(except that your code will generate an error if <> isn't found in Textbox1:
i.e. if intWhere = 0)

As was suggested, you should consider changing your placeholders to <1>,
<2>, etc.
 
R

rkc

Steve said:
The InStr Function is supposed to specify the position of the FIRST
occurrence of one string within another. When I run the following code, it
replaces every "<>" with the specified text. Why does that happen?

The text field - "The customer ordered the <> size. The customer chose the
<> color"

is changed to- "The customer ordered the Large size. The customer chose the
Large color"
Public Sub Replace_Click()


Dim strSearch As String
Dim intWhere As Integer

strSearch = "<>"

' find the first "<>".
intWhere = InStr(Textbox1, strSearch)
Textbox1 = Replace(Textbox1, Mid(Textbox1, intWhere, 2), Mid(Field1,
intWhere, 5) & "Large")

End Sub

The Replace function replaces all occurances of the find string with the
replace string unless you specify the start parameter. The problem
with specifying a start parameter is that the string returned also
begins at the start parameter. The portion of the string that occurs
before the start parameter is lost. So, although at first glance it
seems like you can just step through a string using start and count,
the results are not what you would hope.

That is why I suggested unique 'bookmarks' for each portion of text
you want replaced.

"The customer ordered the <1> size. The customer chose the
<2> color"

text0 = replace(text0.value, "<1>", "large")
text0 = replace(text0.value, "<2>", "blue")
 

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