kita akan mencoba convert file txt ke database msAccess...
file awal txt akan seperti ini
buat tabel di ms access untuk menyimpan hasil convertnya...kita buat contohnya tbl_convert
lalu buat form untuk tbl_convert tersebut dan isi buttonnya....
lalu tambahkan script untuk button yang telah kita buat di VB dari msaccess...contoh scriptnya seperti ini
Option Compare Database
Private Sub Command4_Click()
    Dim str As String
    Dim sItems() As String
    Dim lcount As Long
    Dim lastNonEmpty As Integer
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim x As Integer
    Dim no As Integer
    Dim strPath As String
    Dim Extention As String
    Dim File As String
    Set db = CurrentDb()
    'strFileName = "D:\" & _
    'InputBox("Masukkan File text " & _
    '"Convert File : ", "Import File")
    no = 0
    x = 1
    strPath = "D:\file_to_access\"
    Extention = "txt"
    If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
    If Trim$(Extention) = "" Then
        Extention = "*.*"
    ElseIf Left$(Extention, 2) <> "*." Then
        Extention = "*." & Extention
    End If
    File = Dir$(strPath & Extention)
    Do While Len(File)
        strFileName = "D:\file_to_access\" & File
        Open (strFileName) For Input As #1
        Set rs = db.OpenRecordset("SELECT * FROM tbl_convert")
        Do While Not EOF(1)
         Line Input #1, str
         If str = "HEADER" Or str = "FOOTER" Then
         Else
            sItems() = Split(str)
            lastNonEmpty = -1
            For i = 0 To UBound(sItems) - 1
                If sItems(i) <> "" Then
                    lastNonEmpty = lastNonEmpty + 1
                    sItems(lastNonEmpty) = sItems(i)
                End If
            Next
            rs.AddNew
                rs.Fields(1) = sItems(0)
                rs.Fields(2) = sItems(1)
                rs.Fields(3) = no
            rs.Update
         End If
        Loop
        Close #1
        no = no + 1
        File = Dir$
    Loop
End Sub
No comments:
Post a Comment