Results 1 to 8 of 8

Thread: Transfer Data from the Flat File to the Database

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 1999
    Posts
    15

    Post

    How do you write a program to transfer the data from the flat file (inventory.dat) to the database (inventory.mdb)? Thank you...


  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    The Flat File would have to be Formatted so that you could distinguish between Fields, either by knowing the Length of each Field or by having a Delimiter Character Seperating each Field.

    You would then read the File in a Line/Record at a Time, seperate the Fields and Store each in its Appropriate Database Field, repeating the Process until the End of the Flat File is Reached.

    If the Order of the fields in both the Flat File and the DB match it's even easier, eg.

    Something like..
    Code:
    Dim aFields As Variant
    Dim sRecord As String
    Dim iField As Integer
    Dim oDB As Database
    Dim oRS As Recordset
    
    Set oDB = WorkSpaces(0).OpenDatabase("C:\MyDB.mdb")
    Set oRS = oDB.OpenRecordSet("SELECT * FROM MyTable", dbOpenDynaset)
    Open "C:\FlatFile.dat" For Input As 1
    While Not EOF(1)
        LineInput #1, sRecord
        'Assuming a Comma Delimiter
        aFields = Split(sRecord, ",")
        oRs.AddNew
        For iField = 0 To Ubound(aFields)
            oRs(iField) = aFields(iField)
        Next
        oRs.Update
    Wend
    Of course you'd have different Data Types and you'd need to convert them before assigning them to the Fields, but you get the Idea.

    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 1999
    Posts
    15

    Post

    Aaron, thank you very much...


  4. #4
    Member
    Join Date
    Dec 1999
    Posts
    37

    Post

    For VB6 using split function

    For VB5

    aFields = Split(sRecord, ",")
    =====================
    If InStr(sRecord, ",") > 0 Then
    aFields(i) = Trim(Left(sRecord, InStr(sRecord, ",") - 1))
    sRecord = Mid(sRecord, InStr(sRecord, ",") + 1)
    Else
    aFields(i) = Trim(sRecord)
    sRecord = ""
    End If

    I think this should work.

    Ruchi

  5. #5
    Addicted Member
    Join Date
    Sep 1999
    Posts
    154

    Post

    The code errors out because the value I am trying to assign to the aField is a string(for instance 01) and this does not seem to be a valid variant data type. The other question I have is do you know if the split function will grab a line of data and if there are 13 commas, will it return the value of 13. I ask this because I believe the previous code will only split two variables with 1 comma. I think I can write code to deal with the commas but I would preffer something as simple as the split function.

  6. #6
    Member
    Join Date
    Dec 1999
    Posts
    37

    Post

    The code I wrote gave you an idea for the split function. I haven't tried to test it myself. Any ideas for Steve Thomas?


    Ruchi

  7. #7
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    Heres a Split function I wrote a while ago:
    Code:
    Public Function SplitString(ByVal sString As String, ByVal sSeperator As String) As Variant
        Dim sStrings() As String
        Dim iStrings As Integer
        Dim iPos As Integer
        
        If sString = "" Then Exit Function
        iPos = 1
        Do
            ReDim Preserve sStrings(iStrings)
            iPos = InStr(sString, sSeperator)
            sStrings(iStrings) = sString
            If iPos Then sStrings(iStrings) = Left(sString, iPos)
            sString = IIf(iPos, Mid(sString, iPos + 1), "")
            iStrings = iStrings + 1
        Loop While Len(sString) > 0
        
        SplitString = sStrings
    End Function
    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]


  8. #8
    Addicted Member
    Join Date
    Sep 1999
    Posts
    154

    Post

    I am running VB5 and the split function is not recognized. Any suggestions?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width