473,322 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Converting Excel Data to MSAccess .MDB in VB.NET

Hi All,

I have to convert Excel data to MSAcess .MDB file using VB.NET.
VB.NET Code read the Excel file and write it to .MDB file. For the same I have below code, but I am stuck at the writing it to .MDB file. which I have already created with Table as Terms which contains 10 Fields and my Excel contains 10 Fields, Here I want to Extract 3 columns data from Excel to Table.
Also it shows error of "syntax error in INSERT INTO Command.

Coding:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
mstrInputXLSFile = Application.StartupPath + "\XLS_normalized_format.xls"
TextBox1.Text = mstrInputXLSFile
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim mExcelFile As String = mstrInputXLSFile
Dim mAccessFile As String = Application.StartupPath & "\access_schema.mdb"

Dim mWorkSheet As String
mWorkSheet = "Sheet1"

Dim mTableName As String
mTableName = "Terms"

ExcelToAccess(mstrInputXLSFile, mWorkSheet, mAccessFile, mTableName)

'Dim mDataBase As DAO.Database
'mDataBase = DAODBEngine_definst.OpenDatabase(mExcelFile, True, False, "Excel 5.0;HDR=Yes;IMEX=1")
'mDataBase.Execute("Select Term, Description, Comments into [;database=" & mAccessFile & "]." & mTableName & " FROM [" & mWorkSheet & "$]")
'MsgBox("Done. Use Access to view " & mTableName)
End Sub
Private Sub ExcelToAccess(ByVal sourceFile As String, ByVal sourceSheet As String, ByVal targetFile As String, ByVal targetTable As String)

Dim mExcelFile As String = mstrInputXLSFile
Dim mAccessFile As String = Application.StartupPath & "\access_schema.mdb"

Dim mTableName As String
mTableName = "Terms"

Dim mWorkSheet As String
mWorkSheet = "Sheet1"

''''Sub transfers all records from .xls sourcefile.sourcesheet ...
''''... to .mdb targetfile.targettable
''''It is assumed that the .mdb targettable definition already ...
''''... exists, with the same number and types of fields, ...
''''... in the same order, as the .xls worksheet columns.
''''It does not depend on the .mdb field names being the same ...
''''... as the .xls column headings: although it does assume ...
''''... that the .xls columns are named.

If Not mWorkSheet.EndsWith("$") Then
mWorkSheet &= "$"
End If

Dim sourceConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mExcelFile & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
Dim sourceSQL As String = "SELECT term, description, Comments FROM [" & mWorkSheet & "]"
Dim targetConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mAccessFile & ";User Id=admin;Password=;"
Dim targetSQL As String = "SELECT term, desc, Comments FROM " & mTableName

'use dataadapter for target and command builder to ...
'... create insert command, including parameter collection
Dim targetCon As New OleDbConnection(targetConStr)
Dim targetDA As New OleDbDataAdapter(targetSQL, targetCon)
Dim cb As New OleDbCommandBuilder(targetDA)
Dim targetCommand As OleDbCommand = cb.GetInsertCommand
'To Know the source and target values.............
MsgBox(targetCommand.CommandText)
'Debug.WriteLine(targetCommand.CommandText)

'now do the work
Dim sourceCon As New OleDbConnection(sourceConStr)
Dim sourceCommand As New OleDbCommand(sourceSQL, sourceCon)
targetCommand.Connection.Open()
sourceCon.Open()
Dim sourceReader As OleDbDataReader
sourceReader = sourceCommand.ExecuteReader()
While sourceReader.Read() 'for each row from source
Try
For i As Integer = 0 To sourceReader.FieldCount - 1
'load values into parameters

''Only if .xls columns match exactly to .MDB Table Fields
'targetCommand.Parameters(i).Value = sourceReader(i)

'' If there is no Exact Match than give "targetfieldnumber" and respective Sourcefeildname or number like:

''targetCommand.Parameters(<AccessFieldNumber>).Va lue = sourceReader.Item(<ExcelFieldName>)
''targetCommand.Parameters(<AccessFieldNumber>).Va lue = sourceReader.Item(<ExcelFieldNumber>)

targetCommand.Parameters(1).Value = sourceReader.Item("Term")
targetCommand.Parameters(2).Value = sourceReader.Item("desc")
Next

'then write to target
targetCommand.ExecuteNonQuery()

Catch ex As OleDbException
Dim dbe As OleDbError
Dim strmsg As String
For Each dbe In ex.Errors
strmsg &= "SQL Error: " & dbe.Message & vbCrLf
Next
MessageBox.Show(strmsg, "OleDBException", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End While
sourceReader.Close()
sourceCon.Close()
targetCommand.Connection.Close()
End Sub
May 4 '06 #1
1 11280
Zerin
64
dear friend,

I'm also stucked in the same problem and my problem is that................it's related to my job.I guess,if I can't solve this problem,I'll loose my job.

I guess,you have solved the problem already cause your post is quite old.So, please please please give me your code and please advice me how to do it.

My e-mail address is: zerinzafrin[at]gmail[dot]com
Dec 4 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Nagarajan .A | last post by:
I need to read an excel file, and add that data into msaccess database. Can anyone help me? Thanks in advance. *** Sent via Developersdex http://www.developersdex.com *** Don't just...
0
by: CLarkou | last post by:
In Office XP, I am sending data from MSAccess to Excel, via DDE commands and by using excel commands .CELLS(). Both ways are used in the same procedure. On all machines it works except on a machine...
9
by: Andi Plotsky | last post by:
I have an Access app (running under Access2000) where I want to export the data to an Excel spreadsheet. This has worked fine in the past, but after taking it to my client site (where I ran it...
1
by: Ramakrishnan Nagarajan | last post by:
Hi, I am converting Excel data into a Dataset in C#. There are around 24 columns in the Excel Sheet. First I tried to insert one row with correct values in the Excel sheet. i.e. for text columns...
3
by: David | last post by:
It looks like this is a very popular subject, but all the previous messages have been deleated from the server. In my application, I need to export a table in my MSAccess database to an Excel 2003...
1
by: UKuser | last post by:
Hi Guys, I have a program which converts Excel spreadsheets to Javascript and allows interactivity. However it can't convert it to PHP, which is obviously better for users to view (in case J/S...
1
by: garry.oxnard | last post by:
Can anyone help me to solve a problem which involves switching from Access to Excel (then back to Access) programatically please? I have an Excel template which, on open, also opens an Access...
3
by: stronghold888 | last post by:
Hello everyone, I created a database in Access and on my form, I inserted a button that exports data into a ready Excel template. The export works fine, but I have one problem and it's from my...
7
by: beulajo | last post by:
Hai. I have student marks in excel sheet. I need to export this data in to msaccess. I have no problem in exporting the data. My excel sheet is in this format I have 40 students. Each student had...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.