Constructing a zip code program 
Author Message
 Constructing a zip code program

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



Mon, 26 May 2003 03:00:00 GMT  
 Constructing a zip code program
Your best bet is to write the data into a database (access,fox,etc)
from there you'll be able to use SQL or Seek to find the city & state.
If your text file had ALL the zipcode in the US, it will take far to
long to do a linear search or even to place them all in an array.

Ray

Quote:
-----Original Message-----

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

.



Mon, 26 May 2003 03:00:00 GMT  
 Constructing a zip code program
A simple loop should find the results you need.  I wrote this test code
and used it against a 1.1MB file.  They file I had was repeating so I
just incremented a counter on the number of hits.  This would make my
code slower than what you will have because once you get a single hit
you will be done.  This code ran (with the slower code ie. all my I/O)
in about 3 or 4 seconds tops.

You need to take their input (city and state) and combine it into a
string that matches the file you have. ie. add a comma, or pad it with
spaces whatever.  That variable in my example is called Var. (I get mine
from a simple textbox)
My counter is dimmed globally thus you don't see it here.

Private Sub Command1_Click()
Dim A As String
Label1 = "Looking for " & Var
If Var <> "" Then
   Open "c:\file3.txt" For Input As #1  'open file to search
   Do Until EOF(1)
     'Debug.Print "Checking a line"
     Input #1, A
     If InStr(A, Var) > 0 Then    ' if the line has your search string
       'Debug.Print "Match"
       counter = counter + 1    'add your custom code here
       Label1 = counter
       DoEvents
     End If
   Loop
Else
   MsgBox "Need a search string silly"
End If
MsgBox "Done"

End Sub

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



Tue, 27 May 2003 22:17:03 GMT  
 Constructing a zip code program
Don't load the file into memory, process it line by line. Here's the
complete code that should do exactly what you need:

' requires two Text boxes named City and State and a ListBox named ZIPList
Private Sub Command1_Click()
    ZIPList.Clear
    FindZIP City.Text, State.Text
End Sub

' adds ZIP code passed into a ListBox named ZIPList, but since it passes all
the info
' from the record found, you can do anything else here, like fill a ListView
with
' multiple columns etc.
Private Sub ProcessZIP(ZIP As String, City As String, County As String,
State As String)
    ZIPList.AddItem ZIP
End Sub

Private Sub FindZIP(City As String, State As String)
    Dim Buffer As String
    Dim Fields() As String

    Open "Zipdata.txt" For Input As #1
    Do While Not EOF(1)
        ' read next record from file
        Line Input #1, Buffer
        ' split record into fields, using comma as delimiter
        Fields = Split(Buffer, ",", -1, vbTextCompare)
        ' record has at least 4 fields, so subsequent code won't fail
        If UBound(Fields) >= 3 Then
            ' record matches both City and State, case insensitive
            If (StrComp(City, Fields(1), vbTextCompare) = 0) And
(StrComp(State, Fields(3), vbTextCompare) = 0) Then
                ' make sure you pass the array items in the correct
parameter order
                ProcessZIP Fields(0), Fields(1), Fields(2), Fields(3)
            End If
        End If
        Erase Fields
    Loop
    Close #1
End Sub

--
Uwe W. Radu
(leave capital letters only in host name to email me)



Wed, 28 May 2003 04:58:29 GMT  
 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



Thu, 14 Aug 2003 06:32:12 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. abcrd001.zip/abc0995.zip - ALL BASIC CODE Packets/Reader/Extractor

2. Need ZIP source code or freeware ZIP DLL

3. Accessing char ** C-programming Construct via DLL in VB5

4. Distribution of VB-constructed programs

5. Distribution of VB-constructed programs

6. Accessing char ** C-programming Construct via DLL in VB5

7. Accessing char ** C-programming Construct via DLL in VB5

8. ANN: Zip Code/Area Code/Time Zone Data and Components

9. Postal codes and zip codes

10. international post codes (Zip codes) - ricerca tabella codici postali europei per database

11. 3d-demo program in 320x200 256 colour mode - 3d.zip [1/1]

12. Please test my program - MONITOR.ZIP attached

 

 
Powered by phpBB® Forum Software