Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Copy Excel Data into Access Database Table.

Copy Excel Data into Access Database Table.

Scheduled Pinned Locked Moved Visual Basic
databasetutorialquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jeshra279
    wrote on last edited by
    #1

    Hi All, I have one Excel sheet "Student.xls", which has different worksheet as "Sheet1","Sheet2"..like that. Consider now "Sheet1"; it has 3 columns - "Name", "Age", "Sex". All these columns has row entries. Now I have one access database, "alldata.mdb". It has One table "Record" which has 3 fields as same as column name of Excel sheet-- "Name", "Age", "Sex". I want to populate this access database with the entries of Excel sheet from "Sheet1". Can someone let me know how to do this? I tried this: // first connected xl datasheet: Dim filename As String = "C:\Student.xls" Dim con As String con = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & filename & ";" & "Extended Properties=Excel 8.0;" Dim connection As OleDbConnection = New OleDbConnection(con) connection.Open() The above Excel connection was succesful, but how to import the data from this excel sheet into the access database. Please suggest. Thanks, R.S.

    D 1 Reply Last reply
    0
    • J jeshra279

      Hi All, I have one Excel sheet "Student.xls", which has different worksheet as "Sheet1","Sheet2"..like that. Consider now "Sheet1"; it has 3 columns - "Name", "Age", "Sex". All these columns has row entries. Now I have one access database, "alldata.mdb". It has One table "Record" which has 3 fields as same as column name of Excel sheet-- "Name", "Age", "Sex". I want to populate this access database with the entries of Excel sheet from "Sheet1". Can someone let me know how to do this? I tried this: // first connected xl datasheet: Dim filename As String = "C:\Student.xls" Dim con As String con = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & filename & ";" & "Extended Properties=Excel 8.0;" Dim connection As OleDbConnection = New OleDbConnection(con) connection.Open() The above Excel connection was succesful, but how to import the data from this excel sheet into the access database. Please suggest. Thanks, R.S.

      D Offline
      D Offline
      DaveAuld
      wrote on last edited by
      #2

      Here you go; http://www.davidhayden.com/blog/dave/archive/2006/05/26/2973.aspx[^]

      Dave Don't forget to rate messages!
      Find Me On: Web|Facebook|Twitter|LinkedIn
      Waving? dave.m.auld[at]googlewave.com

      D J 2 Replies Last reply
      0
      • D DaveAuld

        Here you go; http://www.davidhayden.com/blog/dave/archive/2006/05/26/2973.aspx[^]

        Dave Don't forget to rate messages!
        Find Me On: Web|Facebook|Twitter|LinkedIn
        Waving? dave.m.auld[at]googlewave.com

        D Offline
        D Offline
        Dalek Dave
        wrote on last edited by
        #3

        Good Article

        ------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave

        1 Reply Last reply
        0
        • D DaveAuld

          Here you go; http://www.davidhayden.com/blog/dave/archive/2006/05/26/2973.aspx[^]

          Dave Don't forget to rate messages!
          Find Me On: Web|Facebook|Twitter|LinkedIn
          Waving? dave.m.auld[at]googlewave.com

          J Offline
          J Offline
          jeshra279
          wrote on last edited by
          #4

          I have gone through this, and I tried to implement somehow. The implementation is as follows: // first connected xl datasheet: Dim filename As String = "C:\Student.xls" Dim con As String con = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & filename & ";" & "Extended Properties=Excel 8.0;" Dim connection As OleDbConnection = New OleDbConnection(con) connection.Open() // now copying the excel column into access database table columns: Dim cmd As New OleDbCommand cmd.Connection = connection Try cmd.CommandText = "SELECT * INTO [MS Access;Database=C:\alldata.mdb].[Record] FROM [Sheet1$]" cmd.ExecuteNonQuery() connection.Close() MsgBox("The Import is Compelte") Catch ex As Exception MsgBox("Import Failed, correct Column name in the sheet!") End Try While executing I am getting following error: The Microsoft Jet database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly. I am using Microsoft office 2007 with Access 2007 database. Please tell me how to solve this? Regards, R.S.

          D 1 Reply Last reply
          0
          • J jeshra279

            I have gone through this, and I tried to implement somehow. The implementation is as follows: // first connected xl datasheet: Dim filename As String = "C:\Student.xls" Dim con As String con = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & filename & ";" & "Extended Properties=Excel 8.0;" Dim connection As OleDbConnection = New OleDbConnection(con) connection.Open() // now copying the excel column into access database table columns: Dim cmd As New OleDbCommand cmd.Connection = connection Try cmd.CommandText = "SELECT * INTO [MS Access;Database=C:\alldata.mdb].[Record] FROM [Sheet1$]" cmd.ExecuteNonQuery() connection.Close() MsgBox("The Import is Compelte") Catch ex As Exception MsgBox("Import Failed, correct Column name in the sheet!") End Try While executing I am getting following error: The Microsoft Jet database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly. I am using Microsoft office 2007 with Access 2007 database. Please tell me how to solve this? Regards, R.S.

            D Offline
            D Offline
            DaveAuld
            wrote on last edited by
            #5

            The first thing to do would be to try and populate a dataset to prove the data is first getting read from Excel. If you succeed with that then start trying to populate the access table. I think for experimenting with new code, you are trying to do too much in one step. Start small, bite sized chunks, or you could end up getting confused by it all.

            Dave Don't forget to rate messages!
            Find Me On: Web|Facebook|Twitter|LinkedIn
            Waving? dave.m.auld[at]googlewave.com

            J 1 Reply Last reply
            0
            • D DaveAuld

              The first thing to do would be to try and populate a dataset to prove the data is first getting read from Excel. If you succeed with that then start trying to populate the access table. I think for experimenting with new code, you are trying to do too much in one step. Start small, bite sized chunks, or you could end up getting confused by it all.

              Dave Don't forget to rate messages!
              Find Me On: Web|Facebook|Twitter|LinkedIn
              Waving? dave.m.auld[at]googlewave.com

              J Offline
              J Offline
              jeshra279
              wrote on last edited by
              #6

              I am trying this: Dim ds1 As New DataSet Dim da As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM [Sheet1$]", connection) da.Fill(ds1) But getting same error: The Microsoft Jet database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly.

              D 1 Reply Last reply
              0
              • J jeshra279

                I am trying this: Dim ds1 As New DataSet Dim da As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM [Sheet1$]", connection) da.Fill(ds1) But getting same error: The Microsoft Jet database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly.

                D Offline
                D Offline
                DaveAuld
                wrote on last edited by
                #7

                I created a Student.XLSX file with the 3 columns as you stated in your OP. The code below successfully ran and imported the data to a dataset which was then bound to a DataGridView;

                    'Build the connectionstring
                    Dim conBuilder As New OleDbConnectionStringBuilder
                    conBuilder.Provider = "Microsoft.Jet.OLEDB.4.0"
                    conBuilder.DataSource = "C:\\Users\\Dave Auld\\Desktop\\Student.xlsx"
                    conBuilder.PersistSecurityInfo = False
                    conBuilder.Add("Extended Properties", "Excel 8.0;HDR=YES;")
                
                    'Create the connection
                    Dim con As New OleDbConnection
                    con.ConnectionString = conBuilder.ConnectionString
                
                    'create the data command
                    Dim com As New OleDbCommand("Select \* from \[Sheet1$\]")
                    com.Connection = con
                
                    'create the data adapter
                    Dim da As New OleDbDataAdapter(com)
                
                    'Create the data set
                    Dim ds As New DataSet
                
                    Dim rowcount As Integer = da.Fill(ds)
                
                    'Attach to the data grid view
                    DataGridView1.DataSource = ds
                    DataGridView1.DataMember = ds.Tables(0).TableName
                

                Dave Don't forget to rate messages!
                Find Me On: Web|Facebook|Twitter|LinkedIn
                Waving? dave.m.auld[at]googlewave.com

                J 1 Reply Last reply
                0
                • D DaveAuld

                  I created a Student.XLSX file with the 3 columns as you stated in your OP. The code below successfully ran and imported the data to a dataset which was then bound to a DataGridView;

                      'Build the connectionstring
                      Dim conBuilder As New OleDbConnectionStringBuilder
                      conBuilder.Provider = "Microsoft.Jet.OLEDB.4.0"
                      conBuilder.DataSource = "C:\\Users\\Dave Auld\\Desktop\\Student.xlsx"
                      conBuilder.PersistSecurityInfo = False
                      conBuilder.Add("Extended Properties", "Excel 8.0;HDR=YES;")
                  
                      'Create the connection
                      Dim con As New OleDbConnection
                      con.ConnectionString = conBuilder.ConnectionString
                  
                      'create the data command
                      Dim com As New OleDbCommand("Select \* from \[Sheet1$\]")
                      com.Connection = con
                  
                      'create the data adapter
                      Dim da As New OleDbDataAdapter(com)
                  
                      'Create the data set
                      Dim ds As New DataSet
                  
                      Dim rowcount As Integer = da.Fill(ds)
                  
                      'Attach to the data grid view
                      DataGridView1.DataSource = ds
                      DataGridView1.DataMember = ds.Tables(0).TableName
                  

                  Dave Don't forget to rate messages!
                  Find Me On: Web|Facebook|Twitter|LinkedIn
                  Waving? dave.m.auld[at]googlewave.com

                  J Offline
                  J Offline
                  jeshra279
                  wrote on last edited by
                  #8

                  Hi, I have copied the data in a new dataset "ds1". Please tell me how to copy this "ds1" contents into Access Database. I am trying with following: Dim da2 As New OleDbDataAdapter Dim conn As OleDbConnection Dim cmd As OleDbCommand = New OleDbCommand() conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\alldata.mdb;") cmd = New OleDbCommand("SELECT * FROM Denver", con) da2 = New OleDbDataAdapter(cmd) da1.Fill(ds1, "Denver") But not able to populate the Access database. Can You guide me how to do this?? Thanks, R.S.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups