我正在用一个按钮制作一个简单的表单。单击 comCount (按钮)时,我希望我的程序打开一个 txt 文件并在表(tblResults)中返回文本并显示 .txt ..(animals.txt)中每个值的计数
.txt 文件中的输入如下所示
猫 猫 狗 鸟 鱼 猫 狗
等等。每行只有一只动物
我不知道该怎么做,所以如果有人可以帮助我,我将不胜感激。然后 tblResult 应该返回
猫 3 狗 2 鸟 1 鱼 1
谢谢!
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:\Animals.txt" 'Enter your full path here
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
还假设您的表中有两个字段,AnimalName 和 AnimalCount