Results 1 to 5 of 5
  1. #1
    HarmLeastOne is offline Novice
    Windows 10 Access 2016
    Join Date
    Jun 2018
    Posts
    1

    Thumbs up .CreateField Method Naming Issues

    This is what I have so far with some extraneous code left out and some anonymous database and variable names:

    Dim dB as DAO.Database
    Dim tdf as DAO.TableDef
    Dim newField As DAO.Field

    Dim newFieldNameVar As String

    newFieldNameVar = "Variable Name"

    Set dB = CurrentDb



    Set tdf = dB.TableDefs("PreExistingTable")

    Set newField = tdf.CreateField(newFieldNameVar)

    tdf.Fields.Append newField

    Set newField = Nothing
    Set tdf = Nothing
    Set dB = Nothing

    Where the aforementioned code exists in my function is not in a loop but the newFieldNameVar is a product of a loop, and so changes regularly based on question thread irrelevant conditions.

    At the point the .Fields.Append line executes I get this run-time error: Invalid field data type. '3259' I am fairly new to programming in any form or fashion, but imagine that I might be using the wrong syntax for the variable used as the Name element of the .CreateField Method. Any ideas? Am I able to use a string variable as the Name in the .CreateField Method?

    Thanks for the help in advance!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Why are you trying to implement code that modifies db structure? I suspect db is not optimally normalized.

    Doesn't work with literal text either so variable is not issue.

    I set the field Type argument and code works for me.

    Please post code between CODE tags to retain indentation and readability and set it apart from rest of narrative.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,979
    You need to specify the datatype (dbText) and field size e.g. 50

    Code:
    Set newField = tdf.CreateField(newFieldNameVar, dbText,50)
    I've not tried it with a variable rather than a field name in quotes, but I can't see why it wouldn't work

    See this link on Allen Browne's website for many more DAO functions related to setting or modifying tables/fields/indexes/relationships using code http://allenbrowne.com/func-DAO.html

    @June7
    The code also works on linked tables which is very useful for updating backends in split databases
    I use this approach to modify backend databases when distributing frontend updates to clients without needing to visit their site
    e.g. add new tables, add/modify fields, add new indexes etc
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Colin, yes I am familiar with that usage. I actually have a procedure to do that (built by someone else although I modified it); however, I still prefer to modify tables directly, which rarely happens. If this is a regular event I suspect data structure is not optimally normalized.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,979
    In my case, updates are done about 4 times per year, usually to add new features or improve existing features.
    Nothing to do with normalisation issues
    Changes to backend tables are done to facilitate those types of change.
    For Access BE, I use DAO functions. For SQL Server tables, I use a script.
    As some of my large schools apps are used by multiple client schools in different locations, this approach is vital.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Issues with Worksheet.Intersect Method
    By Kluaoha in forum Programming
    Replies: 1
    Last Post: 01-19-2018, 09:56 AM
  2. Replies: 3
    Last Post: 07-26-2017, 08:14 AM
  3. .CreateField Mehtod used with a Variable
    By hockeyman9474 in forum Access
    Replies: 4
    Last Post: 01-05-2017, 07:03 PM
  4. Replies: 6
    Last Post: 06-25-2016, 02:56 PM
  5. Naming scheme thru VBA
    By redbull in forum Programming
    Replies: 10
    Last Post: 04-10-2012, 02:37 PM

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