设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 1499|回复: 0
打印 上一主题 下一主题

[Access本身] 如何將文字檔轉入 Access 資料庫?(二) [转帖]

[复制链接]
跳转到指定楼层
1#
发表于 2003-12-22 03:19:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
類別:函數 / 資料庫 / Access / 檔案 編號:0274

--------------------------------------------------------------------------------

主題:如何將文字檔轉入 Access 資料庫?(二)
來源:Spencer Yang
版本:VB6

--------------------------------------------------------------------------------


前言

在文字檔中,有人用『,』、『;』、『*』或其他 ASCII 控制碼以外不常用的符號--只要與文字檔『資料』有區別的符號皆是可以,來區分每一項資料。各種資料長度的大小沒有限制,如下列是以『;』做分隔符號:

4715122013673;Birtney Spears;Zamba
078636769028;Christina Aquilera;RCA
731453600324;Shania Twain;Mercury

要將這種有分隔符號的文字檔轉入資料庫,我們來看看範例:



下載範例程式碼
http://www.vbguide.com.tw/howto/300/data/impdb2.zip
完整程式碼如下:

Private Sub cmdImport_Click()
    Dim objConn As ADODB.Connection
    Dim intFileNumber As Integer     '檔案編號變數
    Dim intPostition As Integer         '位置變數
    Dim lngCount As Long                '計數變數
    Dim strConnection As String       '連結變數
    Dim strDelimiter As String          '分隔符號變數
    Dim strSQL As String                  'SQL 字串變數
    Dim strTextLine As String           '文字列變數
   
    '連結字串。
    strConnection = "rovider=Microsoft.Jet.OLEDB.4.0;Data Source=" & txtDatabaseFile.Text
                    
    '分隔符號處理。
    strDelimiter = cboDelimiter.Text
    If Len(strDelimiter) = 0 Then
        MsgBox "請選擇分隔符號。"
        Exit Sub
    End If

    If strDelimiter = "<space>" Then strDelimiter = " "
    If strDelimiter = "<tab>" Then strDelimiter = vbTab

    '開啟文字檔。
    intFileNumber = FreeFile
    On Error GoTo NoTextFile
    Open txtTextFile.Text For Input As intFileNumber

    '開啟資料庫。
    On Error GoTo NoDatabase
    Set objConn = New ADODB.Connection
    objConn.Open strConnection
    On Error GoTo 0

    '讀入文字檔資料並寫入資料庫。
    Do While Not EOF(intFileNumber)
        '讀入文字檔資料。
        Line Input #intFileNumber, strTextLine

        If Len(strTextLine) > 0 Then
            '建立動態新增查詢 ( Action Query )。
            '第一列資料:
            '4715122013673;Birtney Spears;Zamba
            strSQL = "INSERT INTO " & txtTable.Text & " VALUES ("

            Do While Len(strTextLine) > 0
                intPostition = InStr(strTextLine, strDelimiter)
                If intPostition = 0 Then
                    '假如沒有分隔符號-已至末欄字串,加入剩餘部分。
                    strSQL = strSQL & "'" & strTextLine & "', "
                    strTextLine = ""
                Else
                    '加入本欄字串。
                    strSQL = strSQL & "'" & Left(strTextLine, intPostition - 1) & "', "
                    strTextLine = Mid(strTextLine, intPostition + Len(strDelimiter))
                End If
            Loop

            '移除 strSQL 字串裡的最後一個逗點。
            strSQL = Left(strSQL, Len(strSQL) - 2) & ")"

            '新增記錄。
            On Error GoTo SQLError
            objConn.Execute strSQL
            On Error GoTo 0

            '作為計數新增幾筆資料之用。
            lngCount = lngCount + 1
        End If
    Loop

    MsgBox "新增 " & Format(lngCount) & " 筆記錄。"

ExitSub:
    ' 關閉文字檔及資料庫。
    Close intFileNumber
    objConn.Close
    Set objConn = Nothing
    Exit Sub

NoTextFile:
    MsgBox "開啟文字檔錯誤。"
    Exit Sub

NoDatabase:
    MsgBox "開啟資料庫錯誤。"
    Resume ExitSub
   
SQLError:
    MsgBox "執行新增記錄之SQL語法錯誤: '" & strSQL & "'"
    Resume ExitSub
End Sub

Private Sub Form_Load()
    txtTextFile.Text = App.Path & "\test.txt"
    txtDatabaseFile.Text = App.Path & "\test.mdb"
End Sub

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-10-2 15:31 , Processed in 0.080802 second(s), 25 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表