Results 1 to 14 of 14

Thread: [RESOLVED] Select case for checkButton to build a string

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    11

    Resolved [RESOLVED] Select case for checkButton to build a string

    Afternoon all

    Slight problem, i've got several check buttons on a form so that the user can select more than one option. I thought that a case statement would be the best way to check which of the values are set to "True". If the value is True then a word would be passed to a string. Problem being that my string just isn't building up depending on what options have been selected.

    Here take a look at this:-
    Code:
    Select Case Appointment
        Case chkDentist.Value = True
            Appointment = "Dentist"
        Case chkDoctor.Value = True
            Appointment = Appointment & "Doctor "
        Case chkHospital.Value = True
            Appointment = Appointment & ", " & "Hospital "
        Case chkOptician.Value = True
            Appointment = Appointment & ", " & "Optician "
        Case chkOther.Value = True
            Appointment = Appointment & ", " & txtOther.Text
    End Select
    I can't see why this isn't working, the variable Appointment is set as a string and should be creating a string of options

    Your help on this would be great

    Thanks

    Nick

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Select case for checkButton to build a string

    Where is Appointment being set to allow the select Case statement to run against? This might actually be better served in a series of If statements.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    11

    Re: Select case for checkButton to build a string

    Appointment is set as a public string variable in the module

    yea i am thinking of just changing it to a series of IF's

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Select case for checkButton to build a string

    In that case I don't think the Select case will work correctly. You are checking a string var against a True/False vale (the check). The If statements are the way I would go.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    11

    Re: Select case for checkButton to build a string

    I'll give that a go then let you know

    i don't know eh, this is what you get when you think to much into things, thanks

  6. #6
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Select case for checkButton to build a string

    As I can see, the Select Case is wrong
    And the If else would be the best soultion.
    And make sure its not a If ... ElseIF ... End If.
    You would have to check for each check box with If .. End If and append to Appointment . Other wise the If ... ElseIF ... EndIf would append only one Text box value.
    Got it?
    IIF(Post.Rate > 0 , , )

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    11

    Re: Select case for checkButton to build a string

    Yes i have just realised that if ... else ... end if isn't the best way either, quite a messy way again!
    Code:
    If chkDentist.Value = True Then
            Appointment = "Dentist "
    End If
    If chkDoctor.Value = True Then
            Appointment = "Doctor "
            Else
            Appointment = Appointment & ", " & "Doctor "
    End If
    If chkHospital.Value = True & Appointment < "" Then
            Appointment = "Hospital "
            Else
            Appointment = Appointment & ", " & "Hospital "
    End If
    If chkOptician.Value = True & Appointment < "" Then
            Appointment = "Optician "
            Else
            Appointment = Appointment & ", " & "Optician "
    End If
    If chkOther.Value = True & Appointment < "" Then
            Appointment = txtOther.Text
            Else
            Appointment = Appointment & ", " & txtOther.Text
    End If
    As you can see i have changed it to add the ", " onto the end, however the if condition itself is wrong

  8. #8
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Select case for checkButton to build a string

    I would try like this:

    vb Code:
    1. Dim Appointment As String
    2. Appointment = vbNullString
    3. If chkDentist.Value = True Then
    4.    If Appointment <> vbNullString Then
    5.        Appointment = Appointment & ", "
    6.    End If
    7.         Appointment = Appointment & "Dentist"
    8. End If
    9. If chkDoctor.Value = True Then
    10.    If Appointment <> vbNullString Then
    11.        Appointment = Appointment & ", "
    12.    End If
    13.     Appointment = Appointment &  "Doctor"
    14. End If
    15. If chkHospital.Value = True Then
    16.    If Appointment <> vbNullString Then
    17.        Appointment = Appointment & ", "
    18.    End If
    19.    Appointment = Appointment & "Hospital "
    20. End If
    21. If chkOptician.Value = True Then
    22.    If Appointment <> vbNullString Then
    23.        Appointment = Appointment & ", "
    24.    End If
    25.         Appointment = Appointment & "Optician "
    26. End IF
    27. If chkOther.Value = True Then
    28.    If Appointment <> vbNullString Then
    29.        Appointment = Appointment & ", "
    30.    End If
    31.    Appointment = Appointment  & txtOther.Text
    32. End If
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  9. #9
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Select case for checkButton to build a string

    I think its wrong again.
    To solve this , either you have to create for all the possible combinations for the checks being true or false
    Or
    Check whether the Appointment is null or not.
    eg:

    Code:
    If chkDentist.Value = True Then
            Appointment = iif(len(Appointment) > 0 , Appointment  & ", ","") &  "Dentist "
    End If


    EDIT: I was slow
    IIF(Post.Rate > 0 , , )

  10. #10

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    11

    Re: Select case for checkButton to build a string

    i was half way through writing something similar when your post came in. Hmmmmm vbNullString, never used that before, quite useful, one for the memory box

    Thanks very much indeed

  11. #11
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Select case for checkButton to build a string

    Your welcome. If this resolves the issue for you could you please use the Thread Tools above and mark the Thread as Resolved.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  12. #12
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Select case for checkButton to build a string

    Consider building a check box control array. Add a command button and use code similar to this:
    Code:
    Dim Appoint() As String
    
    Private Sub Command1_Click()
    Dim Appointment As String
    For I = 0 To 4
        If Check1(I).Value Then Appointment = Appointment + Appoint(I) + ", "
    Next
    If Len(Appointment) Then Appointment = Left$(Appointment, Len(Appointment) - 2)
    MsgBox Appointment
    End Sub
    
    Private Sub Form_Load()
    ReDim Appoint(4)
    Appoint(0) = "Dentist"
    Appoint(1) = "Doctor"
    Appoint(2) = "Hospital"
    Appoint(3) = "Optician"
    Appoint(4) = "Other"
    End Sub
    Doctor Ed

  13. #13
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Select case for checkButton to build a string

    ooor.. if those are your only checkboxes...add the value to the tag property

    vb Code:
    1. Dim ctl as control
    2. for each ctl in me.controls
    3. if typeof(ctl) is Checkbox then
    4. 'add ctl.tag to your sql
    5. end if
    6. next

    lol
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  14. #14

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    11

    Re: Select case for checkButton to build a string

    thank you all for your quick help, garymazzone's answer does the job perfectly, i'll resolve it now, thanks again

Posting Permissions

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



Click Here to Expand Forum to Full Width