又是文本转ACCESS,三十分在线等!

SFMN 2003-12-10 09:33:33
文本内容为:
32132132 2132 132132132 1231321321321321 2123132 32123132
12312313212 00 1212323 23123132123123132 212312 3213223
54564556 4545 4545454 45456465464646 5465 5545456
.
.
.
.

怎么样从这一列列字符中,以空格为准,截取字段???
...全文
46 点赞 收藏 10
写回复
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
yoki 2003-12-12
'测试如下:
date time strip_id stat widt thik wrw_1 twid1 bwid1 wrw_2 twid2 bwid2 wrw_3 twid3 bwid3 wrw_4 twid4 bwid4 wrw_5 twid5 bwid5 wrw_6 twid6 bwid6
02.11.03 15:47:57 203110240160 0128 1269 412 0 25016 25033 0 27012 27011 0 25044 25045 0 7094 7674 0 7068 7677 50 7088 7047
02.11.03 15:53:02 203110250170 0128 1269 412 0 25016 25033 0 27012 27011 0 25044 25045 5 7094 7674 4 7068 7677 54 7088 7047
02.11.03 15:56:19 203110250180 0128 1269 412 0 25016 25033 0 27012 27011 0 25044 25045 10 7094 7674 9 7068 7677 57 7088 7047
02.11.03 16:00:38 203110250190 0128 1269 412 0 25016 25033 0 27012 27011 1 25044 25045 16 7094 7674 14 7068 7677 60 7088 7047
02.11.03 16:07:38 203110250200 0128 1269 412 0 25016 25033 1 27012 27011 1 25044 25045 21 7094 7674 19 7068 7677 64 7088 7047
02.11.03 16:12:41 103110260010 0128 1269 412 1 25016 25033 1 27012 27011 2 25044 25045 25 7094 7674 22 7068 7677 66 7088 7047
02.11.03 16:16:08 203110250210 0128 1269 412 1 25016 25033 1 27012 27011 2 25044 25045 30 7094 7674 27 7068 7677 69 7088 7047
02.11.03 16:22:19 103110260020 0128 1269 399 1 25016 25033 2 27012 27011 3 25044 25045 35 7094 7674 32 7068 7677 73 7088 7047
02.11.03 16:26:04 203110250220 0128 1269 399 1 25016 25033 2 27012 27011 3 25044 25045 41 7094 7674 37 7068 7677 76 7088 7047

'将以上你给的例子数据拷贝放到一个文本文件c:\text.txt中
'经过分析,你的文本文件的第一行是以制表符分割,而其他的则是以不定数的空格分割,不是特别有规律,所以在转之前要分别处理,过程FormatTxt就是起这个作用
'程序将生成一个格式化后以"|"为分割符的文本文件c:\zz.txt和一个数据库c:\a.mdb和一个格式符号文件c:\schema.ini
Private Sub StartChange()
Call WriteTempSchemia("zz.txt", "|") '空格吗?
Call FormatTxt("c:\test.txt", "c:\zz.txt")
Call TxtToMdb("c:\", "zz.txt", "c:\a.mdb", "NewTempTable")
End Sub

Public Sub WriteTempSchemia(strFileName As String, strSeparator As String)
'写入格式符号文件
Open "c:\Schema.ini" For Output As #1
Print #1, "[" & strFileName & "]"
Print #1, "Format=Delimited(" & strSeparator & ")"
Close #1
End Sub

Private Sub TxtToMdb(sTxtPath As String, sTxtFileName As String, sAccessFullFileName As String, sAccessTable As String)
'功能:将文本文件导入到Access中的表
'调用:Call TxtToMdb("c:\","zz.txt","c:\a.mdb","NewTempTable")
'结果:将c:\zz.txt导入到c:\a.mdb中的NewTempTable表中
Dim db As DAO.Database
On Error Resume Next
Set db = DBEngine.CreateDatabase(sAccessFullFileName, dbLangGeneral)
If Err.Number = 3204 Then
Set db = Workspaces(0).OpenDatabase(sAccessFullFileName)
End If
On Error GoTo err_exit
db.Execute "SELECT * into " & sAccessTable & " FROM [Text;HDR=NO;DATABASE=" & sTxtPath & "]." & sTxtFileName
db.Close
Set db = Nothing
Exit Sub
err_exit:
Set db = Nothing
MsgBox Err.Description
End Sub

Private Sub FormatTxt(strFromName As String, strToName As String)
'开始格式化文本文件
Dim strTmp As String
Dim strArray() As String
Dim a() As String
Dim i As Integer
Dim flag As Boolean
Dim j As Integer
Dim k As Integer
Dim p As String
Dim q As String

Open strFromName For Input As #1
strTmp = StrConv(InputB(LOF(1), #1), vbUnicode)
Close #1
strArray = Split(strTmp, vbCrLf)
For i = 0 To UBound(strArray)
k = k + 1
strTmp = ""
If k = 1 Then
Open strToName For Output As #1
flag = True
a() = Split(strArray(i), vbTab)
For j = 0 To UBound(a)
strTmp = strTmp & "|" & a(j)
Next j
Print #1, Right(strTmp, Len(strTmp) - 1)
Else
strTmp = strArray(i)
For j = 0 To Len(strTmp)
If Trim(Left(strTmp, 1) & "|") <> "|" Then
p = p & Left(strTmp, 1)
Else
If p <> "" Then
q = q & "|" & p
p = ""
End If
End If
If Len(strTmp) <= 1 Then
strTmp = strTmp
Else
strTmp = Right(strTmp, Len(strTmp) - 1)
End If
Next j
If Len(q) >= 1 Then Print #1, Right(q, Len(q) - 1)
q = ""
End If
Next i
If flag Then Close #1
End Sub

Private Sub Command1_Click()
Call StartChange
End Sub
回复
yoki 2003-12-12
http://expert.csdn.net/Expert/topic/2457/2457457.xml?temp=.4011652
回复
zhangzhijian 2003-12-12
trim(string)可以去掉空格的!
回复
SFMN 2003-12-11
较验?怎么判断?都是空格啊,如果可以判断,那不就不用较验了
回复
SoHo_Andy 2003-12-11
不是手动,是程序做大部分,手工校验
程序要做的就是把空格替换成其它分隔符
回复
SFMN 2003-12-11
用手动处理吗?文本很大,行不通
回复
SoHo_Andy 2003-12-11
你的文本大么,如果自己本身也有空格那就只好先处理文本符合格式在导了
回复
SFMN 2003-12-11
而且,文本中有些字段为空,也就是说自己本身也有空格,晕死了,没有处理的方法吗???
回复
SFMN 2003-12-10
不行啊,中间有空格怎么样处理???
回复
3661512 2003-12-10
你可以看看这篇文摘

如何将一个TXT文件转化成Access格式数据库中的表
http://www.china-askpro.com/msg9/qa42.shtml
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7453

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告