VB读写Txt文件

每筆記錄以一行表示,每筆記錄的欄位已"@"符號區分.每個欄位的長度皆有其最長的限制.且欄位的定義是以區隔符號位置計算,若該記錄於某欄位後放置分行符號,則其後未填入的欄位以空值填入.

'content 一行记录内容

'contents 全部记录内容数组

'i contents动态数组的长度

Dim content As String
Dim contents() As String
Dim i As Integer

' Function writeTxt  生成文件名为Filename的txt文件,内容为currenttxts
Function writeTxt(Filename As String, currenttxts As Variant)
Dim j As Integer

'Filename其实是文件路径,currenttxts是一个数组
Filename = "C:\" & Filename & ".txt"

'UBound取得currenttxts数组的长度
Debug.Print UBound(currenttxts)

'Open文件For 后面的属性有三个

'append  是在原来内容的基础上添加

'output  重写多行内容

'input 重写记录,并且写记录指针不下移,固定指在第一行

Open Filename For Output As #1
For j = 1 To UBound(currenttxts)
Print #1, currenttxts(j)
Debug.Print currenttxts(j)
Next
Close #1

'打开该TextDocument 文件
Shell "notepad " & Filename
End Function

'读取Field的Value field
Function writeField(field As String)
Dim nField As String
nField = fieldIsEmpty(field)
content = content & nField & "@"
Debug.Print content
End Function
Function writeLine(contenttxt As String)

i = i + 1

'动态定义数组
ReDim Preserve contents(i)
contents(i) = contenttxt
Debug.Print contents(i)

End Function

'判断Field是否为Null或者为""
Function fieldIsEmpty(field As String) As String
Dim mField As String
If field = Null Or field = "" Then
mField = ""
Else
mField = field
End If
fieldIsEmpty = mField
End Function

'生成一个Txt文件
Function createText(sCompany As String, sFileName As String)
Dim rsData As adodb.Recordset, rptCond As clsSQLGenerator, sCommand As String
Set rptCond = New clsSQLGenerator
Call rptCond.sqlRangeCond("MemberID", sMemberID)
Call rptCond.sqlRangeCond("CompanyID", sCompany)
sCommand = "select * from glVoucherDetails " & rptCond.sqlWhereClause(True)

'abPublic是一个数据库连接模块,clsSQLGenerator也是一个sql语句生成模块

Set rsData = dbPublic.ReadSelectCmd(sCommand)
While Not rsData.EOF
Me.writeField (rsData("CompanyID"))
'Me.writeField (rsData("Voucher"))
Me.writeField (rsData("AccountCode"))
Me.writeField (rsData("Currency"))
content = Left(content, Len(content) - 1)
Me.writeLine (content)
content = ""
rsData.MoveNext
Wend
Dim s As String
s = Me.writeTxt(sFileName, contents)
End Function

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值