473,758 Members | 4,381 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting Excel Data to MSAccess .MDB in VB.NET

2 New Member
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(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
mstrInputXLSFil e = Application.Sta rtupPath + "\XLS_normalize d_format.xls"
TextBox1.Text = mstrInputXLSFil e
End Sub
Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button2.Click
Dim mExcelFile As String = mstrInputXLSFil e
Dim mAccessFile As String = Application.Sta rtupPath & "\access_schema .mdb"

Dim mWorkSheet As String
mWorkSheet = "Sheet1"

Dim mTableName As String
mTableName = "Terms"

ExcelToAccess(m strInputXLSFile , mWorkSheet, mAccessFile, mTableName)

'Dim mDataBase As DAO.Database
'mDataBase = DAODBEngine_def inst.OpenDataba se(mExcelFile, True, False, "Excel 5.0;HDR=Yes;IME X=1")
'mDataBase.Exec ute("Select Term, Description, Comments into [;database=" & mAccessFile & "]." & mTableName & " FROM [" & mWorkSheet & "$]")
'MsgBox("Done. Use Access to view " & mTableName)
End Sub
Private Sub ExcelToAccess(B yVal sourceFile As String, ByVal sourceSheet As String, ByVal targetFile As String, ByVal targetTable As String)

Dim mExcelFile As String = mstrInputXLSFil e
Dim mAccessFile As String = Application.Sta rtupPath & "\access_schema .mdb"

Dim mTableName As String
mTableName = "Terms"

Dim mWorkSheet As String
mWorkSheet = "Sheet1"

''''Sub transfers all records from .xls sourcefile.sour cesheet ...
''''... to .mdb targetfile.targ ettable
''''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.Ends With("$") Then
mWorkSheet &= "$"
End If

Dim sourceConStr As String = "Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=" & mExcelFile & ";Extended Properties=""Ex cel 8.0;HDR=Yes;IME X=1"""
Dim sourceSQL As String = "SELECT term, description, Comments FROM [" & mWorkSheet & "]"
Dim targetConStr As String = "Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=" & mAccessFile & ";User Id=admin;Passwo rd=;"
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 OleDbDataAdapte r(targetSQL, targetCon)
Dim cb As New OleDbCommandBui lder(targetDA)
Dim targetCommand As OleDbCommand = cb.GetInsertCom mand
'To Know the source and target values......... ....
MsgBox(targetCo mmand.CommandTe xt)
'Debug.WriteLin e(targetCommand .CommandText)

'now do the work
Dim sourceCon As New OleDbConnection (sourceConStr)
Dim sourceCommand As New OleDbCommand(so urceSQL, sourceCon)
targetCommand.C onnection.Open( )
sourceCon.Open( )
Dim sourceReader As OleDbDataReader
sourceReader = sourceCommand.E xecuteReader()
While sourceReader.Re ad() 'for each row from source
Try
For i As Integer = 0 To sourceReader.Fi eldCount - 1
'load values into parameters

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

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

''targetCommand .Parameters(<Ac cessFieldNumber >).Value = sourceReader.It em(<ExcelFieldN ame>)
''targetCommand .Parameters(<Ac cessFieldNumber >).Value = sourceReader.It em(<ExcelFieldN umber>)

targetCommand.P arameters(1).Va lue = sourceReader.It em("Term")
targetCommand.P arameters(2).Va lue = sourceReader.It em("desc")
Next

'then write to target
targetCommand.E xecuteNonQuery( )

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 ", MessageBoxButto ns.OK, MessageBoxIcon. Error)
End Try
End While
sourceReader.Cl ose()
sourceCon.Close ()
targetCommand.C onnection.Close ()
End Sub
May 4 '06 #1
1 11372
Zerin
64 New Member
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
3824
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 participate in USENET...get rewarded for it!
0
1834
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 with French Office XP. Commands of DDE for page setup works but not the ones for sending data (DDEPOKE). I got the international characters for Excel row and column. What could be the problem for DDEPOKE command to fail ? I get the error 285,...
9
1013
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 under Access2002 - but left it as an Access2000 database), I get the following error message. "Class does not support automation or does not support expected interface". THE LINE IT FAILS ON IS: Wks.range ("A2").CopyFromRecordset rs
1
2647
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 I entered text values and for numeric columns I entered numeric values. It works fine and pass through all the validation checks and gets inserted into the database successfully. But when I gave some junk values in the excel sheet and tried to...
3
1649
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 spreadsheet. I have no trouble with individual ranges, but am having trouble finding a good example where the data from an entire table is written to the worksheet. Thanks in advance for any suggestions. David
1
2154
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 is turned off). How would I go about converting some of this created code (example JS below), or is there an easier way to get PHP to do the calculations itself? I am aware of an excel-server product but this is too expensive and doesnt actually...
1
5740
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 database - containing a list of addresses. Sequence of events is = (1) Excel template opens in its default XXX.xls filename. (2) Code runs to save the spreadsheet as XXX.xls. (3) User clicks a button to open an Access database containing an
3
2623
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 code. First of all, the data from the Access database is inserted in an Excel calendar. The calendar is numbered with days 36 to -18. By exemple, if a calendar statement is for day 36, then it will be inserted in the specific column. For now, statements...
7
2638
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 attended 9 tests which has 5 criteria, under which marks had been given(10). one sample data is B0841 Albert Raja A. 0 0 0 0 0 0 0 0 0 0.5 2 2 0 0 1
0
9299
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10076
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9908
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9740
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5332
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3832
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3402
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.