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.

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.

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

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.