473,325 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

How do we search arrays

I am writing a program to search for names. Right now I have it setup for as all the names to be in a Array.

Expand|Select|Wrap|Line Numbers
  1. Dim Names(4) as String
  2. Movies(0)=Ryan
  3. Movies(1)=Philip
  4. Movies(2)=Roger
  5. Movies(3)=Charles
  6. Movies(4)=Tim
Now what I would like to do is have a program written that would search the array and tell me that the name is in the array or not. Another question that I have is would it matter if the names in the array were lower case or upper case. If so will I have to create a new array for lower case names.

What I kind of have so far is this

Expand|Select|Wrap|Line Numbers
  1. If input1=movies() Then
  2. msgbox("Congrats that name is in the array
  3. end if
  4. If input1<>movies() Then
  5. msgbox("Please try again")
  6. end if
Will this work???
Thanks for your time and any help that you may give.
Oct 24 '10 #1
4 1516
I'm not the best programer but this should work;

Expand|Select|Wrap|Line Numbers
  1. Sub TEST()
  2. Dim Names(5) As String
  3. Dim chkNames As String
  4. Dim chkarray As Boolean
  5.  
  6. chkname = "Tim"
  7.  
  8. Names(0) = "Ryan"
  9. Names(1) = "Philip"
  10. Names(2) = "Roger"
  11. Names(3) = "Charles"
  12. Names(4) = "Tim"
  13.  
  14. For I = 0 To 4
  15.     If UCase(chkname) = UCase(Names(I)) Then
  16.         chkarray = True
  17.         Exit For
  18.     Else
  19.         chkarray = False
  20.     End If
  21. Next
  22.  
  23. End Sub
  24.  
This code should take care of wether the string is lower case, uper case, or mixed as well.
Oct 25 '10 #2
Ok that code helps me out a lot. But I have one problem that I just cant figure out with it. If I search for the name Ryan(which is the first name in the array) it finds it no problem. When I seach for the second or third of fourth name it cant find it. So bascially its not searching the rest of the array.

Heres the code that I have right now

Names(0) = "Ryan"
Names(1) = "Andy"
Names(2) = "Tim"
Names(3) = "Roger"
Names(4) = "Charles"

For I = 0 To 5
If UCase(chkname) = UCase(Names(I)) Then
msgbox("Found")
Exit For

Else
MsgBox ("Please Enter A Name")
Exit For
End If
Next

Any help would be appreciated
Oct 27 '10 #3
Guido Geurs
767 Expert 512MB
1st: the array is from 0 to 4 ! NOT For I = 0 To 5
2nd: when you check the first name, and its not valid, the ELSE set msgbox and EXIT FOR: so you don't loop through the rest of the values.
Oct 27 '10 #4
Take out the "exit for" & the msgbox after the else statement like ggeu said. If you leave it there the program exits the loop before it can search the rest of the array.
Oct 27 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Bryce Maschino | last post by:
hello, i am trying to make a java program that will take several different arrays and search through them in a loop suchs as: for (i=0; i < 5; i++) { if (firstarray<such and such { blah blah
28
by: joshc | last post by:
If I have an array of data that I know to be sorted in increasing order, and the array is less than 50 elements, and I want to find the first element greater than a certain value, is a simple...
1
by: Scott Handojo | last post by:
Lets say I have the following arrays X1 (1, 2, 7, 8) X2 (1, 2, 4, 6) X3 (1, 2, 3, 5) X4 (1, 3, 5) X5 (2, 3, 5) X6 (4, 5, 6, 8) My 'search array' is comprised of
5
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir);...
38
by: ssg31415926 | last post by:
I need to compare two string arrays defined as string such that the two arrays are equal if the contents of the two are the same, where order doesn't matter and every element must be unique. ...
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
3
by: Harry Haller | last post by:
What is the fastest way to search a client-side database? I have about 60-65 kb of data downloaded to the client which is present in 3 dynamically created list boxes. The boxes are filled from 3...
1
by: Harry Haller | last post by:
What is the fastest way to search a client-side database? I have about 60-65 kb of data downloaded to the client which is present in 3 dynamically created list boxes. The boxes are filled from 3...
1
by: Jetboy555 | last post by:
Using C++ language I need to write a program that first separates the data into arrays for year, Winner, Winner's score, Runner-up, Runner-ups score. I’m having trouble putting all my data into...
11
by: cmb3587 | last post by:
I have two arrays and I'm trying to create a 3rd array that is the difference between the two arrays Ex: arrayA: 3 5 8 9 arrayB: 3 4 6 9 difference of A-B: 5 8 however, my...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.