PDA

View Full Version : Help: Option boxes



newk
03-04-2005, 02:52 AM
Hi guys,

I need some help with option boxes and VB. I would like to offer 3 choices. Lets say;

Opt1) Dairy Milk

Opt2) Chocolate Orange

Opt3) Fudge

So I put them on the userform. Now I would like the user to be able to select any combination of the 3 including all of them. Once they select their choice I would like the output to be linked to a bookmark.

Therefore if someone selects Opt1 & Opt3 the output would be;
___________________

Dairy Milk

Fudge______________


I think I need something like

Dim aText As String
aText = ""
For i = 1 To 3
If frmStandardLetter("Text" & i).Text <> "" Then
aText = aText & frmStandardLetter("Textbox" & i).Text & Chr(13)
End If
Next
aText = Left(aText, Len(aText) - 1)
Selection.GoTo What:=wdGoToBookmark, Name:="Choice"
Selection.TypeText Text:=aText
But with optionboxes and I don't have a clue how to add the text (ie Dairy Milk)

Killian
03-04-2005, 03:17 AM
I think the way to go here would be to use checkboxes so you can have multiple selections and put the text in the Caption. It's also a good idea to use the standard naming convention for all the form object names like you did with the form (chkFudge, optFudge, lblFudge, txtFudge etc) not least because it gives you an easy way to loop through all the objects of a particular type on a form.
So, your code would then be something like:

Dim aText As String
Dim ctrl As Control

aText = ""
For Each ctrl In Me.Controls
If Left(ctrl.Name, 3) = "chk" Then
If ctrl.Value = True Then
aText = aText & ctrl.Caption & Chr(13)
End If
End If
Next

'the final text string
MsgBox aText

newk
03-04-2005, 03:32 AM
Thanks Killian, I'll give it a go now.

Hi Killian, I need another prompt. I just can't get it to work.



Dim aText As String
Dim ctrl As Control

aText = ""
For Each ctrl In Me.Controls
If Left(ctrl.Name, 3) = "chk" Then
If ctrl.Value = True Then
aText = aText & ctrl.Caption & Chr(13)
End If
End If
Next

MsgBox aText


Also what if I wanted to add a sentence instead of just the caption. Therefore if optFudge and optchocolateorange are selected, it outputs:
____________________________
Fudge- Cadburys fudge covered in milk chocolate

Chocolate Orange - Segments of Terry's chocolate orange.
____________________________

Lets just say these to sentences are too long for a caption on a checkbox. I think if I see this I will get it.

Sorry for not getting it, I feel like a bit of a spak.

sandam
03-04-2005, 06:57 AM
Have you tried placing the options in a frame (e.g. fraDiaryOptions)? And then using

For Each ctrl In Me.fraDairyOptions

Because option buttons as well as checkboxes work best in frames. epsecially when you use more than one set on a userform. the frame encompasses the control references so that they are easier to get a hold of. :)

Killian
03-04-2005, 07:00 AM
You're not a spak... I just didn't explain it properly :)

Add a userform that contains some CheckBoxes and a CommandButton.
The CheckBoxes' "Name" property should start with "chk" e.g. chkFudge, chkOrange and they should have the appropriate caption e.g. "Cadbury's Fudge", "Terry's Chocolate Orange"
If you put the code into the click event of the commandbutton, the message box will display the string.

With the additional strings, there are a number of ways to do that but here's the original code with a Select Case approach which does the job
Dim aText As String, bText As String
Dim ctrl As Control
aText = ""
For Each ctrl In Me.Controls
If Left(ctrl.Name, 3) = "chk" Then
If ctrl.Value = True Then
'select which string we want to add
Select Case ctrl.Name
Case "chkFudge"
bText = ctrl.Caption & " - Cadburys fudge covered in milk chocolate"
Case "chkOrange"
bText = ctrl.Caption & " - Segments of Terry's chocolate orange"
End Select
aText = aText & bText & Chr(13)
End If
End If
Next
MsgBox aText