Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    pnpez is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    18

    Error adding field in Access 2016 using VBA

    Hello, I am trying to run the code below to add a field to an existing table below:

    Public Sub addColumn()
    Dim strField As String
    Dim curDatabase As Object


    Dim ADHD As Object
    Dim fldNew As Object

    Set curDatabase = CurrentDb
    Set ADHD = curDatabase.TableDefs("ADHD")

    strField = "DrugType2"

    Set fldNew = ADHD.CreateField(strField)
    ADHD.Fields.Append fldNew
    End Sub


    I get the run-time error INVALID FIELD DATA TYPE on line "ADHD.FIELDS.APPEND FLDNEW"
    I got this code on line so could be outdated.
    Any help? Thanks.


  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,943
    Well I'd expect you'd have to say what datatype it is, which appears to be what the error message is indicating?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    pnpez is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    18
    I am new to this so wondering where I would put that code. It would be a TEXT type. Thanks

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,943
    I have never done anything like this, except manually? Why can't you do it that way?. It is not something you are going to do every day, surely?, so no real need for a code solution, especially when you do not know how to do it?

    I'd expect it to be a property of the object, so perhaps FldNew.Datatype ?

    Put a break point in on the creation of FldNew then look in the locals window for it's properties.

    Edit: Looks like it would be the Type property, but you need to select the correct one. EG ID (autonumber) in one of my tables is 4, so look in an existing table like ADHD for a text field and see what number that is.?

    FldNew.Type = 4 (if you were making an autonumber field)

    Edit again: Text appears to be 10
    Last edited by Welshgasman; 05-25-2021 at 08:56 AM. Reason: added more info
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,943
    Yep, fldnew.type = 10 will set it to text, so put before the append line.
    You will probably need to specify a length the same way and any other restrictions?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Can you please try this:
    Code:
     CurrentDb.Execute "ALTER TABLE ADHD ADD COLUMN DrugType2 TEXT(255);" 'add field
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  7. #7
    pnpez is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    18
    Thanks to both of you. In both situations I get Run-time error 3211: Database engine could not lock table 'ADHD' because it's already in use by another person or process.

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,943
    Yes, it needs to be closed. You cannot even do it manually if the table is in use?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    pnpez is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    18
    Vlad, file was open, sorry. I did run your code but it produced no results.

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,943
    Quote Originally Posted by pnpez View Post
    Vlad, file was open, sorry. I did run your code but it produced no results.
    Works for me.?
    Did you check the table?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  11. #11
    pnpez is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    18
    I'm getting "cannot define field more than once" now on your code Welsh

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,943
    Well that would be correct? you cannot have two fieldnames the same in the same table :-(
    That seems ti infer that Vlad's code worked, which I would expect, so just change the name of the field to DrugType3
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  13. #13
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Can you show us the full code you are running?
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  14. #14
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,943
    I have to ask

    Why are you attempting such advanced topics, (well to me at least) when you appear to be a novice with Access.?

    I'm all for learning, but not by jumping in the deep end without learning to swim first?

    You have not said why you can't just do this manually.?

    I can see a use for this if you wanted to build the tables from scratch each time, but for day to day use?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  15. #15
    pnpez is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    18
    Thanks for asking...I'm building this to help a colleague who is doing this for several tables, so it's a pain completing each month. No better way to learn on my own.
    So both methods worked, YOU GUYS ROCK!
    Last thing is to populate field, this code says invalid syntax:

    CurrentDb.Execute "Update ADHD" & "Set DrugType2 = ADHD;"

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Access 2016 not enough memory error
    By Mediaweb in forum Access
    Replies: 3
    Last Post: 09-11-2020, 04:40 AM
  2. Replies: 3
    Last Post: 08-08-2018, 02:48 PM
  3. Error trying to write a record - MS Access 2016
    By Dave Lambert in forum Access
    Replies: 5
    Last Post: 07-18-2018, 07:16 AM
  4. Error Messages On Subforms, Access 2016
    By roxdrob in forum Forms
    Replies: 19
    Last Post: 10-07-2017, 11:40 PM
  5. ODBC connection for Access 2016 - results in error
    By Ashish_Panchal in forum Access
    Replies: 3
    Last Post: 11-25-2016, 01:35 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums