
vb编程,关于查找txt文件匹配行后再读取几行数据的问题,求高手解答!
我正在设计一个查询系统,就是先输入id,然后即可查询出此id的基本信息。文本如下1234010.50.8422011/6/1420分钟123510.522.50.8422...
我正在设计一个查询系统,就是先输入id,然后即可查询出此id的基本信息。
文本如下
1234
0
10.5
0.842
2011/6/14
20分钟
1235
10.5
22.5
0.842
2011/6/14
25分钟
1236
22.5
41
0.842
2011/6/14
1小时
1237
41
65.5
0.842
2011/6/15
4小时
文本中的1234,1235,1236....就是需要索引的id号,请问如何编写程序,才能使得我在
text1(0).text中输入“1234”时
点击“查询”按钮,可以把txt文本“1234”所在行的后5行数据写入text1(1)到(5)中去
?
再加上查询不到id号就报错,例如输入12348,就会执行
if ....... then msgbox"超出记录范围!",vbokonly,"警告":exit sub 展开
文本如下
1234
0
10.5
0.842
2011/6/14
20分钟
1235
10.5
22.5
0.842
2011/6/14
25分钟
1236
22.5
41
0.842
2011/6/14
1小时
1237
41
65.5
0.842
2011/6/15
4小时
文本中的1234,1235,1236....就是需要索引的id号,请问如何编写程序,才能使得我在
text1(0).text中输入“1234”时
点击“查询”按钮,可以把txt文本“1234”所在行的后5行数据写入text1(1)到(5)中去
?
再加上查询不到id号就报错,例如输入12348,就会执行
if ....... then msgbox"超出记录范围!",vbokonly,"警告":exit sub 展开
3个回答
展开全部
Private Sub Command1_Click()
Dim id() As String
Dim lenth As Integer
Open "C:\Documents and Settings\Administrator\桌面\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, tmp
lenth = lenth + 1
Loop
Close #1
ReDim id(0 To lenth) As String
Open "C:\Documents and Settings\Administrator\桌面\1.txt" For Input As #1
Do While Not EOF(1)
Input #1, id(i)
i = i + 1
Loop
Close #1
For i = 1 To lenth - 1
If id(i) = Text1(0).Text Then
Text1(1).Text = id(i + 1)
Text1(2).Text = id(i + 2)
Text1(3).Text = id(i + 3)
Text1(4).Text = id(i + 4)
Text1(5).Text = id(i + 5)
End If
Next
End Sub
Dim id() As String
Dim lenth As Integer
Open "C:\Documents and Settings\Administrator\桌面\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, tmp
lenth = lenth + 1
Loop
Close #1
ReDim id(0 To lenth) As String
Open "C:\Documents and Settings\Administrator\桌面\1.txt" For Input As #1
Do While Not EOF(1)
Input #1, id(i)
i = i + 1
Loop
Close #1
For i = 1 To lenth - 1
If id(i) = Text1(0).Text Then
Text1(1).Text = id(i + 1)
Text1(2).Text = id(i + 2)
Text1(3).Text = id(i + 3)
Text1(4).Text = id(i + 4)
Text1(5).Text = id(i + 5)
End If
Next
End Sub
追问
你的程序虽然有些小错误,但是我修改后已经能运行了,请问能否在加上查询不到id号就报错,例如if ....... then msgbox"超出记录范围!",vbokonly,"警告":exit sub
还有,因为我是初学者,所以能不能请你把语句的作用简单些注释一下?谢谢,如果完成,我会把分给你的。
追答
Private Sub Command1_Click()
Dim id() As String
Dim lenth As Integer
''''''''''''''以下是读取文本文件的长度,赋值给lenth
Open "C:\Documents and Settings\Administrator\桌面\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, tmp
lenth = lenth + 1
Loop
Close #1
'''''''以下是把文本文件的值赋值给id(i)数组
ReDim id(0 To lenth) As String '重新定义数组id(i),数组个数是lenth
Open "C:\Documents and Settings\Administrator\桌面\1.txt" For Input As #1
Do While Not EOF(1)
Input #1, id(i)
i = i + 1
Loop
Close #1
'''''''''''''''以下是把text1(0).text的值在id()数组里循环对照,如果相等,则,其他的text等于依次往下的几个数
For i = 1 To lenth - 1
If id(i) = Text1(0).Text Then
Text1(1).Text = id(i + 1)
Text1(2).Text = id(i + 2)
Text1(3).Text = id(i + 3)
Text1(4).Text = id(i + 4)
Text1(5).Text = id(i + 5)
elseif i=lenth-1 then '''''''此处为新增
msgbox ("超出记录范围!") '''''''此处为新增
End If
Next
End Sub
展开全部
我给你提供思路:
1,定义一个动态数组
2,Line Input语句读取文本文件到动态数组中
3,循环查找动态数组
1,定义一个动态数组
2,Line Input语句读取文本文件到动态数组中
3,循环查找动态数组
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
假充文本文件名为c:\1.txt,程序代码如下:
Private Sub command1_click()
Dim R As String, S As String
S = Text1(0).Text
Open "c:\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, R
If R = S Then Exit Do
Loop
Line Input #1, R
Text1(1).Text = R
Line Input #1, R
Text1(2).Text = R
Line Input #1, R
Text1(3).Text = R
Line Input #1, R
Text1(4).Text = R
Line Input #1, R
Text1(5).Text = R
Close #1
End Sub
Private Sub command1_click()
Dim R As String, S As String
S = Text1(0).Text
Open "c:\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, R
If R = S Then Exit Do
Loop
Line Input #1, R
Text1(1).Text = R
Line Input #1, R
Text1(2).Text = R
Line Input #1, R
Text1(3).Text = R
Line Input #1, R
Text1(4).Text = R
Line Input #1, R
Text1(5).Text = R
Close #1
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询