Solved

Phone Book In Visual Basic

Posted on 2007-11-29
6
3,237 Views
Last Modified: 2016-08-29
I am trying to create a phone book program in Visual Basic. I have so many phone numbers, I thought this would be a neat way to handle the overflow lol. I want it to read the information from a text file with the following data:  First name, last name, home phone, cell phone, and e-mail address (with each item of data on its own line, so that each person listed will have five lines with no blank lines between individuals)  If any data is missing or does not exist its corresponding line is to be blank.

I want it to be where the data can be searched in many different ways.  An individual may be called up by sequence number, and the user is also to be able to browse the names with First, Last, Next, and Previous buttons.  And I'm guessing a find button to find the next occurrence which matches the text entered in the search text box.

So far I have whats below, but I don't really know if I am on the right track for what I want... Can someone please help me :)

Thanks:)

Imports System.IO
Imports str = Microsoft.VisualBasic
Public Class Form1
    Dim InpFileName As String, FileOpenFlag As Integer
    Dim FirstName(25), LastName(25), HomePhone(25), CellPhone(25), Email(25) As String
    Dim Count, Index As Integer
    Dim DataFile As StreamReader
 
    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        ' Get name of file to read and open file
        Dim dgrResult As DialogResult
        Dim dlgOpen As New OpenFileDialog
        If FileOpenFlag > 0 Then DataFile.Close()
        ' Set properties and open file dialog
        dlgOpen.InitialDirectory = "F:"
        dlgOpen.Filter = "Text Files|*.txt|Word Files|*.doc|All Files|*.*"
        dgrResult = dlgOpen.ShowDialog()
 
        ' If file name OK then open file for reading
        If dgrResult = DialogResult.OK Then
            InpFileName = dlgOpen.FileName      ' Get filename
            DataFile = File.OpenText(InpFileName)
            FileOpenFlag = 1
            Count = 0
            ReadData()
            DataFile.Close()
        Else
            TxtFirstName.Text = "File not specified"
        End If
    End Sub
 
    Private Sub ReadData()
        ' Read all the items in the file
        Dim Temp As String
        If FileOpenFlag > 0 Then
            Do While DataFile.Peek <> -1
                Temp = DataFile.ReadLine
                If Temp.Trim <> "" Then
                    Count = Count + 1
                    FirstName(Count) = Temp.Trim
                    LastName(Count) = Temp.Trim
                    HomePhone(Count) = DataFile.ReadLine
                    CellPhone(Count) = DataFile.ReadLine
                    Temp = DataFile.ReadLine
                    Email(Count) = DataFile.ReadLine
                    Temp = DataFile.ReadLine
                Else
                    Exit Do
                End If
            Loop
            If Count > 0 Then
                Index = 1
                DisplayData()
            End If
        End If
    End Sub
 
    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        If IsNumeric(txtItemNumber.Text) Then
            Index = CInt(txtItemNumber.Text)
            If Index > 0 And Index <= Count Then
                DisplayData()
            Else
                Index = 0
                DisplayData()
                TxtFirstName.Text = "Invalid number"
            End If
        End If
    End Sub
    Private Sub DisplayData()
        txtItemNumber.Text = CStr(Index)
        TxtFirstName.Text = FirstName(Index)
        txtlastname.Text = LastName(Index)
        txtphone.Text = HomePhone(Index)
        txtphone2.Text = CellPhone(Index)
        txtemail.Text = Email(Index)
    End Sub
 
    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If Index < Count Then
            Index = Index + 1
            DisplayData()
        End If
    End Sub
 
    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        If Index > 1 Then
            Index = Index - 1
            DisplayData()
        End If
    End Sub
 
    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
        Dim FindString, CurrentString As String
        Dim FindLength As Integer, N As Integer
        FindString = txtFind.Text
        FindString = FindString.Trim
        FindString = FindString.ToUpper
        FindLength = FindString.Length
        If Index < Count Then
            For N = Index + 1 To Count
                CurrentString = FirstName(N).Trim
                CurrentString = CurrentString.ToUpper
                CurrentString = str.Left(CurrentString, FindLength)
                If FindString = CurrentString Then
                    Index = N
                    DisplayData()
                    Exit For
                Else
                    If N = Count Then
                        MessageBox.Show("End of list; search text not found")
                    End If
                End If
            Next
        Else
            MessageBox.Show("End of list; search text not found")
        End If
    End Sub
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If FileOpenFlag > 0 Then DataFile.Close()
    End Sub
End Class

Open in new window

0
Comment
Question by:ashblynn02
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 20381025
Are you storing these in Text files? Something like this is really begging for a database to house these items. A properly designed database would allow you to store the Person with an unlimited number of Contact items. I'd suggest something like this;

.. tPeople
lPeopleID
sLastName
sFirstName
sTitle
etc etc

.. tContacts
lContactID
lPeopleID
sContact

Now add your People, then add all Contacts for each Person, relating tContact with tPeople by storing the value of tPeople.lPeopleID in tContacts.lPeopleID ... you could use any database for this (Access would work fine, if you have it, otherwise MySQL or SQL Server Express).
0
 

Author Comment

by:ashblynn02
ID: 20382593
Yes I am storing these in text files... the above code I have everything there they are just not going in the right boxes for some reason. but everything matches up to what I have on the form (txtfirstname, ect.)

I will try to do what you are saying. I am new with VB so I'm not 100% for sure what I am doing, but I will try to play around with it and do it that way. It seems pretty easy!!!

Thanks :)
0
 
LVL 85
ID: 20391061
Yes, VB is pretty user-friendly, for the most part. Difficult to say why your data isn't going to the correct places, but still - this type of application is almost custom-made for a database backend. Textfiles are fine, and very portable, but they can make working with the data difficult, and you can end up with a real mish-mash of info. With a database, you'll have well structure data that is stored correctly, so you can easily search for information. For example, if you use Textfiles, and you have several contacts with a LastName of Smith, how would you locate those IF you have the info stored in separate textfiles? You could open each file and check it, but that can become unwieldy after a while and can cause some serious performance hits. With a database, you issue a simple query and the db reports back to you all Contacts with a LastName='Smith', quickly and easily!
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:ashblynn02
ID: 20392165
What if I do want to keep it with just text files though?? I have my whole address book in text files so I thought it would be easiest just to keep it that way... I can attach my file that I have so far so you can see what actually is going on if that would be the easist?

Thanks again for your help!
0
 

Author Comment

by:ashblynn02
ID: 20393331
I figured that part out thanks :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question