Results 1 to 9 of 9

Thread: [RESOLVED] Please HELP I have aged in a week trying to figure this out have no slept in 36 hours

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    6

    Resolved [RESOLVED] Please HELP I have aged in a week trying to figure this out have no slept in 36 hours

    I have a data file and i would like to search it for the matching zip code entered onto my form and auto fill in the city and state form the same line as the zip code. I can match the zip code but I cannot match the entire line. My I have Zip, City, State, County in my array. if I type 10005 into my form I get new York NY; but if I type 38116 into my form I get new York NY when the city and state should be Memphis, TN. I have tried several things so my code is really messy. can anyone please help!

    Imports System.IO

    Public Class Personal_Information

    Dim Zips(38854) As String
    Dim currentline As Integer
    Dim Ziprecord(4) As String



    Private Sub txtZip_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtZip.TextChanged

    'Dim sr As StreamReader
    'Dim line As String
    'Dim zip As Integer
    'Dim ZipRecord(5) As String

    'zip = txtZip.Text
    'sr = IO.File.OpenText(My.Resources.zips1)
    'line = sr.ReadLine()

    'ZipRecord = line.Split(",")

    'txtState.Text = ZipRecord(3)
    'txtCity.Text = ZipRecord(2)

    End Sub

    'Public Sub DisplayZip(ByVal thiszip As String)

    ' Dim ZipRecord(4) As String

    ' ZipRecord = thiszip.Split(",")

    ' txtState.Text = ZipRecord(2)
    ' txtCity.Text = ZipRecord(1)
    ' txtZip.Text = ZipRecord(0)

    'End Sub
    Private Sub txtZip_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtZip.Validated

    Dim sr As StreamReader
    Dim line As String
    Dim zip As String

    'Dim thiszip As String

    'currentline += 1

    'thiszip = Zips(currentline)

    'DisplayZip(thiszip)

    'Dim zipIndex As String
    'Dim index As String


    sr = IO.File.OpenText("zip1.csv")
    line = sr.ReadLine
    zip = txtZip.Text
    Ziprecord = Zips


    'DisplayZip(Zips(0))



    Dim city As String
    Dim state As String
    Dim currentline As Integer

    'Do While currentline < recCnt









    'If line.Contains("Cedar Falls") Then
    ' txtCity.Text = Ziprecord(1)
    'End If
    'MessageBox.Show(line & vbNewLine & Ziprecord(1) & line.Count)

    Ziprecord = line.Split(",")
    'Ziprecord(2) += 1
    'Ziprecord(1) += 1
    'Ziprecord(0) = zip
    Do Until line.Contains(zip)
    line = sr.ReadLine()
    zip = Zips(currentline)
    Ziprecord = zip.Split(",")

    city += Ziprecord(1)
    state += Ziprecord(2)

    currentline += 1
    Loop


    'txtCity.Text = Ziprecord(1)
    'txtState.Text = Ziprecord(2)
    'Ziprecord(0) += 1
    'MessageBox.Show(line & vbNewLine & Ziprecord(1) & vbNewLine & zip)

    'For Each line In Zips
    'If line.Contains(zip) Then

    'End If
    'Next

    'If Ziprecord(0) = zip Then
    ' txtState.Text = Ziprecord(2)
    ' txtCity.Text = Ziprecord(1)
    'End If
    'Loop


    'If Ziprecord(0) = zip Then
    ' txtState.Text = Ziprecord(2)
    ' txtCity.Text = Ziprecord(1)
    'End If

    'Do Until line = txtZip.Text
    ' line = sr.ReadLine()
    'Loop



    ' Do While sr.Peek <> -1
    'line = sr.ReadLine
    'If ZipRecord(0) = zip Then
    'End If
    ' Loop
    'zipIndex = ZipRecord(0)
    'index = zipIndex.IndexOf(zip)
    'MessageBox.Show("hello")

    '
    ' txtZip.Text = line
    'Loop




    sr.Close()
    End Sub

    End Class

  2. #2
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Please HELP I have aged in a week trying to figure this out have no slept in 36 h

    Hi,

    From what I can see of your code you are definitely making things more difficult than they need to be.

    The first thing to do is read your Zip file into memory so that you save time by NOT reading your file all the time. The next thing to do is then search that loaded object for the Line of Data that starts with the actual Zip code that you are typing. Only then do you split that Data Line and display it in your other TextBox's.

    In this example I have sourced a US Zip Code Database from the net if the form of:-

    501,Holtsville,NY
    544,Holtsville,NY
    601,Adjuntas,PR
    etc
    etc...
    Using the above structure, have a go with this:-

    Code:
    Public Class Form1
     
      Private myZipCodes() As String = IO.File.ReadAllLines(Application.StartupPath & "\USZipCodeDatabase.txt")
     
      Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
        Dim myZipRecord As String = myZipCodes.Where(Function(x) x.StartsWith(TextBox1.Text & ",")).FirstOrDefault
     
        If Not myZipRecord = String.Empty Then
          Dim myZipDetails() As String = myZipRecord.Split(","c)
          TextBox2.Text = myZipDetails(1)
          TextBox3.Text = myZipDetails(2)
        Else
          TextBox2.Clear()
          TextBox3.Clear()
        End If
      End Sub
    End Class
    Hope that helps.

    Cheers,

    Ian

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    6

    Re: Please HELP I have aged in a week trying to figure this out have no slept in 36 h

    Are you suggesting that I create a function to read in my entire file before doing the other? I see the nested function in your example.

  4. #4
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Please HELP I have aged in a week trying to figure this out have no slept in 36 h

    Hi,

    Are you suggesting that I create a function to read in my entire file before doing the other?
    No, what I have demonstrated here is creating a Private variable at the Class level that is populated when the Form loads. Once loaded, this collection of in-memory strings can then be used within the Events of your project as demonstrated.

    I see the nested function in your example.
    Sorry, but there is no nested function in my example. I have simply demonstrated how to code the TextChanged Event of a TextBox.
    (Edit: Ah, you mean the Lambda Expression? That is being used to search the myZipCodes array to find what you are typing, and if found, return the results of the search to your other TextBox's)

    Hope that clears things up for you.

    Cheers,

    Ian
    Last edited by IanRyder; May 5th, 2013 at 08:05 AM. Reason: Added Lambda Comment

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    6

    Re: Please HELP I have aged in a week trying to figure this out have no slept in 36 h

    I placed in your suggestion and got this message...An error occurred creating the form. See Exception.InnerException for details. The error is: Could not find file 'C:\Users\Virginia Ann\documents\visual studio 2010\Projects\InsuranceQuote\InsuranceQuote\bin\Debugzip1.csv'.

    this is only one form of six but once i get it done the others should come easy...I have been trying for over a week.

    I am in visual basic 10 for beginners and this is my final project. my only problem is getting the file read in correctly when before i began to do all of the extra stuff in the first post all i had was

    Imports System.IO

    Public Class Personal_Information


    Private Sub txtZipCode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtZipCode.TextChanged

    End Sub

    Private Sub txtZipCode_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtZipCode.Validated

    dim sr as StreamReader
    dim line as string
    dim zip as string
    dim Ziprecord as string

    sr = IO.File.OpenText("zip1.csv")
    line = sr.ReadLine
    zip = txtZip.Text
    Ziprecord = Zips

    Do Until line = txtZip.Text
    line = sr.ReadLine()
    Loop

    'But this gave me an infinite loop so i did

    do until line.contains(zip)
    line = sr.ReadLine
    loop

    if line.contains(zip) then
    txt.city.text = ZipRecord(1)
    txtstate.text = ZipRecord(2)


    I am not a computer science major I am a math major and the problems I am having with this program is depressing and making me very ill. I just want to pass this class!

  6. #6
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Please HELP I have aged in a week trying to figure this out have no slept in 36 h

    Hi,

    I am not sure what else to suggest since you seem to have ignored what I suggested. Also, if you are getting an error that states that a file could not be found then you need to check that you have supplied the correct Path and File Name to the file you are trying to read.

    Cheers,

    Ian

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    6

    Re: Please HELP I have aged in a week trying to figure this out have no slept in 36 h

    I really appreciate the help you have provided I just don't understand why my file cannot be found when I use your suggestion but it is found when I use the codes I had. I don't mind reading it line by line I just could not get the my record values to change.

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    6

    Re: Please HELP I have aged in a week trying to figure this out have no slept in 36 h

    IF I could post a screen shot I would post it and prove it to you that I have cut and placed my file into the debug folder in my project file.

  9. #9

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    6

    Re: Please HELP I have aged in a week trying to figure this out have no slept in 36 h

    Don't laugh too hard but I left the \ out of the code which is why the file could not be found. LOL I am going to sleep now wow. Thank You Thank You.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width