Results 1 to 4 of 4

Thread: Automatically creating new fields in Access using Macros

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2008
    Posts
    1

    Automatically creating new fields in Access using Macros

    Hi,

    I am relatively new to Access so play nice...

    I have a requirement to import regular data into Access and then do a series of queries on the data. I have got quite a long way with it but I am stuck on the following;

    I need to add some fields to a table (PageExtent, Runcode and MailsortTrigger) and then populate these fields with data. I can't find any other way apart from manually adding the fields to the table first then running the update queries as individual queries. Is there a better way of doing this?

    Thanks in advance...

    Richard

    UPDATE table SET PageExtent = "E18";

    UPDATE table SET RunCode = "testR";

    UPDATE table SET table.MailsortTrigger = "0";

  2. #2
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Automatically creating new fields in Access using Macros

    Hi. Welcome to the forum.

    You can create a Data Definition query:
    Code:
    ALTER TABLE theTable 
    ADD PageExtent TEXT(20), 
      RunCode TEXT(20), 
      MailSortTrigger SMALLINT;
    and populate it in one update query like this:
    Code:
    UPDATE theTable 
    SET PageExtent = "E18", RunCode = "testR", MailSortTrigger = 0 
    WHERE theKey = "something";
    I'm not sure why you need to add columns dynamically at run-time though. That doesn't necessarily sound like the best solution. Can you tell us any more about what you are trying to achieve?

  3. #3
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Automatically creating new fields in Access using Macros

    DDL have implicit COMMIT, it's not advisable to mix DML and DDL.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Automatically creating new fields in Access using Macros

    If its only the 3 fields then it would be better to manually create them as you dont want it to run in a macro possibly a second time etc.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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