Pulling street based on city provided from a list?
I don't know what you would call it but I want to do something like this:
I have a list that contains the following and will have more in the future.
{buffalo}
street
street2
street3
{end}
{albany}
street1
street2
street3
street4
{end}
Now I want to create a function where I can pull a random street from the city I call, for example:
msgbox PullStreet(albany)
and the above might return "street3"
Re: Pulling street based on city provided from a list?
Code:
Private Type CitiesAndStreets
City As String
street As String
End Type
Private CAS() As CitiesAndStreets
Private Sub Form_Load()
Dim intIndex As Integer
Randomize
ReDim CAS(0)
' You could do this in a loop while reading a file containing the data
CAS(UBound(CAS)).City = "buffalo"
CAS(UBound(CAS)).street = "street"
ReDim Preserve CAS(UBound(CAS) + 1)
CAS(UBound(CAS)).City = "buffalo"
CAS(UBound(CAS)).street = "street1"
ReDim Preserve CAS(UBound(CAS) + 1)
CAS(UBound(CAS)).City = "buffalo"
CAS(UBound(CAS)).street = "street2"
ReDim Preserve CAS(UBound(CAS) + 1)
CAS(UBound(CAS)).City = "Albany"
CAS(UBound(CAS)).street = "street1"
ReDim Preserve CAS(UBound(CAS) + 1)
CAS(UBound(CAS)).City = "Albany"
CAS(UBound(CAS)).street = "street2"
ReDim Preserve CAS(UBound(CAS) + 1)
CAS(UBound(CAS)).City = "Albany"
CAS(UBound(CAS)).street = "street3"
ReDim Preserve CAS(UBound(CAS) + 1)
CAS(UBound(CAS)).City = "Albany"
CAS(UBound(CAS)).street = "street3"
ReDim Preserve CAS(UBound(CAS) + 1)
Do
intIndex = Int((UBound(CAS) + 1) * Rnd)
If CAS(intIndex).City = "Albany" Then
MsgBox CAS(intIndex).street
Exit Do
End If
Loop
Re: Pulling street based on city provided from a list?
thud
Seems that several steps could do the trick..
First, use a loop to store the relevant streets in an array
1. Loop thru the list to find the city.
2. Continue the loop, adding items to an array (this will be streets)
3. When next city (or end) is encountered, exit the loop
Second, randomly pick a street from that array.
Does that get you started?
EDIT: Dang, Marty was quicker to the punch than I was :wave:
EDIT-2: Not to mention -- somewhat more specific
Spoo
Re: Pulling street based on city provided from a list?
Can I call shenanigans? Are you doing this in VB6 (this thread) or in .NET (as in this thread? http://www.vbforums.com/showthread.php?t=568696 ) ???
-tg