How to insert new Field (column) in query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Moah Scout
    New Member
    • Oct 2010
    • 58

    #1

    How to insert new Field (column) in query

    Hi,
    I have a Select query from two tables, I want in this query to add new column so that I can enter a new data in that column directly. Under normal situations I know its impossiple, but with some sort of Designed VBA or SQL it can be made possible.
    I also know how to insert column as expression in query, tell me how can a new field (not expression) be inserted?
    Please, any hint!!!!
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    You can accomplish this through the combination of ALTER TABLE & ADD COLUMN Syntax. The following Code will add a Field named [Next_Of_Kin] to the Northwind Employees Table. It will be TEXT Data Type with a Maximum Length of 25:
    Code:
    Dim strSQL As String
    
    strSQL = "ALTER TABLE Employees ADD COLUMN Next_Of_Kin TEXT(25);"
    
    CurrentProject.Connection.Execute strSQL

    Comment

    • Moah Scout
      New Member
      • Oct 2010
      • 58

      #3
      I have ambitions of using the code but couldnt make it.
      Where can I exactly past the code?. Am not much familiar with access!

      Comment

      • Moah Scout
        New Member
        • Oct 2010
        • 58

        #4
        Sorry it works!
        I have created a Module and run! It has created a new column named Next_of_Kin as you have stipulated, But if you run the Code against Query it gives error!
        "Cannot execute data definition Statements on linked data structure"
        How to get rid of this error?

        Comment

        • Moah Scout
          New Member
          • Oct 2010
          • 58

          #5
          I have tried to past the code [ALTER TABLE Employees ADD COLUMN Next_Of_Kin TEXT(25)]on the data definition of the query DesignView but gives the same error as above and it works for tables as well!

          Comment

          • Mr Key
            New Member
            • Aug 2010
            • 132

            #6
            You may try to create table from query first and then insert a new column or field as you wish

            Comment

            Working...