Click to See Complete Forum and Search --> : Inport .csv File into a Access DB Programmatically


BMLekki
April 12th, 2001, 02:09 PM
I need to be able to inport a .csv file into an access database programmatically. If you have any ideas please let me know.
Thanks,
Brian

Iouri
April 12th, 2001, 02:30 PM
private Sub Form_Load()
CommonDialog1.ShowOpen
If len(CommonDialog1.Filename) > 0 then ParseFile (CommonDialog1.Filename)
End Sub

public Function ParseFile(Filename as string, optional ParseChar as string = "|") as Boolean
Dim sArray() as string
Dim intNum as Integer
Dim lngCnt as Long
Dim inputString as string
Dim lngLoop as Long

intNum = FreeFile'Read the File Into an Array
Open Filename for input as intNum
Do While Not EOF(intNum)
Line input #intNum, inputString
'if you want to put into excel then use this and write sarray to an output file
'sArray(lngLoop) = Replace(sArray(lngLoop), "|", vbTab)

ReDim Preserve sArray(lngCnt)
sArray(lngCnt) = inputString
lngCnt = lngCnt + 1
Loop'Close the File
Close intNum
Dim sRowArray() as string
Dim RowLoop as Long
'Split each line into an array
for lngLoop = 0 to lngCnt - 1
sRowArray = Split(sArray(lngLoop), ParseChar)
'Read each field in the line
for RowLoop = 0 to UBound(sRowArray) - 1
Debug.print sRowArray(RowLoop)
' Here you would probably want to do your field assingment and do an addnew
next
next
End Function




Iouri Boutchkine
iouri@hotsheet.com

Cubbie
April 12th, 2001, 02:54 PM
This code will import into a table in Access
[vbcode]
Dim appAccess As New Access.Application
'***********************************************************************
'************* Open Access database and transfer file to DB ************
'Must select Access 8 Object Library for Access 97 &
'Access 9 Library for Access 2000 in Project|References
'Open Database DB Path Exclusive
appAccess.OpenCurrentDatabase "Path\Some..MDB", True

' Transfer text file to database
appAccess.DoCmd.TransferText acImportDelim, , "Tablename", "Path\somefile.csv", 1

' Close
appAccess.CloseCurrentDatabase

' Set object to Nothing
Set appAccess = Nothing
[\vbcode]