
Constructing a zip code program
How about sorting the entire file, first by state, then by city, and
finally by zipcode? Once they were all put in order, in the file,
you could create another file that stores the offsets of each
state, and then each city that starts a new letter. You would use
that index to get the file offset to start looking for any state and
city.
Your index might look something like:
Alabama - A : 1
Alabama - B : 201
...
Alabama - Y : 5121
Alabama - Z : 0 'If there are no cities by that letter, use 0 and bail
Alaska - A : 5201
Alaska - B : 6401
Alaska - C : 6601
... etc.
You really don't need the state name in that file, just the values,
because you know that the first 26 values are Alabama A-Z and the
next 26 for the next state and so on, so it can just be a list of values.
You would then, in the click event of that button, go look up the
value that is associated with the user entered state and city (first letter).
With that file offset value, you can open the file and Seek right to the
start of that section where you begin to read lines and test for a match.
If at any time you find a city entry that compares greater than the one
you're looking for, you know you have passed up where it should have
been and can quit the search. So at most, to find the zipcode, you
have to look through all the cities in a specific state, begining with a
specific letter. Thats much better than looking for a match through the
whole mess!
That index array should be small enough to hold in memory, it is just
a list of values, remember, 26 values for each state, not a very big
array at all...
perhaps this will help:
Option Explicit
Dim Index(0 To 2) As Long
Const File = "c:\temp\test"
Private Sub Command1_Click() 'Add 3 buttons
ReadLine Index(0)
End Sub
Private Sub Command2_Click()
ReadLine Index(1)
End Sub
Private Sub Command3_Click()
ReadLine Index(2)
End Sub
Private Sub Form_Load()
Const Fn = 1
Label1.Caption = "Press any button"
Open File For Output As Fn
Index(0) = Seek(Fn)
Print #Fn, "CommAnd1"
Index(1) = Seek(Fn)
Print #Fn, "COMMand2"
Index(2) = Seek(Fn)
Print #Fn, "commAND3"
Close
Debug.Print Index(0); vbCrLf; Index(1); vbCrLf; Index(2)
End Sub
Private Sub ReadLine(ByVal Offset As Long)
Const Fn = 1
Dim ln As String
Open File For Input As Fn
Seek Fn, Offset
Line Input #Fn, ln
Close Fn
Label1.Caption = ln
End Sub
Read help's Seek function and Seek statement for a better understanding of
that command...
--
President - ChildsPlay Software Services
Quote:
> I am having trouble with a program I am creating. I have a .txt file
> that has just about all the zipcodes in the united states. The format of
> the txt file is :
> Zip Code, City, County, State
> Example : 12533, Hopewell Junction , Dutchess , NY
> and so on.
> What I need huge help with is constructing a program that has the user
> input a city ans state, and then with a click of a command button,
> prints the zipcode(s) in a picture box.
> I have some ideas about how this should work but I have just spent too
> much time by myself trying to figure this out. I know I have to use some
> sort of a two-dimensional array (I'm knew to arrays and vb, so its a
> little difficult for me.) Since the file is about 1.09MB, I figure the
> file should be sorted in a way that makes the program run more
> efficiently.
> So the overall object is to have the user type in the city and state in
> 2 seperate text boxes, and then click the command button, which will
> open the .txt file for input, and then search through the file for the
> zip code or zip codes of that city and state.
> Any code would be greatly appreciated. Thanks