By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,471 Members | 722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,471 IT Pros & Developers. It's quick & easy.

Appending Strings

P: n/a
I need some code to append several strings together, and insert those strings
into a text box. But the insertion should only happen when a particular
check box has been checked. For instance, I might have to check boxes
(check1, check2), and a text box (text1). If check one is checked then the
text box should read "check1;" or whatever the real label would be. If both
check1 and check2 have been checked then the textbox would read: "check1;
check2".

I have been asking alot of questions. I'm new to this, but I need to learn
fast.

Any help is greatly appreciated.

Thanks,
Shannan

--
Message posted via http://www.accessmonster.com
Nov 13 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
rkc
Shannan Casteel via AccessMonster.com wrote:
I need some code to append several strings together, and insert those strings
into a text box. But the insertion should only happen when a particular
check box has been checked. For instance, I might have to check boxes
(check1, check2), and a text box (text1). If check one is checked then the
text box should read "check1;" or whatever the real label would be. If both
check1 and check2 have been checked then the textbox would read: "check1;
check2".


Do you want the label captions on seperate lines as shown?

Do you want to remove the caption from the textbox when the
checkbox is un-selected?
Nov 13 '05 #2

P: n/a
Goto the design view of your form. Right click on a checkbox (don't
right click the label of the checkbox - you have to right click right on
the actual checkbox). When the dropdown menu comes up select "Build".
This brings you to a code window to the Click Event of that checkbox -
say check0_Click(). It will look something like this:

Private Sub Check0_Click()

End Sub

It is inside of this sub that you can write some code like this:

Private Sub Check0_Click()
If Check0.Value = -1 Then Text0 = "Check0"
If Check0.Value <> -1 Then Text6 = ""
End Sub

If you have a check mark in the checkbox then the string "Check0" will
get written to a control called Text0 - assuming you have a textbox
control called Text0. If the check mark is removed (Check0.Value = 0)
then the text from Text0 will also be removed per this code instruction
for example. You can make it do anything you want. If you don't want
to remove the text from Text0 if Check0 is unchecked then just remove
the line

If Check0.Value <> -1 Then Text6 = ""

HTH

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #3

P: n/a
rkc,

No...to your first question.
Yes...to your second question.

I have almost gotten it to work using the following code, but it won't let me
have a zero length string.

For Check box called CheckAirCleaner:

Private Sub CheckAirCleaner_Click()

If Me.CheckAirCleaner = True Then
Me.TextProblemSubcategory = (ProblemSubcategory) & "Air Cleaner;"
Else
If Me.TextProblemSubcategory.Value <> "" Then
Me.TextProblemSubcategory.Value = "NA"
End If
End If
End Sub
The next check box is coded similiarly with appropriate name changes. I was
hoping to get rid of the "NA" part and just have a blank text box when no
boxes are checked.

Right now the text box may start blank, but if I check a box like
CheckAirCleaner and then uncheck it, "Air Cleaner" is replaced with "NA"
which stays there until I manually clear it. Therefore, if I now select a
box such as CheckBearing the text box would now read "NABearing".

Thanks for your help...

Shannan

--
Message posted via http://www.accessmonster.com
Nov 13 '05 #4

P: n/a
rkc
Shannan Casteel via AccessMonster.com wrote:
rkc,

No...to your first question.
Yes...to your second question.

I have almost gotten it to work using the following code, but it won't let me
have a zero length string.

For Check box called CheckAirCleaner:

Private Sub CheckAirCleaner_Click()

If Me.CheckAirCleaner = True Then
Me.TextProblemSubcategory = (ProblemSubcategory) & "Air Cleaner;"
Else
If Me.TextProblemSubcategory.Value <> "" Then
Me.TextProblemSubcategory.Value = "NA"
End If
End If
End Sub


I can save you a bit of time and effort with this, but after reading
another post of yours on the same subject I think you may have a
table design problem.

Any way, paste the following code into your form's code module.
After you have done that, open your form in design view and
select all the checkboxes that you want to have the desired
behaviour. Once you have done that click the properties icon,
select the Events tab, scroll to the On Click Event and enter
=updateProblemSubcategory().

Close the property sheet and give the form a try.
'---- start of code ----
Function updateProblemSubcategory()
Dim sCaption As String
Dim sac As Access.Control

'find the caption of the label
Set sac = Screen.ActiveControl
sCaption = getLabelCaption(sac)

With Me.TextProblemSubcategory
Select Case sac.value
Case vbTrue 'add the caption
If Len(.value & vbNullString) > 0 Then
.value = .value & ";"
End If
.value = .value & sCaption
Case vbFalse 'remove the caption
.value = Replace(.value, sCaption, "")
If InStr(.value, ";;") > 0 Then
.value = Replace(.value, ";;", ";")
End If
If left$(.value, 1) = ";" Then
.value = Mid$(.value, 2)
End If
If Right$(.value, 1) = ";" Then
.value = left$(.value, Len(.value) - 1)
End If
Case Else
'nothing
End Select
End With

Set sac = Nothing
End Function

Private Function getLabelCaption(xs As Access.Control) As String
'returns the caption of the label attached
'to the control argument if it exists.

Dim ctl As Access.Control

For Each ctl In Me.Controls
If ctl.ControlType = acLabel Then
If ctl.parent.Name = xs.Name Then
getLabelCaption = ctl.Caption
Exit For
End If
End If
Next

Set ctl = Nothing
End Function

'---- end of code ----
Nov 13 '05 #5

P: n/a
rkc,

That worked great. Thanks for your help. Last question...Where in that code
could I change it so that spaces would be inserted between the start of a
word and a semicolon?

Thanks for your help,

Shannan

--
Message posted via http://www.accessmonster.com
Nov 13 '05 #6

P: n/a
rkc
Shannan Casteel via AccessMonster.com wrote:
I need some code to append several strings together, and insert those strings
into a text box. But the insertion should only happen when a particular
check box has been checked. For instance, I might have to check boxes
(check1, check2), and a text box (text1). If check one is checked then the
text box should read "check1;" or whatever the real label would be. If both
check1 and check2 have been checked then the textbox would read: "check1;
check2".

I have been asking alot of questions. I'm new to this, but I need to learn
fast.

Any help is greatly appreciated.

Thanks,
Shannan

Nov 13 '05 #7

P: n/a
rkc
Shannan Casteel via AccessMonster.com wrote:
rkc,

That worked great. Thanks for your help. Last question...Where in that code
could I change it so that spaces would be inserted between the start of a
word and a semicolon?


You don't want to do that. It just makes it more difficult to parse
out the single values later. As it stands if you come to your senses
and decide you want to store the values seperately all you have to
do is feed the textbox value to the Split() function.

dim v as variant
dim i as integer
v = split(Me.TextProblemSubcategory.value,";")
for i = 0 to ubound(v)
'write v(i) to the database
next
Nov 13 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.