摘要:將Access轉成txt檔
程式構思 : 
將Access中,常見的北風資料庫裡的訂單主表與明細表,挑選任意欄位,並隨機產生5筆資料,寫入到名為 “testfile.txt” 中。步驟如下:
Step 1: 開起北風資料庫, 並啟動「模組」。 
Step 2 : 撰寫如下的 Code。
Option Compare Database
'**********************************************************
'SmpWrite 模組 : 將北風資料庫,訂單資料之主副表隨機產生5筆
'資料,然後寫入並產生一份文字檔.txt,存放在 C:\XXX.txt 下。
'**********************************************************
Public Sub AccessToTxt()
'產生一份 testfile 文件檔.
Set fstr = CreateObject("Scripting.FileSystemObject")
'先判斷文字檔 testfile.txt 是否存在.
If fstr.FileExists("c:\testfile.txt") Then
Response = MsgBox("已存在相同檔案,是否刪除檔案!?", vbYesNo + vbQuestion, "刪除檔案")
If Response = vbYes Then
fstr.DeleteFile ("c:\testfile.txt")
End If
Else
'建立ADODB.Recordset物件.
Dim rs1 As ADODB.Recordset
Dim rs2 As ADODB.Recordset
Set rs1 = New ADODB.Recordset
Set rs2 = New ADODB.Recordset
'宣告變數.
Dim i As Integer
Dim str As String
str = ""
'開啟[訂貨主檔].
rs1.Open "Select * from 訂貨主檔", CurrentProject.Connection, adOpenKeyset, adLockOptimistic
'隨機指定5筆訂單資料,並撈出對應之明細表.
For i = 0 To 4
rs1.AbsolutePosition = Int((830 * Rnd) + 1)
rs2.Open "Select * from 訂貨明細 where 訂單號碼 =" & rs1("訂單號碼"), CurrentProject.Connection, adOpenKeyset, adLockOptimistic
'將對應的[訂貨明細]寫入.txt檔.
Do While Not rs2.EOF
'Debug.Print rs2("訂單號碼")
str = str + CStr(rs1("訂單號碼")) + ","
str = str + rs1("客戶編號") + ","
str = str + CStr(rs1("訂單日期")) + ","
str = str + CStr(rs1("要貨日期")) + ","
str = str + CStr(rs2("產品編號")) + ","
str = str + CStr(rs2("訂單號碼")) + ","
str = str + CStr(rs2("訂單號碼")) + ","
str = str + Chr(10)
rs2.MoveNext
Loop
rs2.Close
Next
rs1.Close
'產生一份 testfile 文件檔.
Dim a
Set a = fstr.CreateTextFile("c:\testfile.txt", True)
a.Write (str)
Set a = Nothing
Response = MsgBox("已產生文件檔,是否檢視檔案?", vbYesNo + vbQuestion, "檢視檔案")
If Response = vbYes Then
'開起文件檔.
Dim b
Set b = fstr.OpenTextFile("c:\test.txt")
Set b = Nothing
MsgBox ("開啟成功")
End If
End If
Set fstr = Nothing
End Sub
Step 3 : 執行成功後,會在C:\ 下產生testfile.txt 檔。 
打開txt檔案後,原先的換行程式chr(10) 卻變成了「▊」符號!?
為了探究是故為何,於是有了 Step 4 …
Step 4 : 在 Ruby 下編輯一段簡單的讀檔程式。
# 讀取檔案.
filename = ARGV[0]
file = open("testfile.txt")
text = file.read
print text
file.close

凡事,總有更輕鬆的解決方法。