|
-
Nov 5th, 2007, 10:45 AM
#1
Thread Starter
New Member
[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
-
Nov 5th, 2007, 10:47 AM
#2
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
-
Nov 5th, 2007, 10:49 AM
#3
Thread Starter
New Member
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
-
Nov 5th, 2007, 10:50 AM
#4
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
-
Nov 5th, 2007, 10:52 AM
#5
Thread Starter
New Member
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
-
Nov 5th, 2007, 11:14 AM
#6
Frenzied Member
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?
-
Nov 5th, 2007, 11:18 AM
#7
Thread Starter
New Member
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
-
Nov 5th, 2007, 11:24 AM
#8
Re: Select case for checkButton to build a string
I would try like this:
vb Code:
Dim Appointment As String
Appointment = vbNullString
If chkDentist.Value = True Then
If Appointment <> vbNullString Then
Appointment = Appointment & ", "
End If
Appointment = Appointment & "Dentist"
End If
If chkDoctor.Value = True Then
If Appointment <> vbNullString Then
Appointment = Appointment & ", "
End If
Appointment = Appointment & "Doctor"
End If
If chkHospital.Value = True Then
If Appointment <> vbNullString Then
Appointment = Appointment & ", "
End If
Appointment = Appointment & "Hospital "
End If
If chkOptician.Value = True Then
If Appointment <> vbNullString Then
Appointment = Appointment & ", "
End If
Appointment = Appointment & "Optician "
End IF
If chkOther.Value = True Then
If Appointment <> vbNullString Then
Appointment = Appointment & ", "
End If
Appointment = Appointment & txtOther.Text
End If
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Nov 5th, 2007, 11:27 AM
#9
Frenzied Member
Re: Select case for checkButton to build a string
-
Nov 5th, 2007, 11:30 AM
#10
Thread Starter
New Member
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
-
Nov 5th, 2007, 11:46 AM
#11
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
-
Nov 5th, 2007, 03:01 PM
#12
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
-
Nov 5th, 2007, 03:10 PM
#13
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:
Dim ctl as control
for each ctl in me.controls
if typeof(ctl) is Checkbox then
'add ctl.tag to your sql
end if
next
lol
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Nov 6th, 2007, 04:20 AM
#14
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|