Giserno
11-05-2013, 10:58 AM
Hello Friends.
I'm making a simple program that counts animals from a .txt file when a button is pushed, and then return them in a table. My problem is that i get a runtime error (2135) each time i push my button. Here is the code i use.
Private Sub comCount_Click() Dim sFileName As String
Dim sAnimal As String
Dim sQuery As String
Dim dbs As DAO.Database
Dim rsSQL As DAO.Recordset
Dim iCount As Integer
Set dbs = CurrentDb
sFileName = "C:\Users\Giserno\Desktop\Animals.txt"
Open sFileName For Input As #1
While Not EOF(1)
Line Input #1, sAnimal
sQuery = "Select * from tblResults where AnimalName = """ & sAnimal & """"
Set rsSQL = dbs.OpenRecordset(sQuery)
If rsSQL.RecordCount = 0 Then
rsSQL.AddNew
rsSQL.Fields("AnimalName") = sAnimal
rsSQL.Fields("AnimalCount") = 1
rsSQL.Update
Else
rsSQL.Edit
Count = rsSQL.Fields("AnimalCount") + 1
rsSQL.Fields("AnimalCount") = iCount
rsSQL.Update
End If
rsSQL.Close
Wend
Close #1
End Sub
The input in the .txt file looks like this:
Dog
Dog
Dog
Cat
Cat
Bird
and when put in the table it should look like
dog 3
Cat 2
Bird 1
Does anyone know how i can fix this?
I'm making a simple program that counts animals from a .txt file when a button is pushed, and then return them in a table. My problem is that i get a runtime error (2135) each time i push my button. Here is the code i use.
Private Sub comCount_Click() Dim sFileName As String
Dim sAnimal As String
Dim sQuery As String
Dim dbs As DAO.Database
Dim rsSQL As DAO.Recordset
Dim iCount As Integer
Set dbs = CurrentDb
sFileName = "C:\Users\Giserno\Desktop\Animals.txt"
Open sFileName For Input As #1
While Not EOF(1)
Line Input #1, sAnimal
sQuery = "Select * from tblResults where AnimalName = """ & sAnimal & """"
Set rsSQL = dbs.OpenRecordset(sQuery)
If rsSQL.RecordCount = 0 Then
rsSQL.AddNew
rsSQL.Fields("AnimalName") = sAnimal
rsSQL.Fields("AnimalCount") = 1
rsSQL.Update
Else
rsSQL.Edit
Count = rsSQL.Fields("AnimalCount") + 1
rsSQL.Fields("AnimalCount") = iCount
rsSQL.Update
End If
rsSQL.Close
Wend
Close #1
End Sub
The input in the .txt file looks like this:
Dog
Dog
Dog
Cat
Cat
Bird
and when put in the table it should look like
dog 3
Cat 2
Bird 1
Does anyone know how i can fix this?