Results 1 to 22 of 22

Thread: Error trying to ALTER TABLE in an Access database

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2023
    Posts
    6

    Error trying to ALTER TABLE in an Access database

    I have a simple problem: I am trying to add a column to an existing table using the ALTER TABLE command. I pulled the code off the MS documentation:

    ALTER TABLE Notes_file
    ADD DateOfBirth date;

    However, when I do this I get an error: "Query must have at least one destination field." Not sure what this means at all; a search didn't do much to enlighten me. I've tried using different tables/queries and adding different columns and datatypes. Not sure where I am going wrong.

    I want to embed this is a VB.net program and also have a question as to how I execute the. To create the query I have been using the .CreateQueryDef command. Would I use this or perhaps .Execute?

    Thanks for any help.

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,554

    Re: Error trying to ALTER TABLE in an Access database

    For future reference SQL questions goes to Database forums.

    https://www.sqlservertutorial.net/sq...le-add-column/
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2023
    Posts
    6

    Re: Error trying to ALTER TABLE in an Access database

    Well, the link just repeats all the other documentation. When I use this command I get the error indicated, hence the post. I also want guidance concering implementation within VB.net, hence the post.

  4. #4
    PowerPoster
    Join Date
    Nov 2017
    Posts
    2,895

    Re: Error trying to ALTER TABLE in an Access database

    https://learn.microsoft.com/en-us/of...oft-access-sql

    If you look at the example statement on this page, you see that you need to use ADD COLUMN, not just ADD.

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2023
    Posts
    6

    Re: Error trying to ALTER TABLE in an Access database

    Thanks for answering, but I think you misread the directions. Column is in lowercase; it is the (variable) name to be assigned the column. If you scroll down a bit to the code examples, you will see what I mean.

  6. #6
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,148

    Re: Error trying to ALTER TABLE in an Access database

    Quote Originally Posted by WMR1 View Post
    Thanks for answering, but I think you misread the directions. Column is in lowercase; it is the (variable) name to be assigned the column. If you scroll down a bit to the code examples, you will see what I mean.
    It seems you've misread or probably went to the wrong link. COLUMN is not lower case in the link provided by OP1.

  7. #7
    PowerPoster
    Join Date
    Nov 2017
    Posts
    2,895

    Re: Error trying to ALTER TABLE in an Access database

    Quote Originally Posted by WMR1 View Post
    Thanks for answering, but I think you misread the directions. Column is in lowercase; it is the (variable) name to be assigned the column. If you scroll down a bit to the code examples, you will see what I mean.
    Wrong. Good luck.

  8. #8
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,012

    Re: Error trying to ALTER TABLE in an Access database

    here a sample

    Code:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\DB2010.accdb" & ";Persist Security Info=True")
            conn.Open()
            Dim cmd As New OleDb.OleDbCommand("", conn)
            cmd.CommandText = "ALTER TABLE Customer ADD DateOfBirth date;"
            cmd.ExecuteNonQuery()
            conn.Close()
        End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  9. #9

    Thread Starter
    New Member
    Join Date
    Sep 2023
    Posts
    6

    Re: Error trying to ALTER TABLE in an Access database

    Here is another from the link provided by sapator;

    ALTER TABLE table_name
    ADD
    column_name_1 data_type_1 column_constraint_1,
    column_name_2 data_type_2 column_constraint_2,


    Note the absence of COLUMN following ADD. So I am perplexed why you insist I am wrong.
    ...,
    column_name_n data_type_n column_constraint_n;

  10. #10
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,148

    Re: Error trying to ALTER TABLE in an Access database

    Quote Originally Posted by ChrisE View Post
    here a sample

    Code:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\DB2010.accdb" & ";Persist Security Info=True")
            conn.Open()
            Dim cmd As New OleDb.OleDbCommand("", conn)
            cmd.CommandText = "ALTER TABLE Customer ADD DateOfBirth date;"
            cmd.ExecuteNonQuery()
            conn.Close()
        End Sub
    Interesting. It seems to work with or without "COLUMN".

  11. #11
    PowerPoster
    Join Date
    Nov 2017
    Posts
    2,895

    Re: Error trying to ALTER TABLE in an Access database

    Quote Originally Posted by WMR1 View Post
    Here is another from the link provided by sapator;

    ALTER TABLE table_name
    ADD
    column_name_1 data_type_1 column_constraint_1,
    column_name_2 data_type_2 column_constraint_2,


    Note the absence of COLUMN following ADD. So I am perplexed why you insist I am wrong.
    ...,
    column_name_n data_type_n column_constraint_n;
    Because your response to mine was just wrong. The link I provided showed the following example:

    Code:
    ALTER TABLE Employees ADD COLUMN Notes TEXT(25)
    How, exactly, is "column" in lowercase in that example?

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,622

    Re: Error trying to ALTER TABLE in an Access database

    Quote Originally Posted by OptionBase1 View Post
    Because your response to mine was just wrong. The link I provided showed the following example:

    Code:
    ALTER TABLE Employees ADD COLUMN Notes TEXT(25)
    How, exactly, is "column" in lowercase in that example?
    True, but as Wes seemed to show, the word COLUMN appears to be optional, though I always include it.
    My usual boring signature: Nothing

  13. #13
    PowerPoster
    Join Date
    Nov 2017
    Posts
    2,895

    Re: Error trying to ALTER TABLE in an Access database

    Quote Originally Posted by Shaggy Hiker View Post
    True, but as Wes seemed to show, the word COLUMN appears to be optional, though I always include it.
    That seems to be the case, yes. I just tested directly in Access and it adds a column with or without COLUMN in the ADD statement.

  14. #14
    PowerPoster
    Join Date
    Nov 2017
    Posts
    2,895

    Re: Error trying to ALTER TABLE in an Access database

    OP, I apologize.

    So, perhaps it would be best to verify some of the obvious assumptions. Do you have a table named "Notes_file" in the database that you are running this query against?

    Edit: There is more information given by the OP on SO, including the actual VB.NET code used.

    https://stackoverflow.com/questions/...base-using-sql
    Last edited by OptionBase1; Sep 4th, 2023 at 03:17 PM.

  15. #15

    Thread Starter
    New Member
    Join Date
    Sep 2023
    Posts
    6

    Re: Error trying to ALTER TABLE in an Access database

    No problem and thanks for listening. Just now I pasted:

    ALTER TABLE Notes_file
    ADD COLUMN DateOfBirth date;

    in as a new query in Access and I am still getting that "Query must have at least one destination field." error. Is it because it is not a query?

    And thanks for the point to the VB code to both of you.

  16. #16
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,148

    Re: Error trying to ALTER TABLE in an Access database

    You need to post the relevant code. That one line doesn't tell us enough.

  17. #17
    PowerPoster
    Join Date
    Nov 2017
    Posts
    2,895

    Re: Error trying to ALTER TABLE in an Access database

    Quote Originally Posted by WMR1 View Post
    No problem and thanks for listening. Just now I pasted:

    ALTER TABLE Notes_file
    ADD COLUMN DateOfBirth date;

    in as a new query in Access and I am still getting that "Query must have at least one destination field." error. Is it because it is not a query?

    And thanks for the point to the VB code to both of you.
    No idea then. I ran your query under two problematic scenarios:

    1. The table didn't exist
    2. The field already existed

    Both generated errors, but different errors than what you are getting.

    Good luck.

  18. #18
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,148

    Re: Error trying to ALTER TABLE in an Access database

    After rereading your last post it sounds like you are trying to run this query using Access. So I created a new query in Access. I ran it with and without COLUMN, both ran fine and added the column.

    This is a .Net forum, it doesn't sound like your using .Net. But ChrisE provided you with a good .Net example to follow if you want to try.

    With the information provided there's not much else help I can provide.

  19. #19
    PowerPoster
    Join Date
    Nov 2017
    Posts
    2,895

    Re: Error trying to ALTER TABLE in an Access database

    Quote Originally Posted by wes4dbt View Post
    After rereading your last post it sounds like you are trying to run this query using Access. So I created a new query in Access. I ran it with and without COLUMN, both ran fine and added the column.

    This is a .Net forum, it doesn't sound like your using .Net. But ChrisE provided you with a good .Net example to follow if you want to try.

    With the information provided there's not much else help I can provide.
    I believe the OP was initially using VB.NET to run the query, but I think they may have decided to remove VB.NET from the equation and try running the query directly in Access as I did when I was testing it.

    At this point it probably should be moved to the Database forum or the Office forum, since the error persists outside of anything VB.NET related.

  20. #20
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,153

    Re: Error trying to ALTER TABLE in an Access database

    https://learn.microsoft.com/en-us/of...tination-field
    Workaround

    To work around this problem, use one of the following methods:

    • For each database that is affected by this problem, open the database in Access, click Options on the File menu, select Current Database, and then clear the Track name AutoCorrect info check box.
    • Make sure that each query is opened and saved while in you are in Design View.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  21. #21

    Thread Starter
    New Member
    Join Date
    Sep 2023
    Posts
    6

    Re: Error trying to ALTER TABLE in an Access database

    Thanks Zvoni; that did clear up the error I was getting, though I have no idea why unchecking Track name AutoCorrect would do that. On to the VB implementation!

  22. #22
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,153

    Re: Error trying to ALTER TABLE in an Access database

    Quote Originally Posted by WMR1 View Post
    Thanks Zvoni; that did clear up the error I was getting, though I have no idea why unchecking Track name AutoCorrect would do that. On to the VB implementation!
    https://support.microsoft.com/en-us/...4-a93c10a9d98b
    Track name AutoCorrect info

    When you enable name AutoCorrect to track name changes for a database, Access does the following:

    1. Attempts to gain exclusive access to the database.
    2. Searches for objects that are open.
    3. Prompts and closes open objects.
    4. Updates name maps by opening, saving, and closing all tables, queries, forms, and reports.
    5. Returns the database to whatever state it was in before it was promoted to Exclusive.
    At a guess:
    Your table is open somewhere,
    or you have a VIEW based on "SELECT * FROM Notes_file",
    or you have exclusive access to the Database, meaning access itself fails to claim exclusive access (see point 1)
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width