我写的代码如下:
Sub depart(ByVal a As String)
Dim i As Short
Dim j As Short
Dim n As Integer
Dim k As Integer
n = DataSet11.Tables("timing").Rows.Count
' If InStr(1, a, "*") = 0 Then ' 以防没有数字直接读入*end of table
For k = 1 To n
For i = 1 To 3
a = LTrim(a)
j = InStr(1, a, " ")
Select Case i
Case 1
If Len(Trim(a)) = 0 Then
Exit For
Else
DataSet11.Tables("timing").Rows(k - 1)(0) = Val(a.Substring(0, j - 1))
a = Mid(a, j)
End If
Case 2
If Len(a) = 0 Then
Exit For
Else
DataSet11.Tables("timing").Rows(k - 1)(1) = Val(a.Substring(0, j - 1))
a = Mid(a, j)
End If
Case 3
If Len(a) = 0 Then
Exit For
Else
DataSet11.Tables("timing").Rows(k - 1)(2) = Val(a)
End If
End Select
Next i
Next
If InStr(1, a, "* Date records of type dd mm yyyy") <> 0 Then
a = LineInput(1)
Debug.Print(a)
Do
Call depart(a)
Debug.Print(a)
a = LineInput(1)
Loop Until InStr(1, a, "*") <> 0
End If
从txt中找出的数据,都是对的,但是在数据上只显示最后一条记录。。。为什么啊?求大神帮我修改代码
depart改成类似下面的逻辑,没有调测过,自己试吧
Sub depart(ByVal a As String)
Dim i As Short
Dim j As Short
Dim n As Integer
Dim k As Integer
Dim dt as DataTable = DataSet11.Tables("timing")
dim dr as datarow=DataTable.NewRow()
' If InStr(1, a, "*") = 0 Then ' 以防没有数字直接读入*end of table
For i = 1 To 3
a = LTrim(a)
j = InStr(1, a, " ")
Select Case i
Case 1
If Len(Trim(a)) = 0 Then
Exit For
Else
dr(0) = Val(a.Substring(0, j - 1))
a = Mid(a, j)
End If
Case 2
If Len(a) = 0 Then
Exit For
Else
dr(1) = Val(a.Substring(0, j - 1))
a = Mid(a, j)
End If
Case 3
If Len(a) = 0 Then
Exit For
Else
dr(2) = Val(a)
End If
End Select
Next i
dt.rows.add(dr)
'End If
'Debug.Print(k, i, j, n)
End Sub
n = DataSet11.Tables("timing").Rows.Count
这里n是多少
你显示在界面上的代码在哪里?
你怎么绑定的datagrid
Do
Call depart(a)//这里每次都是执行一行的数据,但depart里面n每次都是全部的tables数据,这循环应该是有问题的。这种循环应该是table每次加一行,而不用所有rows重新执行一遍
Debug.Print(a)
a = LineInput(1)
Loop Until InStr(1, a, "*") <> 0
Sub depart(ByVal a As String)
Dim i As Short
Dim j As Short
Dim n As Integer
Dim k As Integer
n = DataSet11.Tables("timing").Rows.Count
' If InStr(1, a, "*") = 0 Then ' 以防没有数字直接读入*end of table
For k = 1 To n
For i = 1 To 3
a = LTrim(a)
j = InStr(1, a, " ")
Select Case i
Case 1
If Len(Trim(a)) = 0 Then
Exit For
Else
DataSet11.Tables("timing").Rows(k - 1)(0) = Val(a.Substring(0, j - 1))
a = Mid(a, j)
End If
Case 2
If Len(a) = 0 Then
Exit For
Else
DataSet11.Tables("timing").Rows(k - 1)(1) = Val(a.Substring(0, j - 1))
a = Mid(a, j)
End If
Case 3
If Len(a) = 0 Then
Exit For
Else
DataSet11.Tables("timing").Rows(k - 1)(2) = Val(a)
End If
End Select
Next i
Next
'End If
'Debug.Print(k, i, j, n)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
OleDbDataAdapter2.Fill(DataSet11, "timing")
OleDbConnection2.Open()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Dim O As String
' Dim VbOFNFileMustExist As Object
Dim a As String
' Dim i As Short
OpenFileDialog1.FileName = ""
OpenFileDialog1.FileName = ""
' OpenFileDialog1.Flags = VbOFNFileMustExist
OpenFileDialog1.Filter = "All Files(*.*)|*.*|KEY.Files(*.KEY)|*.KEY"
OpenFileDialog1.Filter = "All Files(*.*)|*.*|KEY.Files(*.KEY)|*.KEY"
OpenFileDialog1.FilterIndex = 2
OpenFileDialog1.FilterIndex = 2
OpenFileDialog1.Title = "Open File"
OpenFileDialog1.Title = "Open File"
OpenFileDialog1.ShowDialog()
If OpenFileDialog1.FileName = "" Then
MsgBox("No file selected", 37, "checking")
Else
End If
FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input)
Do While Not EOF(1)
a = LineInput(1)
If InStr(1, a, "* Date records of type dd mm yyyy") <> 0 Then
a = LineInput(1)
Debug.Print(a)
Do
Call depart(a)
Debug.Print(a)
a = LineInput(1)
Loop Until InStr(1, a, "*") <> 0
End If
Loop
a = "1"
Debug.Print(a)
FileClose(1)
End Sub
整个代码都不对。depart是干嘛的,你每次读取一行就调用一次,然后循环整个表。
你不是不懂数据库,而是整个不会写程序。