📄 project1.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "将数据库转换为文本文件"
ClientHeight = 3015
ClientLeft = 1140
ClientTop = 1515
ClientWidth = 5010
LinkTopic = "Form1"
PaletteMode = 1 'UseZOrder
ScaleHeight = 3015
ScaleWidth = 5010
Begin VB.TextBox txtFileName
Height = 285
Left = 600
TabIndex = 4
Top = 1440
Width = 3855
End
Begin VB.TextBox txtDatabaseName
Height = 285
Left = 600
TabIndex = 3
Top = 600
Width = 3855
End
Begin VB.CommandButton cmdExport
Caption = "输 出"
Height = 495
Left = 1800
TabIndex = 2
Top = 2160
Width = 1215
End
Begin VB.Label Label1
Caption = "输出为文本文件: "
Height = 255
Index = 1
Left = 360
TabIndex = 1
Top = 1080
Width = 1575
End
Begin VB.Label Label1
Caption = "数据库:"
Height = 255
Index = 0
Left = 360
TabIndex = 0
Top = 240
Width = 735
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Sub cmdExport_Click()
Dim fnum As Integer
Dim file_name As String
Dim database_name As String
Dim db As Database
Dim rs As Recordset
Dim num_fields As Integer
Dim field_width() As Integer
Dim field_value As String
Dim i As Integer
Dim num_processed As Integer
On Error GoTo MiscError
'打开文本文件
fnum = FreeFile
file_name = txtFileName.Text
Open file_name For Output As fnum
'打开数据库
Set db = OpenDatabase(txtDatabaseName.Text)
' 打开记录集
Set rs = db.OpenRecordset("SELECT * FROM Books")
'字段的个数
num_fields = rs.Fields.Count
ReDim field_width(0 To num_fields - 1)
'写入字段名
For i = 0 To num_fields - 1
field_width(i) = rs.Fields(i).Size
If field_width(i) < Len(rs.Fields(i).Name) Then
field_width(i) = Len(rs.Fields(i).Name)
End If
field_width(i) = field_width(i) + 1
'写入字段名
Print #fnum, rs.Fields(i).Name;
'写入空格
Print #fnum, Space$(field_width(i) - Len(rs.Fields(i).Name));
Next i
Print #fnum, ""
'写入记录
Do While Not rs.EOF
num_processed = num_processed + 1 '记录数
For i = 0 To num_fields - 1
field_value = rs.Fields(i).Value
'写入字段内容
Print #fnum, field_value;
'写入空格
Print #fnum, Space$(field_width(i) - Len(field_value));
Next i
'写入空行
Print #fnum, ""
'下一条记录
rs.MoveNext
Loop
'关闭记录集和数据库
rs.Close
db.Close
'关闭文件
Close fnum
'显示信息
MsgBox "Processed " & Format$(num_processed) & " records.", , "Finished"
Exit Sub
'错误处理
MiscError:
MsgBox "Error " & Err.Number & _
vbCrLf & Err.Description
End Sub
Private Sub Form_Load()
'加载文件与数据库
txtDatabaseName.Text = App.Path & "\book.mdb"
txtFileName.Text = App.Path & "\book.txt"
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -