Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do you limit data being selected from a combo box? 1

Status
Not open for further replies.

Dhr003

Programmer
Nov 27, 2001
8
GB
im working on a vb project to design a juke box which plays a number of songs which is dependent on the money u enter. for example if £0.50p is enter 1 song is allowed to be selected or if £1.00 is entered 3 songs are allowed to be selected and displayed on a textbox etc. Ive started the coding but am stuck on a problem. when i enter money it should only let me select the number of songs allowed to but it doesnt it lets me keep on selecting without the limitations i have put there. ill paste my coding here( it isnt finished its done up to the point where im stuck) so ppl
plz correct me where im goign wrong.
-----------------------------------------------------------------------------------------------------------------------------------
Private Sub cmbSong_Click()

If cmbSong.Text = "1" Then
txtsongName = "Stan"
Me.txtartistname = "Eminem"
ElseIf cmbSong.Text = "2" Then
txtsongName = "Krishna"
Me.txtartistname = "Dido"
ElseIf cmbSong.Text = "3" Then
txtsongName = "Thriller"
Me.txtartistname = "Michael Jackson"
ElseIf cmbSong.Text = "4" Then
txtsongName = "Slave 2 u"
Me.txtartistname = "Britney Spears"
ElseIf cmbSong.Text = "5" Then
txtsongName = "Lavida loka"
Me.txtartistname = "Ricky Martin"
End If

End Sub


Private Sub cmdSelectSong_Click()

Dim songName As String
Dim artistName As String
Dim counter As Integer
Dim songcount As Integer
Dim i As Integer

If Text3.Text = "0:50" Then
counter = 1
ElseIf Text3.Text = "1:00" Then
counter = 3
ElseIf Text3.Text = "2:00" Then
counter = 8
End If


Select Case counter

Case 1
songName = Me.txtsongName.Text
artistName = Me.txtartistname.Text
txtsonglist.Text = songName & vbCrLf & artistName
Case 3
counter = 0
songName = Me.txtsongName.Text
artistName = Me.txtartistname.Text
txtsonglist.Text = songName & vbCrLf & artistName
Case 8
counter = 0
songName = Me.txtsongName.Text
artistName = Me.txtartistname.Text
txtsonglist.Text = songName & vbCrLf & artistName
Case Else
MsgBox "ERROR", vbCritical
'txtartistname.Text = artistName
End Select

End Sub




Private Sub Form_Load()

MsgBox "Welcome!", vbInformation, "JukeBox Selector"
' settings for combo box initialisation values
cmbSong.AddItem "1"
cmbSong.AddItem "2"
cmbSong.AddItem "3"
cmbSong.AddItem "4"
cmbSong.AddItem "5"

End Sub

Private Sub quit_Click()

End
End Sub
 
hi,

I'm not sure what you mean but
In the if ..... elsif statement of the textbox you put the sign ":" in stead of "."
Maybe this is the problem?
Should it not be 0.50 in stead of 0:50?
Also I don't understand why you set the counter to 0 when you enter the select case statement
--> case=3
---->counter = 0
In your code I see a declaration "Songcount as integer", but I don't see where else you use this

And why not use the select case statement iso the nested elsif code?
This reads far more easy and is not liable to errors as you have in nested elsif statements.

in stead of
*********************
If Text3.Text = "0:50" Then
counter = 1
ElseIf Text3.Text = "1:00" Then
counter = 3
ElseIf Text3.Text = "2:00" Then
counter = 8
End If
******************************
use
******************************

Select Case Text3.Text
Case "0.50"
counter = 1
Case 1.00
counter = 3
Case 2.00
counter = 8
Case Else
MsgBox "Not a valid entry"
End Select
******************************************

 
Let me comment on your code -

1. Its really not very good. Its very sloppy, and doesn't really do what you want it to, anyways.

2. You should name your variables and controls with consistency. I.E. - use txtArtistName if you are going to use txtSongName. It cuts down on the typos you make, and makes maintaining your code much easier in the long run. (especially in the LONG run)

Anyways, here's my idea.


'I declared these variables in the General Declarations to
'make them public scoped. Its not really the way to do it,
'but passing variables between events leaves too much room
'for error at this point.

Option Explicit
Dim songName(1 To 8) As String 'these two arrays hold the
Dim artistName(1 To 8) As String 'songs and names
Dim counter As Integer 'counter variable
Dim songcount As Integer 'I din't use this
Dim i As Integer 'another counter variable


Private Sub cmbSong_Click()

'I just added the If-Then. It may be better for you to use
'a Case-Select to put the songnames and artists in the text
'boxes

If i <= counter Then

If cmbSong.Text = &quot;1&quot; Then
txtsongName = &quot;Stan&quot;
Me.txtartistname = &quot;Eminem&quot;
ElseIf cmbSong.Text = &quot;2&quot; Then
txtsongName = &quot;Krishna&quot;
Me.txtartistname = &quot;Dido&quot;
ElseIf cmbSong.Text = &quot;3&quot; Then
txtsongName = &quot;Thriller&quot;
Me.txtartistname = &quot;Michael Jackson&quot;
ElseIf cmbSong.Text = &quot;4&quot; Then
txtsongName = &quot;Slave 4 U&quot;
Me.txtartistname = &quot;Britney Spears&quot;
ElseIf cmbSong.Text = &quot;5&quot; Then
txtsongName = &quot;La Vida Loca&quot;
Me.txtartistname = &quot;Ricky Martin&quot;
End If

songName(i) = txtsongName.Text
artistName(i) = txtartistname.Text
End If

'if i is bigger or equal to the number of songs allowed,
'then tell the user that they entered the last song

If i >= counter Then
MsgBox &quot;Last song entered&quot;
Else
i = i + 1
End If
End Sub

Private Sub cmdSelectSong_Click()

'you should make sure you include some instructions for the text box



If Text3.Text = &quot;0:50&quot; Then
counter = 1
ElseIf Text3.Text = &quot;1:00&quot; Then
counter = 3
ElseIf Text3.Text = &quot;2:00&quot; Then
counter = 8
Else
MsgBox &quot;put in your money&quot;
End If

i = 1



End Sub



Private Sub Form_Load()


MsgBox &quot;Welcome!&quot;, vbInformation, &quot;JukeBox Selector&quot;

' settings for combo box initialisation values
cmbSong.AddItem &quot;1&quot;
cmbSong.AddItem &quot;2&quot;
cmbSong.AddItem &quot;3&quot;
cmbSong.AddItem &quot;4&quot;
cmbSong.AddItem &quot;5&quot;

End Sub

Private Sub quit_Click()

End

End Sub
 
I don't know what this is all about.
The code looks very messy.
Will these be all the songs the customers can choose ??
Why not putting them in a table ??

If you are interested i will write a GOOD code the help you out.

Just let me know.

You're never to old to learn but the older i get the more i forget.
 
jasek78 thanks for the code but the program is now only letting me select only 1 song now. i did what u told me to do.

ElseIf cmbSong.Text = &quot;5&quot; Then
txtsongName = &quot;La Vida Loca&quot;
txtartistname = &quot;Ricky Martin&quot;
End If

songName(i) = txtsongName.Text = these two staements are coming out wrong can u plz help
artistName(i) = txtartistname.Text = jazek or anybody

End If
 
it will only show one artist and song at a time, but it holds all of the selected ones in the songName and artistName arrays
 
jazek78 isnt there a way for it to do more than 1 at a time. and also it doesnt hold the selected ones in songName and artistname

ElseIf cmbSong.Text = &quot;5&quot; Then
txtsongName = &quot;La Vida Loca&quot;
txtartistname = &quot;Ricky Martin&quot;
End If
songName(i) = txtsongName.Text = these two staements are coming out wrong
artistName(i) = txtartistname.Text =
End If
can u explain this plz
 
It looks to me as though your design is wrong from the start. If you charge 50p for one song, nobody is going to want to use it anyway - so it doesn't matter if it works or not!!!!
 
it does matter if it works because its my university Visual basic programming coursework .thats y im making a big deal out of it!!!can anybody plz make suggestions to improve my problem as stated above
 
songName(i) = txtsongName.Text
artistName(i) = txtartistname.Text

these two lines put the song and artist names into the arrays.
 
I've made some changes to your code, and added a feature to show the list of songs that were selected.

I need you to add a command button called &quot;cmdPrint&quot; and a listbox control called &quot;lstSongs&quot; to see the list of songs.

jase

**********************************************************
**********************************************************
'I declared these variables in the General Declarations to
'make them public scoped. Its not really the way to do it,
'but passing variables between events leaves too much room
'for error at this point.

Option Explicit
Dim songName(1 To 8) As String 'these two arrays hold the
Dim artistName(1 To 8) As String 'songs and names
Dim counter As Integer 'counter variable
Dim songcount As Integer 'I din't use this
Dim i As Integer 'another counter variable


Private Sub cmbSong_Click()
'I just added the If-Then and changed the nested if-then-elseifs
'into a case statement

Dim inSongNum As Integer 'this is for the Case



If i <= counter Then 'if i is less than or equal to
'the number of songs then
'display the song and artist
inSongNum = Val(cmbSong.Text)

Select Case inSongNum
Case 1
txtsongName.Text = &quot;Stan&quot;
txtartistname.Text = &quot;Eminem&quot;
Case 2
txtsongName.Text = &quot;Krishna&quot;
txtartistname.Text = &quot;Dido&quot;
Case 3
txtsongName.Text = &quot;Thriller&quot;
txtartistname.Text = &quot;Michael Jackson&quot;
Case 4
txtsongName.Text = &quot;Slave 4 U&quot;
txtartistname.Text = &quot;Britney Spears&quot;
Case 5
txtsongName.Text = &quot;La Vida Loca&quot;
txtartistname.Text = &quot;Ricky Martin&quot;
End Select

'these two lines put the songname and artist name into an array

songName(i) = txtsongName.Text
artistName(i) = txtartistname.Text

End If

'if i is bigger or equal to the number of songs allowed,
'then tell the user that they entered the last song

If i >= counter Then
MsgBox &quot;Last song entered&quot;
Else
i = i + 1
End If
End Sub

Private Sub cmdSelectSong_Click()

'you should make sure you include some instructions
'for the text box

'I implemented Imhoteb's Case Select statement here

Select Case Text3.Text
Case &quot;0:50&quot;
counter = 1
Case &quot;1:00&quot;
counter = 3
Case &quot;2:00&quot;
counter = 8
Case Else
MsgBox &quot;Not a valid entry&quot;
End Select
i = 1



End Sub



Private Sub Form_Load()


MsgBox &quot;Welcome!&quot;, vbInformation, &quot;JukeBox Selector&quot;

' settings for combo box initialisation values
cmbSong.AddItem &quot;1&quot;
cmbSong.AddItem &quot;2&quot;
cmbSong.AddItem &quot;3&quot;
cmbSong.AddItem &quot;4&quot;
cmbSong.AddItem &quot;5&quot;

End Sub

Private Sub quit_Click()

Unload Me 'I implemented this. Its just *better*

End Sub

'add a command button called &quot;cmdPrint&quot;
'and a listbox control called &quot;lstSongs&quot;
'when you click on the cmdPrint button,
'the list of songs will show up in the listbox


Private Sub cmdPrint_Click()
Dim t As Integer 'temp counter variable
Dim strSong As String 'temp string to hold songs

lstSongs.Clear 'clears the listbox, so no old
'records show

For t = 1 To counter Step 1

'the first line puts the artist and songname into a
'string variable.

'the second line adds the song to the listbox control

strSong = artistName(t) & &quot; - &quot; & songName(t)
lstSongs.AddItem strSong

Next t

End Sub
 
jazek thanks alot i really am grateful for your help
 
im still having errors with

'these two lines put the songname and artist name into an array

songName(i) = txtsongName.Text
artistName(i) = txtartistname.Text

End If
 
what is going wrong? I tested it on my computer before I sent it. it should work fine.

jase
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top