一个从数据库导出的文本文件部分数据如下:num branch vest_in_site
8300207 8295 121
8300241 8295 121
8300242 8295 121
8300245 8295 121
8300247 8295 121----------------------------------------------------
写如下函数一行一行读取:
Public Function ImportSpeData() As Boolean
Dim a$(), i%, x$, n%, pos%Dim phsnum As String
Dim bureau As String
Dim channel As String
On Error GoTo errhandle
CommonDialog1.Filter = "txt File|*.txt"
CommonDialog1.ShowOpen
Dim FileName As String
FileName = CommonDialog1.FileName
Text1.Text = FileName
Open FileName For Input As #1 ' 打开文件
Do While Not EOF(1)
Line Input #1, x
'以下循环是将数据间的多个空格压缩成一个空格,因为可能字段值间空格会变
Do
n = Len(x)
x = Replace(x, " ", " ")
Loop While n > Len(x)
If x Like "*[A-Za-z]*" Then '如果含有字母,表示为第一行,是字段名,不处理
Else
a = Split(x, " ") '将数据分割到数组A中
Dim j As Integer
phsnum = a(0) '为什么在这里a(0)的值为一整行的数据,而下面a(1)没值,于是越界退出?
bureau = a(1)
channel = a(2)
End If
Loop
ImportSpeData = True
Close #1
Exit Function
errhandle:
ImportSpeData = False
End Function
8300207 8295 121
8300241 8295 121
8300242 8295 121
8300245 8295 121
8300247 8295 121----------------------------------------------------
写如下函数一行一行读取:
Public Function ImportSpeData() As Boolean
Dim a$(), i%, x$, n%, pos%Dim phsnum As String
Dim bureau As String
Dim channel As String
On Error GoTo errhandle
CommonDialog1.Filter = "txt File|*.txt"
CommonDialog1.ShowOpen
Dim FileName As String
FileName = CommonDialog1.FileName
Text1.Text = FileName
Open FileName For Input As #1 ' 打开文件
Do While Not EOF(1)
Line Input #1, x
'以下循环是将数据间的多个空格压缩成一个空格,因为可能字段值间空格会变
Do
n = Len(x)
x = Replace(x, " ", " ")
Loop While n > Len(x)
If x Like "*[A-Za-z]*" Then '如果含有字母,表示为第一行,是字段名,不处理
Else
a = Split(x, " ") '将数据分割到数组A中
Dim j As Integer
phsnum = a(0) '为什么在这里a(0)的值为一整行的数据,而下面a(1)没值,于是越界退出?
bureau = a(1)
channel = a(2)
End If
Loop
ImportSpeData = True
Close #1
Exit Function
errhandle:
ImportSpeData = False
End Function
解决方案 »
- vb6.0串口接收数据的程序,老是提示下表出界,哪位高手来指导一下!
- 请问谁知道如何将添加好的imagelist里面的图转换成二进制数据?
- treeview控件,各个结点node怎么才能有一个+号和一个小图片?
- vb中存储过程的应用?
- 批更改数据,多进程修改数据库,会出什么问题?
- 如何限制一个dcom组件只能在某台特定的机器上运行?
- 如何将文本框(TEXT1.TEXT)中的内容自动保存到mdb文件中?
- ?使用MSFlexGrid在点击他的时候可以使被点的那一行整行变一下颜色吗?
- 数据库打印问题
- 怎么用DAO连接带口令的ACCESS数据库?
- VB中如何获取当前字体的Twip宽度和高度?
- VB datareport 问题
这句没用到,是我程序拷出来时忘了删
直接用RS打开吧..
参考一下:Dim conn As New ADODB.Connection
Dim rs As New ADODB.RecordsetPrivate Sub Form_Load()
Dim ConnStr As String
ConnStr = "Provider=MSDASQL.1;" & _
"DRIVER={Microsoft Text Driver (*.txt; *.csv)};" & _
"DBQ=E:\DEMO\txt"
conn.Open ConnStr
rs.CursorLocation = adUseClient
rs.Open "Select * From score2.txt", conn, adOpenDynamic, adLockPessimistic
DisplayRecordset
End SubPrivate Sub DisplayRecordset() ' 列出 Recordset 的所有记录
Dim S As String, i As Integer
rs.MoveFirst
List1.Clear
While Not rs.EOF
S = ""
For i = 0 To rs.Fields.Count - 1
S = S & rs.Fields(i).Value & vbTab
Next
List1.AddItem S
rs.MoveNext
Wend
End Sub
x = Replace(x, vbtab, " ")
x = Replace(x, " ", " ")
a = Split(x, " ") '将数据分割到数组A中
换成
a = Split(x,vbTab) '将数据分割到数组A中
试试