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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. How can I add a new column in an excel file correctly?

How can I add a new column in an excel file correctly?

Scheduled Pinned Locked Moved Visual Basic
questionhelp
4 Posts 2 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.
  • A Offline
    A Offline
    ATC
    wrote on last edited by
    #1

    I am able to create a table with some columns in an Excel file & I can add or read data from those columns. But when I try to add a new column (NewInfo) with the following codes ... I had an error "Invalid operation" after line cmd.CommandText = "ALTER TABLE ..." Dim conn1 As New System.Data.OleDb.OleDbConnection(m_sConn1) conn1.Open() Dim cmd As New System.Data.OleDb.OleDbCommand() cmd.Connection = conn1 cmd.CommandText = "ALTER TABLE EmployeeData ADD NewInfo VARCHAR(20) NULL" cmd.ExecuteNonQuery() conn1.Close() Might someone show me what is wrong? Thank in-advance :confused:

    D 1 Reply Last reply
    0
    • A ATC

      I am able to create a table with some columns in an Excel file & I can add or read data from those columns. But when I try to add a new column (NewInfo) with the following codes ... I had an error "Invalid operation" after line cmd.CommandText = "ALTER TABLE ..." Dim conn1 As New System.Data.OleDb.OleDbConnection(m_sConn1) conn1.Open() Dim cmd As New System.Data.OleDb.OleDbCommand() cmd.Connection = conn1 cmd.CommandText = "ALTER TABLE EmployeeData ADD NewInfo VARCHAR(20) NULL" cmd.ExecuteNonQuery() conn1.Close() Might someone show me what is wrong? Thank in-advance :confused:

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      ATC wrote:

      cmd.CommandText = "ALTER TABLE EmployeeData ADD NewInfo VARCHAR(20) NULL" cmd.ExecuteNonQuery() conn1.Close() Might someone show me what is wrong?

      The Excel driver doesn't support the "ALTER TABLE" SQL statement. You have to add the columns using the Excel object model.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      A 1 Reply Last reply
      0
      • D Dave Kreskowiak

        ATC wrote:

        cmd.CommandText = "ALTER TABLE EmployeeData ADD NewInfo VARCHAR(20) NULL" cmd.ExecuteNonQuery() conn1.Close() Might someone show me what is wrong?

        The Excel driver doesn't support the "ALTER TABLE" SQL statement. You have to add the columns using the Excel object model.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        A Offline
        A Offline
        ATC
        wrote on last edited by
        #3

        Thank for the explaination, now I know why it has not work! Would you kindly provide some links to use the Excel object model. I appreciate a lot! Many thanks

        D 1 Reply Last reply
        0
        • A ATC

          Thank for the explaination, now I know why it has not work! Would you kindly provide some links to use the Excel object model. I appreciate a lot! Many thanks

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          MSDN docs on the Excel object model[^], complete with examples.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          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