Jeanette Durham
asked on
On a computer without Access, how do I import a csv into an access table? (using vb.net 2005)
Ok, I have this program which downloads csv files in order to update it's tables (in an access db). I want to import these csvs into my access db, (this is all in vb.net 2000).
First I tried the Access 11.0 library, but when I tried running the code to automate access and execute my macro to transferText it erred out. This is the code I was using:
Private Sub CallMacroInAccess(ByVal dbPath$, ByVal csvPath$, ByVal tableName$)
Dim oAccess As Access.ApplicationClass
'Start Access and open the database.
oAccess = New Access.ApplicationClass
'CreateObject("Access.Appl ication")
'oAccess.Visible = True
'You will need to put the path to your own database here.
oAccess.OpenCurrentDatabas e(dbPath, False)
'Run the macro.
If tableName = "AffInfo" Then
'oAccess.Run("ImportTxtFil e_AffInfo" )
oAccess.DoCmd.RunMacro("Im portTxtFil e_AffInfo" )
'oAccess.DoCmd.TransferTex t(Access.A cTextTrans ferType.ac ImportDeli m, _
' "", tableName, csvPath)
Else
oAccess.DoCmd.RunMacro("Im portTxtFil e_SplitNam es")
End If
oAccess.DoCmd().Quit(Acces s.AcQuitOp tion.acQui tSaveNone)
System.Runtime.InteropServ ices.Marsh al. _
ReleaseComObject(oAccess)
oAccess = Nothing
End Sub
Anyways, I need to know how to do a TransferText into my access db when there Access is NOT installed on the computer.. Does anyone know a good fast way to do this? (My csvs are over 400,000 records apiece, and semi-sizable.)
Another thing I've tried is this builtin csv reader.. here is the code for that. It was agonizingly slow but it did work.. (perhaps because my csv has fields which include vbcrlf inside them to seperate strings, that is my theory..)
Private Function LoginForm_importCSV2(ByVal pathName As String, ByVal AffTrue_SplitFalse As Boolean) As Long
myDB.InitOleDBConnection()
'
Me.ProgressBar1.Value = 0
Me.ProgressBar1.Maximum = countLinesInTextFile(pathN ame)
Me.ProgressBar1.Visible = True
Dim recordsAdded As Long = 0
Dim fields As String(), fieldNames As String()
Dim delimiter As String = ","
gImportDupKnt = 0
'
Using parser As New FileIO.TextFieldParser(pat hName)
parser.SetDelimiters(delim iter)
fieldNames = parser.ReadFields()
While Not parser.EndOfData
' Read in the fields for the current line
fields = parser.ReadFields()
Me.ProgressBar1.Value = Me.ProgressBar1.Value + 1
Me.ProgressBar1.Refresh()
If UBound(fieldNames) <> UBound(fields) Then Stop
'If myDB.writeImportSet(fieldN ames, fields) = True Then
Dim result As Boolean
If AffTrue_SplitFalse = True Then
Debug.Print(Replace(Join(f ields, ","), vbCrLf, "|"))
result = write_AffInfo(fieldNames, fields)
Else
result = write_SplitNames(fieldName s, fields)
End If
If result = True Then
recordsAdded = recordsAdded + 1
Else
gImportDupKnt += 1
End If
Me.fldDoing.Text = recordsAdded & ":" & gImportDupKnt
My.Application.DoEvents()
End While
'importCSV = recordsAdded
End Using
cleanexit:
Me.ProgressBar1.Visible = False
myDB.CloseOleDBConnection( )
Return recordsAdded
End Function
Private Function write_AffInfo(ByVal fieldnames(), ByVal fieldvalues()) As Boolean
Dim mySQL$, strFields$, strValues$
strFields = UCase(makeListForInsertSQL (fieldname s, "[", "]"))
strValues = UCase(makeListForInsertSQL (fieldvalu es, "'", "'"))
mySQL = "INSERT INTO AffInfo " & strFields & " VALUES " & strValues & ";"
myOLEDBCommand = New OleDb.OleDbCommand(mySQL, myOleDBConnection)
Dim result As Long = 0
On Error GoTo myErrHandler
result = myOLEDBCommand.ExecuteNonQ uery()
cleanup:
myOLEDBCommand.Dispose()
Return IIf(result > 0, True, False)
Exit Function
myErrHandler:
If InStr(Err.Description, "duplicate") > 0 Then Err.Clear()
GoTo cleanup
End Function
What's the best way to do this? Another function I'm worried about is how to compact & repair a db if they don't have access, but perhaps that is a different question..
Thanks Experts! ~ Michael
First I tried the Access 11.0 library, but when I tried running the code to automate access and execute my macro to transferText it erred out. This is the code I was using:
Private Sub CallMacroInAccess(ByVal dbPath$, ByVal csvPath$, ByVal tableName$)
Dim oAccess As Access.ApplicationClass
'Start Access and open the database.
oAccess = New Access.ApplicationClass
'CreateObject("Access.Appl
'oAccess.Visible = True
'You will need to put the path to your own database here.
oAccess.OpenCurrentDatabas
'Run the macro.
If tableName = "AffInfo" Then
'oAccess.Run("ImportTxtFil
oAccess.DoCmd.RunMacro("Im
'oAccess.DoCmd.TransferTex
' "", tableName, csvPath)
Else
oAccess.DoCmd.RunMacro("Im
End If
oAccess.DoCmd().Quit(Acces
System.Runtime.InteropServ
ReleaseComObject(oAccess)
oAccess = Nothing
End Sub
Anyways, I need to know how to do a TransferText into my access db when there Access is NOT installed on the computer.. Does anyone know a good fast way to do this? (My csvs are over 400,000 records apiece, and semi-sizable.)
Another thing I've tried is this builtin csv reader.. here is the code for that. It was agonizingly slow but it did work.. (perhaps because my csv has fields which include vbcrlf inside them to seperate strings, that is my theory..)
Private Function LoginForm_importCSV2(ByVal
myDB.InitOleDBConnection()
'
Me.ProgressBar1.Value = 0
Me.ProgressBar1.Maximum = countLinesInTextFile(pathN
Me.ProgressBar1.Visible = True
Dim recordsAdded As Long = 0
Dim fields As String(), fieldNames As String()
Dim delimiter As String = ","
gImportDupKnt = 0
'
Using parser As New FileIO.TextFieldParser(pat
parser.SetDelimiters(delim
fieldNames = parser.ReadFields()
While Not parser.EndOfData
' Read in the fields for the current line
fields = parser.ReadFields()
Me.ProgressBar1.Value = Me.ProgressBar1.Value + 1
Me.ProgressBar1.Refresh()
If UBound(fieldNames) <> UBound(fields) Then Stop
'If myDB.writeImportSet(fieldN
Dim result As Boolean
If AffTrue_SplitFalse = True Then
Debug.Print(Replace(Join(f
result = write_AffInfo(fieldNames, fields)
Else
result = write_SplitNames(fieldName
End If
If result = True Then
recordsAdded = recordsAdded + 1
Else
gImportDupKnt += 1
End If
Me.fldDoing.Text = recordsAdded & ":" & gImportDupKnt
My.Application.DoEvents()
End While
'importCSV = recordsAdded
End Using
cleanexit:
Me.ProgressBar1.Visible = False
myDB.CloseOleDBConnection(
Return recordsAdded
End Function
Private Function write_AffInfo(ByVal fieldnames(), ByVal fieldvalues()) As Boolean
Dim mySQL$, strFields$, strValues$
strFields = UCase(makeListForInsertSQL
strValues = UCase(makeListForInsertSQL
mySQL = "INSERT INTO AffInfo " & strFields & " VALUES " & strValues & ";"
myOLEDBCommand = New OleDb.OleDbCommand(mySQL, myOleDBConnection)
Dim result As Long = 0
On Error GoTo myErrHandler
result = myOLEDBCommand.ExecuteNonQ
cleanup:
myOLEDBCommand.Dispose()
Return IIf(result > 0, True, False)
Exit Function
myErrHandler:
If InStr(Err.Description, "duplicate") > 0 Then Err.Clear()
GoTo cleanup
End Function
What's the best way to do this? Another function I'm worried about is how to compact & repair a db if they don't have access, but perhaps that is a different question..
Thanks Experts! ~ Michael
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I also tried to use the OLEDB library, I'm probably doing this wrong, it keeps saying that the C:\creditwatchexpress\affI nfo_update .csv is not a valid path name..
This is the function I was attempting to use:
Public Sub LoadCSVIntoDT(ByVal csvPath$, ByVal tableName$)
Try
Try
Dim strConnectionString As String
Dim strPathToTextFile As String
strPathToTextFile = csvPath
strConnectionString = "Provider=Microsoft.Jet.OL EDB.4.0;" & _
"Data Source=" & strPathToTextFile & ";" & _
"Extended Properties=""text;HDR=NO;F MT=Delimit ed"""
Dim objConnection As New System.Data.OleDb.OleDbCon nection(st rConnectio nString)
objConnection.Open()
Dim objDA As New OleDbDataAdapter("SELECT * FROM CSV.txt", objConnection)
objDA.Fill(myDS, tableName)
myfuncs.debugPrintDataTabl e(myDS.Tab les(tableN ame))
objConnection.Close()
Catch ex As Exception
Debug.Print(ex.ToString())
End Try
Catch Exc As Exception
Debug.Print("Error: " & Exc.Message)
End Try
End Sub
This is the function I was attempting to use:
Public Sub LoadCSVIntoDT(ByVal csvPath$, ByVal tableName$)
Try
Try
Dim strConnectionString As String
Dim strPathToTextFile As String
strPathToTextFile = csvPath
strConnectionString = "Provider=Microsoft.Jet.OL
"Data Source=" & strPathToTextFile & ";" & _
"Extended Properties=""text;HDR=NO;F
Dim objConnection As New System.Data.OleDb.OleDbCon
objConnection.Open()
Dim objDA As New OleDbDataAdapter("SELECT * FROM CSV.txt", objConnection)
objDA.Fill(myDS, tableName)
myfuncs.debugPrintDataTabl
objConnection.Close()
Catch ex As Exception
Debug.Print(ex.ToString())
End Try
Catch Exc As Exception
Debug.Print("Error: " & Exc.Message)
End Try
End Sub
ASKER
Ok, I've altered my functions like so:
Public Sub LoadCSVIntoDataTable(ByVal csvPath$, ByVal tableName$)
Dim conn As System.Data.Odbc.OdbcConne ction
Dim strConnstr$ = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + csvPath + ";"
conn = New Odbc.OdbcConnection(strCon nstr)
Dim da As New System.Data.Odbc.OdbcDataA dapter("Se lect * From AffInfo_Update.csv", strConnstr)
da.Fill(myDS, "CSV")
'myOLEDBAdapter.Fill(myDS, "Output")
myfuncs.debugPrintDataTabl e(myDS.Tab les("CSV") )
End Sub
Public Sub LoadCSVIntoDT(ByVal csvFolderPath$, ByVal csvFileName$, ByVal tableName$)
Try
Try
Dim strConnectionString As String
Dim strPathToTextFile As String
strPathToTextFile = csvFolderPath & csvFileName
strConnectionString = "Provider=Microsoft.Jet.OL EDB.4.0;" & _
"Data Source=" & csvFolderPath & ";" & _
"Extended Properties=""text;HDR=NO;F MT=Delimit ed"""
Dim objConnection As New System.Data.OleDb.OleDbCon nection(st rConnectio nString)
objConnection.Open()
Dim objDA As New OleDbDataAdapter("SELECT * FROM " & csvFileName, objConnection)
objDA.Fill(myDS, tableName)
myfuncs.debugPrintDataTabl e(myDS.Tab les(tableN ame))
objConnection.Close()
Catch ex As Exception
Debug.Print(ex.ToString())
End Try
Catch Exc As Exception
Debug.Print("Error: " & Exc.Message)
End Try
End Sub
Now using these arguments, I can actually make it work:
?mydb.LoadCSVIntoDataTable ("C:\Credi tWatchExpr ess\", "zTable")
?mydb.LoadCSVIntoDT("C:\Cr editWatchE xpress\", "AffInfo_Update.csv", "zTable")
Thank you, The Learned One, for your help with this!
~Michael
Public Sub LoadCSVIntoDataTable(ByVal
Dim conn As System.Data.Odbc.OdbcConne
Dim strConnstr$ = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + csvPath + ";"
conn = New Odbc.OdbcConnection(strCon
Dim da As New System.Data.Odbc.OdbcDataA
da.Fill(myDS, "CSV")
'myOLEDBAdapter.Fill(myDS,
myfuncs.debugPrintDataTabl
End Sub
Public Sub LoadCSVIntoDT(ByVal csvFolderPath$, ByVal csvFileName$, ByVal tableName$)
Try
Try
Dim strConnectionString As String
Dim strPathToTextFile As String
strPathToTextFile = csvFolderPath & csvFileName
strConnectionString = "Provider=Microsoft.Jet.OL
"Data Source=" & csvFolderPath & ";" & _
"Extended Properties=""text;HDR=NO;F
Dim objConnection As New System.Data.OleDb.OleDbCon
objConnection.Open()
Dim objDA As New OleDbDataAdapter("SELECT * FROM " & csvFileName, objConnection)
objDA.Fill(myDS, tableName)
myfuncs.debugPrintDataTabl
objConnection.Close()
Catch ex As Exception
Debug.Print(ex.ToString())
End Try
Catch Exc As Exception
Debug.Print("Error: " & Exc.Message)
End Try
End Sub
Now using these arguments, I can actually make it work:
?mydb.LoadCSVIntoDataTable
?mydb.LoadCSVIntoDT("C:\Cr
Thank you, The Learned One, for your help with this!
~Michael
ASKER
Public Sub LoadCSVIntoDataTable(ByVal
Dim conn As System.Data.Odbc.OdbcConne
Dim strConnstr$ = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + csvPath + ";"
conn = New Odbc.OdbcConnection(strCon
Dim da As New System.Data.Odbc.OdbcDataA
da.Fill(myDS, "CSV")
'myOLEDBAdapter.Fill(myDS,
myfuncs.debugPrintDataTabl
End Sub
ERROR [HY024] [Microsoft][ODBC Text Driver] '(unknown)' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.
ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr failed
ERROR [HY024] [Microsoft][ODBC Text Driver] '(unknown)' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.