borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Linking MS-Access to Excel)Next Topic (How can i load combobox from \".txt\" document?) New Topic New Poll Post Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : Text to Listbox error - whats wrong with my code?
Poster Message
desdee
Level: Guest


icon Text to Listbox error - whats wrong with my code?

Can anyone explain why I keep getting errors when running this code to import a text file into a list box? I doesn't seem to like line 6:
ListBox1.AddItem textfile
and comes up with a run time error 70; permission denied


Private Sub CommandButton1_Click()

Dim textfile As String

Open "C:\myTextFile.txt" For Input As #1
Do While Not EOF(1)
  Line Input #1, textfile
  ListBox1.AddItem textfile
  Loop
Close #1
CommandButton1.Enabled = False
End Sub

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
If ListBox1(0).Selected Then
MsgBox "This cannot be deleted"
Exit Sub

Else
MsgBox ("Are you sure you want to delete this record?"), vbYesNo
If vbNo Then
Exit Sub
Else

Call CommandButton6_Click

End If
End Sub


Private Sub CommandButton6_Click()

Dim ff As Integer
Dim i As Integer

On Error Resume Next

MsgBox (" Are you sure you want to delete this record?"), vbYesNo
ListBox1.RemoveItem ListBox1.ListIndex
ff = FreeFile
Open "C:\myTextFile.txt" For Output As ff
For i = 0 To ListBox1.ListCount - 1
Print #ff, ListBox1.List(i)
Next
Close #ff
End If
End Sub

Private Sub UserForm_Initialize()

ListBox1.ColumnCount = 57
ListBox1.RowSource = "a1:B4"

ListBox1.ControlSource = "a6"
'Place the ListIndex into cell a6
ListBox1.BoundColumn = 0
End Sub   

01-04-2004 at 07:04 AM
| Quote Reply
neutrall
Level: Master


Registered: 28-03-2004
Posts: 122
icon Re: Text to Listbox error - whats wrong with my code?

One of the thing I notice in your code, when you open a file, you should always ask for a freefile :

Dim X as integer

X = freefile

And then you open the file

You can also use the Dir function to make sure your file actually exist befor you open it.

Daniel B.

____________________________
A Stick give a wise man something to think about... and a fool, something to put in is mouth.

01-04-2004 at 05:49 PM
View Profile Send Email to User Show All Posts Visit Homepage ICQ | Quote Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : Text to Listbox error - whats wrong with my code?
Previous Topic (Linking MS-Access to Excel)Next Topic (How can i load combobox from \".txt\" document?) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder