Sorry.......

willstephens1
11-12-2003, 01:08 PM
I know it must be really annoying for you guys when someone like me comes and asks the most stupid questions, but im really struggling with my college project and at present i have no reference to help me ( i do have same VB on its way).

Im trying to search for text in a listbox. I have the following code.

Private Sub CmdSearch_Click()
For i = 1 To 15
If TxtTitle.Text Like title(i) Then
lbltitle.Caption = title(i)
End If
Next i
End Sub

So when the user clicks the cmdsearch button, the txttitle textbox data is compared to an array of string i have called title(15) and whe it gets the right match it will paste the name of the relevant title(15) into the lbltitle label.

Unfortunatly, when i run the program, as soon as i click on the cmdsearch i get the message "sub or function not defined" and the "title(i)" after the "like" is highlighted.

I have never used the "like" command before but i presumeit is there if you need to compare to variables.

Im not sure if ive explained myself fully here but if anyone has any ideas i would appreciate it.

Thanks

Will

wilbert
11-12-2003, 01:24 PM
Just replace 'Like' with '='
If txttitle.text = title(i) then

Wilbert

Cert
11-12-2003, 01:25 PM
Has Title() been initilized/declared & given data?

willstephens1
11-12-2003, 01:26 PM
Nope, didnt work, i did try that first before i tried "like". It just comes up with the same error message.

willstephens1
11-12-2003, 01:27 PM
Yes, title was initialised

Private Sub Form_Load()

Dim title(15) As String
Dim genre(15) As String
Dim year(15) As Integer
Dim rating(15) As String
Dim length(15) As Integer
Dim description(15) As String


That is the first few lines of the form_load routine.

I did think that maybe it had something to do with title being a local variable and not a global one but we havent covered that at college yet so wasnt sure.

wilbert
11-12-2003, 01:35 PM
Yes that's true, declare those arrays in the general section of your form.

IGBP
11-12-2003, 01:38 PM
Will,
All those variables are going to go out of focus as soon as the form_load event is done, and therefore will not be available anywhere else. I believe in MSDN they call it Scope.
remove the variables from that event and put them in the General Declarations section of the form code window, right under the option explicit statement. Then try again and let us know

willstephens1
11-12-2003, 01:39 PM
hmm, did that and noqw i get the message complie error: member already exists a object module from wehich the object module derives.

IGBP
11-12-2003, 01:57 PM
Where/when is this error happening? what line is being highlighted?

willstephens1
11-12-2003, 02:00 PM
This is the full code of my project (not a lot)


Dim title(15) As String
Dim genre(15) As String
Dim year(15) As Integer
Dim rating(15) As String
Dim length(15) As Integer
Dim description(15) As String



Private Sub CmdSearch_Click()

For i = 1 To 15
If TxtTitle.Text = title(i) Then
lbltitle.Caption = title(i)
End If
Next i

End Sub

Private Sub Form_Load()

title(1) = "The Matrix"
title(2) = "Final Destination"
title(3) = "Signs"
title(4) = "Scary Movie"
title(5) = "Shallow Hal"
title(6) = "The Others"
title(7) = "Sixth sense"
title(8) = "Terminator 3"
title(9) = "Next Friday"
title(10) = "Liar Liar"
genre(1) = "Sci Fi"
genre(2) = "Horror"
genre(3) = "Horror"
genre(4) = "Comedy"
genre(5) = "Comedy"
genre(6) = "Horror"
genre(7) = "Horror"
genre(8) = "Action"
genre(9) = "Comedy"
genre(10) = "Comedy"
year(1) = 1999
year(2) = 2000
year(3) = 2002
year(4) = 2000
year(5) = 2001
year(6) = 2001
year(7) = 1999
year(8) = 2003
year(9) = 2000
year(10) = 1997
rating(1) = "R"
rating(2) = "R"
rating(3) = "PG-13"
rating(4) = "R"
rating(5) = "PG-13"
rating(6) = "PG-13"
rating(7) = "PG-13"
rating(8) = "R"
rating(9) = "R"
rating(10) = "PG-13"
length(1) = 136
length(2) = 98
length(3) = 106
length(4) = 88
length(5) = 113
length(6) = 101
length(7) = 107
length(8) = 109
length(9) = 98
length(10) = 86
description(1) = "A computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war against the controllers of it."
description(2) = "After having a vision of his friends crashing in a plane, he tells them not to get on only later his friends start getting killed one by one."
description(3) = "A family living on a farm finds mysterious crop circles in their fields which suggests something more frightening to come."
description(4) = "A year after disposing the body of a man they accidently killed, a group of dumb teenagers are stalked by a bumbling serial killer."
description(5) = "A shallow man falls in love with a 300 pound woman because of her inner beauty."
description(6) = "A woman who lives in a darkened old house with her two photosensitive children becomes convinced that her family home is haunted."
description(7) = "A boy who communicates with spirits that don't know they're dead seeks the help of a disheartened child psychologist."
description(8) = "John Connor is now in his 20's, and a female terminator, called T-X or Terminatrix, is after him. "
description(9) = "Craig Jones moves from the ghetto to the suburbs to live with his lottery-winning uncle and cousins."
description(10) = "Jim Carrey is back in yet another great comedy, although it doesn't quite live up to the spectacular Dumb & Dumber"

Dim i As Integer
For i = 1 To 15
List1.AddItem (title(i))
Next i
For i = 1 To 15
List2.AddItem (genre(i))
Next i
For i = 1 To 15
List3.AddItem (year(i))
Next i
For i = 1 To 15
List4.AddItem (rating(i))
Next i
For i = 1 To 15
List5.AddItem (length(i))
Next i
For i = 1 To 15
List6.AddItem (description(i))
Next i
End Sub

The error message appears when i try running and it highlights the "genre(15) as string" part of the code right near the top. For some reason it doesnt give me the error message on the title(15) even though they are both declaring variables. The top bit of the code is under (general) (declarations)

goblinhybrid
11-12-2003, 02:06 PM
for i = 0 to 14
if searchlist.list(i) = searchtext then
''add the code here''
end if
next i

i can't remember if listboxes start at 0 or 1 anyway...
change searchtext to the text you want to find and searchlist to the listbox and it should work

willstephens1
11-12-2003, 02:10 PM
Nope, still no joy, got the same message i had before. I tried moving the variables back to the form_load section and i still got the old error message.

goblinhybrid
11-12-2003, 02:14 PM
are u trying to search for text in a listbox or textbox? Cause if u searching a listbox then TxtTitle.Text will only return the text of the highlighted part of the listbox

willstephens1
11-12-2003, 02:17 PM
txttile.text is the textbox that the user can type the title in that they are searching for and i want the details from txttitle.text to be compared to the title(15) array which is show in the listbox.

IGBP
11-12-2003, 02:17 PM
Will ....
Save the project as it is ..... Zip it(all forms,modules.... everything) and upload it. For some reason it is creating that object twice. I would like to look at it, but since this is homework, we kind of limit ourselves to helping with homework and not doing it for someone.

willstephens1
11-12-2003, 02:21 PM
Sure, where can i uplaod it to. I can mail it you if thats easier, its only a few k. I understand the thing about it being homework but at the end of the day if someone did it for me i wouldnt learn anything and that wouldnt do me much good.lol

willstephens1
11-12-2003, 02:22 PM
Sorry, just seen the attachments bit at the bottom.

goblinhybrid
11-12-2003, 02:25 PM
there isn't a problem with ur code there although, personally, i think it would be easier to search listbox instead of an array

willstephens1
11-12-2003, 02:30 PM
Well i didnt know you could do that.lol But....... Ive done the array becuase title(3) is relevent to genre(3) and length(3) etc, so all the arrays are linked around the same movie, if you get what i mean.

But im a pure beginner, only been doing VB for 7 weeks so maybe i could use the dearch listbox function, i have no idea. This guy at college did his project using a listview and his works really well, but he did VB last year and he knows what he's doing.lol

IGBP
11-12-2003, 02:46 PM
Will ...
No Form ... the project wont load. try again

blindwig
11-12-2003, 02:46 PM
I haven't tried running your code, but I think that Title may be a reserved keyword. Try changing it to MovieTitle or something like that and see if that works.

willstephens1
11-12-2003, 02:48 PM
Sorry, my mistake.

willstephens1
11-12-2003, 02:53 PM
Holy balony. it worked. Well it nearly worked, its case sensative but it worked. Thanks for that, i presume title is a reserved word then.lol

Well thanks for all your help guys and no doubt you'll be hearing from me pretty soon (when i get stuck again.lol) unless my sams VB book turns up soon.lol

Thanks again everyone.

Will

vbFace
11-12-2003, 03:29 PM
Try this, it will make your project a lot easier! It is called a user defined type, and you create just one array of it:


Public Type MyMovies
Title As String
Genre As String
Year As Integer
Rating As String
Length As Integer
Description As String
End Type

Public Movies(1 to 15) as MyMovies

' To get/set data
Movies(1).Title = "Rambo"
Text7.Text = Movies(13).Description
' etc

willstephens1
11-12-2003, 03:32 PM
Thanks for that, i'll keep that in mind. At present, my project seems to be working pretty good so i'll not put it in jsut yet cause i dont want to break it.lol But you know that feeling you get when you soend ages trying to get something to work and then it works, well i got that feeling, its great. I suppose thats what programming is all about. Feel like cracking open a beer.lol

vbFace
11-12-2003, 03:35 PM
Thanks for that, i'll keep that in mind. At present, my project seems to be working pretty good so i'll not put it in jsut yet cause i dont want to break it.lol But you know that feeling you get when you soend ages trying to get something to work and then it works, well i got that feeling, its great. I suppose thats what programming is all about. Feel like cracking open a beer.lol


LOL...and the hardest part of programming is going back to working code and making it better...because you just know that someone is going to want you to do more with it!!!

IGBP
11-12-2003, 04:23 PM
Will ...
OK .. got it figured out ... several small things to change, but it is easy.
First ..
Move all the Array(s) out of the form load event. they really do not belong there. Put them in the General declarations section. Then change the name of each one, using Hungarian notation .... str for Strings, int For Integers ... etc. So one could be Dim sGenre(15) As String ..etc.
That will clear up all ambiguos declarations.
Then in your code, change the references to the old array names to the new array names.
Just FYI ... arrays are zero based ... and in your code you begin assigning to index 1, leaving 0 empty. To make it easiest on me playing I just added a line like this:
sMovieTitle(0) = ""
sMovieTitle(1) = "The Matrix"
before each instance where you are refering to an array in your form load. make sure you give them the right data types ... "" for strings,
0 for Integers ...
At the end of your form load where you have these...
Dim i As Integer
For i = 1 To 15
List1.AddItem (sMovieTitle(i))....
They should be like this ...
Dim i As Integer
For i = 1 To 15
List1.AddItem sMovieTitle(i) ... remove the () where not needed

Finally ... in the command button click event ...
First dim something as a string
assign that string = txtTitle
Then look in MSDN for the StrConv function. there is one in there that will convert that input to Proper Case.
Use that StrConv to change the string variable ...
Change your For Loop in there from 0 to 1 ... 0 is empty .. you did not assign anything to it.
Consider this in that For Loop
If sTempString = sMovieTitle(i) Then
lbltitle.Caption = sMovieTitle(i)
Exit For 'If it matches ... bail out

End If
And Finally ...
After the Next i statement ,,, consider something like this

If lbltitle = "" Then
MsgBox "Search string not found", vbInformation + vbOKOnly, "Not found" 'Tell the User it was not found
End If
TxtTitle = "" 'clear the box
TxtTitle.SetFocus ' be ready for next input
HTH
Michael

MikeJ
11-12-2003, 05:01 PM
One way you can fix the case sensativity is to use the UCase$ function like so and add it whenever you are doing some checking:

If UCase$(TxtTitle.Text) = UCase$(MovieTitle(i)) Then
'Notice the placement for the UCase statements

The UCase makes the string convert to uppercase, casuing the way to check not worry about CaSe SeNsItIvItY. So whatever is selected will always appear to be CASE SENSITIVITY if you decide to do a Debug.Print on the variable.

:cool:
~Mike

P.S.
LCase$ works just as well...

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum