Consulting

Results 1 to 5 of 5

Thread: Help: Option boxes

  1. #1
    VBAX Regular
    Joined
    Nov 2004
    Posts
    74
    Location

    Help: Option boxes

    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

    [VBA] 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[/VBA]

    But with optionboxes and I don't have a clue how to add the text (ie Dairy Milk)

  2. #2
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    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:

    [VBA]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[/VBA]
    K :-)

  3. #3
    VBAX Regular
    Joined
    Nov 2004
    Posts
    74
    Location
    Thanks Killian, I'll give it a go now.

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

    [VBA]
    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 [/VBA]


    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.

  4. #4
    Knowledge Base Approver
    Space Cadet
    VBAX Tutor sandam's Avatar
    Joined
    Jan 2005
    Location
    London
    Posts
    292
    Location
    Have you tried placing the options in a frame (e.g. fraDiaryOptions)? And then using

    [vba]For Each ctrl In Me.fraDairyOptions[/vba]

    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.
    Nothing annoys a non-conformist more than other non-conformists who refuse to conform to the rules of non-conformity.


    Confused is my normal state of mind


  5. #5
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    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
    [VBA] 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 [/VBA]
    K :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •