Discussion:
ListBoxes
(too old to reply)
Tim^^BOB
2003-10-04 20:04:10 UTC
Permalink
I want to make an application that holds phone numbers. It has a
textbox, a listbox and a commandbutton. All i want to do is whenever
you press the command button, the text in the textbox is added to the
listbox and saved to a file (eg. "mynumbers.txt). Also all the numbers
need to be loaded when the application is started. I dont understand
why my code wont work!! This is what I have now:

'command button code
Private Sub AddPhone_Click()
For I = 1 To 10
If PhoneNumbers(I) = "" Then
A = I
End If
Next I
If A = "" Then
MsgBox "CANNOT ADD MORE NUMBERS"
Exit Sub
Else
PhoneNumbers(A) = NewPhone.Text
PhoneList.AddItem PhoneNumbers(A)
End If

Open "mynumbers.txt" For Random As #1
For I = 1 To 10
Put #1, I, PhoneNumbers(I)
Next I
Close #1

End Sub


'form load codePrivate Sub Form_Load()
Open "mynumbers.txt" For Random As #1
For I = 1 To 10
PhoneList.AddItem PhoneNumbers(I)
Next I
Close #1



End Sub

Thank you in advance everyone.
Dr Hangman
2003-10-05 01:14:30 UTC
Permalink
I had trouble with using Random Access Files, this should put them
sequentialy...
now, this will change the way the file looks. and it will re-write the file
each time, so if you have your listbox sorted, they will be in the right
spot...
assuming the listbox is 'PhoneList' and the textbox is 'NewPhone' and the
command button is "AddPhone"

Private Sub AddPhone_Click()
PhoneList.AddItem NewPhone.Text
NewPhone.Text = ""
Open App.Path & "\mynumbers.txt" For Output As #1
For I = 0 To (PhoneList.ListCount - 1)
Write #1, PhoneList.List(I)
Next I
Close #1
End Sub

Sub Form_Load()
Dim I As Integer
Dim PhoneNumber As String
Open App.Path & "\mynumbers.txt" For Input As #1
Do Until EOF(1)
Input #1, PhoneNumber
PhoneList.AddItem PhoneNumber
Loop
Close #1
End Sub


Now this last piece is for making sure they enter only numbers
You can use it or use a masked text box with the mask "###-####" or "(###)
###-####" depending on if you want 7 or 10 digits...

Private Sub NewPhone_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case vbKeyBack
Case Else
KeyAscii = 0
End Select
End Sub

You might also want to do a check to see if they entered enough numbers...
I think you were trying to use an array, in which you needed to declare it
and input values into it when the form loaded...


Dr Hangman
Post by Tim^^BOB
I want to make an application that holds phone numbers. It has a
textbox, a listbox and a commandbutton. All i want to do is whenever
you press the command button, the text in the textbox is added to the
listbox and saved to a file (eg. "mynumbers.txt). Also all the numbers
need to be loaded when the application is started. I dont understand
'command button code
Private Sub AddPhone_Click()
For I = 1 To 10
If PhoneNumbers(I) = "" Then
A = I
End If
Next I
If A = "" Then
MsgBox "CANNOT ADD MORE NUMBERS"
Exit Sub
Else
PhoneNumbers(A) = NewPhone.Text
PhoneList.AddItem PhoneNumbers(A)
End If
Open "mynumbers.txt" For Random As #1
For I = 1 To 10
Put #1, I, PhoneNumbers(I)
Next I
Close #1
End Sub
'form load codePrivate Sub Form_Load()
Open "mynumbers.txt" For Random As #1
For I = 1 To 10
PhoneList.AddItem PhoneNumbers(I)
Next I
Close #1
End Sub
Thank you in advance everyone.
Tim^^BOB
2003-10-11 21:54:42 UTC
Permalink
Thank you for your help Dr. Hangman. It was appreciated. Now my
application works! Thanks!
Dr Hangman
2003-10-12 01:44:19 UTC
Permalink
anytime, I will post as long as I can remember to hit 'Reply To Group'
button instead of just reply...

I sent one the other day, but it didn't seem to take, I'll just re-send
it...

Dr Hangman
Post by Tim^^BOB
Thank you for your help Dr. Hangman. It was appreciated. Now my
application works! Thanks!
Continue reading on narkive:
Loading...