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