Converting Excel Data to MSAccess .MDB in VB.NET

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2006
Posts: 1
Reputation: ShailShin is an unknown quantity at this point 
Solved Threads: 0
ShailShin ShailShin is offline Offline
Newbie Poster

Converting Excel Data to MSAccess .MDB in VB.NET

 
0
  #1
May 3rd, 2006
Hi All,

I have to convert Excel data to MSAcess .MDB file using VB.NET.
VB.NET Code read the Excel fi;le and write ti 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.

For More Information about Excel Columns are:

term, description, image, comments

Also for .MDB Table Contains:

term_id, term, desc, comments

Here I have to Extract Data from Excel for term description and comments and stored it in .MDB Terms table.


  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. mstrInputXLSFile = Application.StartupPath + "\XLS_normalized_format.xls"
  3. TextBox1.Text = mstrInputXLSFile
  4. End Sub
  5. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  6. Dim mExcelFile As String = mstrInputXLSFile
  7. Dim mAccessFile As String = Application.StartupPath & "\access_schema.mdb"
  8.  
  9. Dim mWorkSheet As String
  10. mWorkSheet = "Sheet1"
  11.  
  12. Dim mTableName As String
  13. mTableName = "Terms"
  14.  
  15. ExcelToAccess(mstrInputXLSFile, mWorkSheet, mAccessFile, mTableName)
  16.  
  17. 'Dim mDataBase As DAO.Database
  18. 'mDataBase = DAODBEngine_definst.OpenDatabase(mExcelFile, True, False, "Excel 5.0;HDR=Yes;IMEX=1")
  19. 'mDataBase.Execute("Select Term, Description, Comments into [;database=" & mAccessFile & "]." & mTableName & " FROM [" & mWorkSheet & "$]")
  20. 'MsgBox("Done. Use Access to view " & mTableName)
  21. End Sub
  22. Private Sub ExcelToAccess(ByVal sourceFile As String, ByVal sourceSheet As String, ByVal targetFile As String, ByVal targetTable As String)
  23.  
  24. Dim mExcelFile As String = mstrInputXLSFile
  25. Dim mAccessFile As String = Application.StartupPath & "\access_schema.mdb"
  26.  
  27. Dim mTableName As String
  28. mTableName = "Terms"
  29.  
  30. Dim mWorkSheet As String
  31. mWorkSheet = "Sheet1"
  32.  
  33. ''''Sub transfers all records from .xls sourcefile.sourcesheet ...
  34. ''''... to .mdb targetfile.targettable
  35. ''''It is assumed that the .mdb targettable definition already ...
  36. ''''... exists, with the same number and types of fields, ...
  37. ''''... in the same order, as the .xls worksheet columns.
  38. ''''It does not depend on the .mdb field names being the same ...
  39. ''''... as the .xls column headings: although it does assume ...
  40. ''''... that the .xls columns are named.
  41.  
  42. If Not mWorkSheet.EndsWith("$") Then
  43. mWorkSheet &= "$"
  44. End If
  45.  
  46. Dim sourceConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mExcelFile & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
  47. Dim sourceSQL As String = "SELECT term, description, Comments FROM [" & mWorkSheet & "]"
  48. Dim targetConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mAccessFile & ";User Id=admin;Password=;"
  49. Dim targetSQL As String = "SELECT term, desc, Comments FROM " & mTableName
  50.  
  51. 'use dataadapter for target and command builder to ...
  52. '... create insert command, including parameter collection
  53. Dim targetCon As New OleDbConnection(targetConStr)
  54. Dim targetDA As New OleDbDataAdapter(targetSQL, targetCon)
  55. Dim cb As New OleDbCommandBuilder(targetDA)
  56. Dim targetCommand As OleDbCommand = cb.GetInsertCommand
  57. 'To Know the source and target values.............
  58. MsgBox(targetCommand.CommandText)
  59. 'Debug.WriteLine(targetCommand.CommandText)
  60.  
  61. 'now do the work
  62. Dim sourceCon As New OleDbConnection(sourceConStr)
  63. Dim sourceCommand As New OleDbCommand(sourceSQL, sourceCon)
  64. targetCommand.Connection.Open()
  65. sourceCon.Open()
  66. Dim sourceReader As OleDbDataReader
  67. sourceReader = sourceCommand.ExecuteReader()
  68. While sourceReader.Read() 'for each row from source
  69. Try
  70. For i As Integer = 0 To sourceReader.FieldCount - 1
  71. 'load values into parameters
  72.  
  73. ''Only if .xls columns match exactly to .MDB Table Fields
  74. 'targetCommand.Parameters(i).Value = sourceReader(i)
  75.  
  76. '' If there is no Exact Match than give "targetfieldnumber" and respective Sourcefeildname or number like:
  77.  
  78. ''targetCommand.Parameters(<AccessFieldNumber>).Value = sourceReader.Item(<ExcelFieldName>)
  79. ''targetCommand.Parameters(<AccessFieldNumber>).Value = sourceReader.Item(<ExcelFieldNumber>)
  80.  
  81. targetCommand.Parameters(1).Value = sourceReader.Item("Term")
  82. targetCommand.Parameters(2).Value = sourceReader.Item("desc")
  83. Next
  84.  
  85. 'then write to target
  86. [B]targetCommand.ExecuteNonQuery()[/B]
  87.  
  88. Catch ex As OleDbException
  89. Dim dbe As OleDbError
  90. Dim strmsg As String
  91. For Each dbe In ex.Errors
  92. strmsg &= "SQL Error: " & dbe.Message & vbCrLf
  93. Next
  94. MessageBox.Show(strmsg, "OleDBException", MessageBoxButtons.OK, MessageBoxIcon.Error)
  95. End Try
  96. End While
  97. sourceReader.Close()
  98. sourceCon.Close()
  99. targetCommand.Connection.Close()
  100. End Sub
Last edited by adatapost; 34 Days Ago at 9:38 pm. Reason: Corrected code tags; Use [code] tags to wrap your source code.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1
Reputation: gmcase0 is an unknown quantity at this point 
Solved Threads: 0
gmcase0 gmcase0 is offline Offline
Newbie Poster

Re: Converting Excel Data to MSAccess .MDB in VB.NET

 
0
  #2
Oct 17th, 2006
Originally Posted by ShailShin View Post
Hi All,

I have to convert Excel data to MSAcess .MDB file using VB.NET.
VB.NET Code read the Excel fi;le and write ti 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.

For More Information about Excel Columns are:

term, description, image, comments

Also for .MDB Table Contains:

term_id, term, desc, comments

Here I have to Extract Data from Excel for term description and comments and stored it in .MDB Terms table.




 

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>).Value = sourceReader.Item(<ExcelFieldName>)
                    ''targetCommand.Parameters(<AccessFieldNumber>).Value = 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

Hey man this is kinda old so I dont know if you figured it out or even still check this, but the answer you are looking for is:

No matter where it is, access, VB, or Excel, chande the word desc to something like descrip or some thing, and it should work
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2
Reputation: samathasri is an unknown quantity at this point 
Solved Threads: 0
samathasri samathasri is offline Offline
Newbie Poster

Re: Converting Excel Data to MSAccess .MDB in VB.NET

 
0
  #3
Feb 10th, 2009
I need clear explanation for the code to converting excel data into MS-ACCESS database
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2
Reputation: samathasri is an unknown quantity at this point 
Solved Threads: 0
samathasri samathasri is offline Offline
Newbie Poster

Re: Converting Excel Data to MSAccess .MDB in VB.NET

 
0
  #4
Feb 10th, 2009
i am unable to understand Application.StartUpPath
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 22
Reputation: bk_bhupendra is an unknown quantity at this point 
Solved Threads: 1
bk_bhupendra bk_bhupendra is offline Offline
Newbie Poster
 
0
  #5
Nov 17th, 2009
hi,

have u got the solution for converting excel to access.
I am also working on such a matter . Can u help me to solve this problem.
i dont know hw the process should start??

thanxs in advance..
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 11947 | Replies: 4
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC