Link to home
Start Free TrialLog in
Avatar of jwellis
jwellis

asked on

Upload a txt file from through ACCESS 97 from VB 6.0

I would like to be able to upload a txt file from VB, into ACCESS 97.  What's the trick.  Please help.  Thanks.
Avatar of halapaya
halapaya

Why can't you write what ever in the text file, to a access data field which datatype is of Memo ??

no specail tricks.. same way as u write any other data[text] to the database
Avatar of jwellis

ASKER

That sounds simple enought.  But, I'm a littl new at this stuff and need an example of what you are talking about.  When the file is uploaded to ACCESS, it needs to sort data into different tables and then delete duplicates within each table.
ASKER CERTIFIED SOLUTION
Avatar of Ruchi
Ruchi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jwellis

ASKER

Ruchi, that looks like it might work but one thing, my data is not have comma delimiters.  Is there anything I need to different since this is the case.
   
Ok, you can change the type data to whatever..

aFields = SplitString(sRecord, "whatever")


Another one ...

Place three text boxes and a combobox and a command button on the form.


Option Explicit
Private Sub cmdImport_Click()
Dim delimiter As String
Dim wks As Workspace
Dim db As Database
Dim fnum As Integer
Dim text_line As String
Dim sql_statement As String
Dim pos As Integer
Dim num_records As Long

    delimiter = cboDelimiter.Text
    If Len(delimiter) = 0 Then
        MsgBox "Please select a delimiter"
        Exit Sub
    End If

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

    ' Open the text file.
    fnum = FreeFile
    On Error GoTo NoTextFile
    Open txtTextFile.Text For Input As fnum

    ' Open the database.
    On Error GoTo NoDatabase
    Set wks = DBEngine.Workspaces(0)
    Set db = wks.OpenDatabase(txtDatabaseFile.Text)
    On Error GoTo 0

    ' Read the file and create records.
    Do While Not EOF(fnum)
        ' Read a text line.
        Line Input #fnum, text_line
        If Len(text_line) > 0 Then
            ' Build an INSERT statement.
            sql_statement = "INSERT INTO " & _
                txtTable.Text & " VALUES ("
            Do While Len(text_line) > 0
                pos = InStr(text_line, delimiter)
                If pos = 0 Then
                    ' Add the rest of the line.
                    sql_statement = sql_statement & _
                        "'" & text_line & "', "
                    text_line = ""
                Else
                    ' Add the next field.
                    sql_statement = sql_statement & _
                        "'" & Left$(text_line, pos - 1) & _
                        "', "
                    text_line = Mid$(text_line, pos + Len(delimiter))
                End If
            Loop

            ' Remove the last comma.
            sql_statement = Left$(sql_statement, Len(sql_statement) - 2) & ")"

            ' Insert the record.
            On Error GoTo SQLError
            db.Execute sql_statement
            On Error GoTo 0
            num_records = num_records + 1
        End If
    Loop

    ' Close the file and database.
    Close fnum
    db.Close
    wks.Close
    MsgBox "Inserted " & Format$(num_records) & " records"
    Exit Sub

NoTextFile:
    MsgBox "Error opening text file."
    Exit Sub

NoDatabase:
    MsgBox "Error opening database."
    Close fnum
    Exit Sub

SQLError:
    MsgBox "Error executing SQL statement '" & _
        sql_statement & "'"
    Close fnum
    db.Close
    wks.Close
    Exit Sub
End Sub
Private Sub Form_Load()
    ' Enter default file and database names.
    txtTextFile.Text = App.Path & "\testdata.txt"
    txtDatabaseFile.Text = App.Path & "\testdata.mdb"
End Sub


Avatar of jwellis

ASKER

Thanks for the help Ruchi.