import csv file to mdb using vb 2008

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello,
I am fairly new to vs 2008 and need some help. After searching high and low for help along the way, I have modified many helpful sources to come up with the attached code. What I am trying to acomplish is I need to import a txt or csv file and export
it to an mdb file and then eventually back again. Everything works fine with the attached code when ever the path of the txt/csv file is inserted into the code. However the problem I am having is that I do not always know where the enduser is going to place
the txt/csv file. When I run the app, I get the following error message, "Not a valid File Name". Any help would be greatly appreciated. Also I would like to see some sample ideas on how to use vb 2008 to move the table in the mdb file to a comma delimited
txt or csv file. Thanks!!
Private Sub btnTransferClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownLoadPLU.Click<br/>
Dim GetItemsSuccess, NoF<br/>
Dim AccessConn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source="" + C:PLUProTestdata.mdb")<br/>
Dim ofd As New OpenFileDialog
ofd.Filter = "Text files (*.txt)|*.txt|csv files (*.csv)|*.csv"
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim fullpath As String = ofd.FileName
Dim pathonly As String = fullpath.Replace(ofd.SafeFileName, "")
Dim dt As New DataTable<br/>
strPath = ofd.FileName<br/>
Me.txtFullPLUFILEPath.Text = strPath<br/>
Extract the file name<br/>
strFile = Mid(strPath, InStrRev(strPath, "") + 1)<br/>
Separate out the path and put in my record<br/>
Me.txtPLUFilePath.Text = LSet(strPath, Len(strPath) - Len(strFile))<br/>
Put in the file name<br/>
Me.txtPLUFileName.Text = strFile
End If<br/>
<br/>
AccessConn.Open()
Dim AccessCommand As New System.Data.OleDb.OleDbCommand("SELECT * INTO [Table1] (F1, F2, F3, F4, F5) SELECT F1, F2, F3, F4, F5 FROM [Text;DATABASE=strPath].[strFile]", AccessConn)
RowResult = AccessCommand.ExecuteNonQuery()<br/>
AccessConn.Close()<br/>
MsgBox("Number of items Imported =" & RowResult, MsgBoxStyle.OkOnly, "SUCCESS")
Exit Sub
End Sub <hr class="sig Charles

View the full article
 
Back
Top