Log inUsernamePassword
Log me on automatically each visit    
Register
Register
Log in to check your private messages
Log in to check your private messages
Visual Basic Forum for Visual Basic Programmers VB Forum Index » Visual Basic for Applications

Post new topic   Reply to topic
Phone Numbers VB Phone Book
View previous topic :: View next topic  
Author Message
gphillips
Newbie


Joined: 13 Oct 2005
Posts: 22

PostPosted: Oct 13th, 2005 01:53 PM    Post subject: Phone Numbers VB Phone Book Reply with quote

Guys,

Doing a phonebook form application to enbable me to find Phone numbers and add Phone numbers on VB.6.0.

For some reason the find function does not regognise phone number beginning with 0, i.e 0293788. I have chnaged the data type to string but still only shows 293788 instead of 0293788 when search for a phone number. There must be something simple wrong with my code.

Would really appreciate some help!

This is the code I have split into findcontact /addcontact

ADD contact

Private Sub cmdsave_Click()
Dim Surname As String
Dim PhoneNumber As String
Dim Initials As String
Open "h:\friends" For Append As #1
Surname = txtSurname.Text
Initials = txtInitials.Text
PhoneNumber = txtPhoneNumber.Text
Write #1, Surname, Initials, PhoneNumber
Close #1
End Sub


Find Contact (i.e search for the phone number you have just added using the ADD FORM)

Private Sub cmdFind_Click()
'Program to display the Phone Number of a friends
Dim Surname As String
Dim Initials As String
Dim PhoneNumber As String
Dim SelectedContact As String
SelectedContact = frmFind.txtSurname.Text
Open "h:\friends" For Input As #1
Do While Not EOF(1)
Input #1, Surname, Initials, PhoneNumber
If Surname = SelectedContact Then
frmFind.txtPhoneNumber.Text = Str(PhoneNumber)
frmFind.txtInitials.Text = Initials
End If
Loop
Close #1
End Sub


Many thanks

G
Back to top
View user's profile Send private message
dougthomas
Moderator


Joined: 27 Jul 2005
Posts: 271
Location: Essex, UK

PostPosted: Oct 14th, 2005 05:18 AM    Post subject: Reply with quote

Hi,

I suspect you've still got entries in the file without the Zero, that were put there before you changed the PhoneNumber to a string. Try opening the file with Notepad and see what's actually in it.

Regards
Doug
_________________
If you can see the light at the end of the tunnel, it probably means there's a Train coming.
Back to top
View user's profile Send private message
gphillips
Newbie


Joined: 13 Oct 2005
Posts: 22

PostPosted: Oct 16th, 2005 05:55 AM    Post subject: Phone Book - Retrieving Matching records Reply with quote

Thanks very much doug, it was because i didn't put txt at the end of the file name.

My problem now is that I can't get the find section to retreive contacts if there are matching surnames i.e Jones Jones.

Would prefer this to be done using some kind of loop mechanism which I have tried but frustainly can't get it to work. If I need to add a separate find next command then so be it but I have no idea of the code.

Important to note:

When a surname is used for a search, the file (friends) is read sequentially, and all the records matching the surname are displayed one record at a time.

What needs to be added to acheive matching records issues -

Find contact code


Private Sub cmdFind_Click()
'Program to display the Phone Number of a friends
Dim Surname As String
Dim Initials As String
Dim PhoneNumber As String
Dim SelectedContact As String
SelectedContact = frmFind.txtSurname.Text
Open "h:\friends.txt" For Input As #1
Do While Not EOF(1)
Input #1, Surname, Initials, PhoneNumber
If Surname = SelectedContact Then
frmFind.txtPhoneNumber.Text = PhoneNumber
frmFind.txtInitials.Text = Initials
txtValidation.Text = " You have sucessfully found a phone contact"
End If
Loop
Close #1
End Sub
Back to top
View user's profile Send private message
dougthomas
Moderator


Joined: 27 Jul 2005
Posts: 271
Location: Essex, UK

PostPosted: Oct 16th, 2005 06:19 AM    Post subject: Reply with quote

Hi again,

One problem with your FindNext is that you re-open the file every time you call it and so will always get the same record passed back.

One solution woul dbe to Open the file in say, the Form_Load event and remove it from FindMext. That way FindNext will read from the next record rather than the first.

Other things to watch for are Uppercase and Lowercase, you may accidently put in jones instead of Jones. The test will fail because the two values are no equal. You can overcome this by using StrConv(Surname, vbProperCase), preferably when you write the record in the first place. You can then use:
Code:

If StrConv(Surname, vbProperCase) = strConv(SelectedContact, vbProperCase Then

which will convert whatever is in Surname and SelectedContact to having the first letter in uppercase and the rest in lowercase before doing the test. So jones vs Jones will match
Jones vs jones will match
JONES vs jones will match etc etc.

You could also explore the use of the Like operator (http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/office97/html/output/F1/D6/S5B25F.asp) which would allow you to look for jo* and return jones, johnsone, Jo, etc.

Regards
Doug
_________________
If you can see the light at the end of the tunnel, it probably means there's a Train coming.
Back to top
View user's profile Send private message
gphillips
Newbie


Joined: 13 Oct 2005
Posts: 22

PostPosted: Oct 16th, 2005 12:59 PM    Post subject: Phone Book Reply with quote

Thanks very much, appreciate this

I will come back to that again another time if you don't mind:-

Some fundemental part of the code is wrong

When I add the statment in bold below its not achieving what I want it to achieve in validation text box

Basically :

If Surname exists in friends file retrieve the initials and phonenumber and return
positive response " You have sucessfully found a phone contact".

If Surname does not exists in friends file return mesage "No matching record exists for the surname'

Basically it shows the error message three times repeated - and if the surname is there - there is a mixture of positive/negative error messages again three times.

What code is to be added below to make it work for both positive and negative validations messages.

'Program to display the Phone Number of a friends when searching the surname
Dim Surname As String
Dim Initials As String
Dim PhoneNumber As String
Dim SelectedContact As String
SelectedContact = frmFind.txtSurname.Text
Open "h:\friends.txt" For Input As #1
Do While Not EOF(1)
Input #1, Surname, Initials, PhoneNumber
If Surname = SelectedContact Then
frmFind.txtPhoneNumber.Text = PhoneNumber
frmFind.txtInitials.Text = Initials
frmFind.txtValidation.Text = frmFind.txtValidation.Text & "You have sucessfully found a phone contact"
Else
frmFind.txtValidation.Text = frmFind.txtValidation.Text & "You have not found a matching record in the phone book"
End If
Loop
Close #1
End Sub
Back to top
View user's profile Send private message
dougthomas
Moderator


Joined: 27 Jul 2005
Posts: 271
Location: Essex, UK

PostPosted: Oct 17th, 2005 07:35 AM    Post subject: Reply with quote

Hi,

I'd exit the sub after an entry has been found.
Code:

Dim Surname As String
Dim Initials As String
Dim PhoneNumber As String
Dim SelectedContact As String
SelectedContact = frmFind.txtSurname.Text
Open "h:\friends.txt" For Input As #1
Do While Not EOF(1)
    Input #1, Surname, Initials, PhoneNumber
    If Surname = SelectedContact Then
        frmFind.txtPhoneNumber.Text = PhoneNumber
        frmFind.txtInitials.Text = Initials
        frmFind.txtValidation.Text = frmFind.txtValidation.Text & "You have sucessfully found a phone contact"
        Close #1
        Exit Sub
    End If
Loop
frmFind.txtValidation.Text = frmFind.txtValidation.Text & "You have not found a matching record in the phone book"
Close #1
End Sub


Regards
Doug
_________________
If you can see the light at the end of the tunnel, it probably means there's a Train coming.
Back to top
View user's profile Send private message
gphillips
Newbie


Joined: 13 Oct 2005
Posts: 22

PostPosted: Oct 18th, 2005 02:57 AM    Post subject: Logic of do while code Reply with quote

Thanks once again doug,
why does it not work with else staement? Put simply what is the logic with the added exit sub statement of the code, I don't understand. Why did you change what you did?
i.e exit sub
i.e chage order of Close #1
Exit Sub
End If
Loop
i.e close file v twice

Maybe its best to to explain from Do while down in simple terms

Cheers M8,

G.
Back to top
View user's profile Send private message
dougthomas
Moderator


Joined: 27 Jul 2005
Posts: 271
Location: Essex, UK

PostPosted: Oct 18th, 2005 03:27 AM    Post subject: Reply with quote

Hi,

What was happening before was that you were reading a record and checking if the name matched the one you were looking for, if it did then you updated the textboxes, if it didnt you said you couldn't find it and then you went on the the next record of the file. So you would get a "didn't find it" mesage for all the records in the file that didn't match what you were looking for.

The changes I made were to exit as soon as you found a match. Then the only way you could drop out of the Loop was if you didn't find a match.

Hope that helps
Regards
Doug
_________________
If you can see the light at the end of the tunnel, it probably means there's a Train coming.
Back to top
View user's profile Send private message
gphillips
Newbie


Joined: 13 Oct 2005
Posts: 22

PostPosted: Oct 18th, 2005 08:39 AM    Post subject: Reply with quote

Think I understand, so if:
Names in file were

Ian
Bob
James
Searching for bob, it would have validation messages:-

No matching records - Ian
Sucess match - bob
No match records James

So basically it assigns a validation to every record in this case 3 message will appear above. I this correct?

If my assumption is coorrect. My question why it works - relate to nature of do loops and use of exit subs for false answers?

Greg
Back to top
View user's profile Send private message
gphillips
Newbie


Joined: 13 Oct 2005
Posts: 22

PostPosted: Oct 18th, 2005 09:33 AM    Post subject: Reply with quote

Thanks very much m8.
Please see above about my understanding of what was happenning.
Sorry made typo to my next question.

To be clear of my next question why you code works in relation to.

1. nature of do loops
2. Why you added the extra code / change order of close/end if/exit sub. Why you did what you did?.


Read books and it says do loops must have true staements only, does this mean that after the "else" would have been a false stament therfore it could not handle it.

Not sure if I am getiing mixed up but put simply:-

i.e if then else -can have true and false results - i,e false coming after the else, true between the if and then.
do loops can't have an false outcome. That why it works if i just kept the "succesful match" part only" and not had any negative validations.

As the do - loop has and if statement within it, it gets a little complicated thats all, just trying to grasp it.


Thanks Doug.
Cheers,
G.
Back to top
View user's profile Send private message
gphillips
Newbie


Joined: 13 Oct 2005
Posts: 22

PostPosted: Oct 27th, 2005 12:34 PM    Post subject: Reply with quote

Doug, All,

Tried doing the below, i.e. opening the file in form load but it wouldn’t work. Any more ideas?

By the way I currently only have one find button. Do I need to create a find next command button. What would be the code be to make it search for more than one person who have the same “Surname"?

One solution would be to Open the file in say, the Form_Load event and remove it from FindMext. That way FindNext will read from the next record rather than the first.

Cheers,
G
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Visual Basic Forum for Visual Basic Programmers VB Forum Index » Visual Basic for Applications All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Visual Basic Forum runs phpBB | Forum Template © iOptional
VB Resources | SSL | Visual Basic