Solved

MS Access/VBA Add Field to Form

Posted on 2007-04-04
6
710 Views
Last Modified: 2013-11-27
I'm trying to write a VBA program to add a new field to a MS Access database table, then to add the field as a text box on a form.  I've figured out how to create the field with the code below, but is it possible to programatically add the field to a form?


Set db = OpenDatabase(sFileName)
Set tdf = db.TableDefs("theTable")
Set fld = tdf.CreateField("NewFieldName", dbText, 255)
tdf.Fields.Append fld
0
Comment
Question by:cfg1980
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:TheSloath
ID: 18850112
Use the CreateControl procedure
0
 
LVL 61

Expert Comment

by:mbizup
ID: 18850982
>programatically add the field to a form?

Yes... but you can't do this very dynamically (you can't do this while viewing your form in normal view).  You can, through code open a form in design view, add the control, save, and reopen the form in normal view:

Add the following function to a module:

Function addTextBox(strFieldName as string, strFormName as string, strTBName)
    Dim ctl As Control
    DoCmd.OpenForm  strFormName, acDesign, , , , acHidden
    Set ctl = CreateControl(strFormName, acTextBox, acDetail)
    ctl.name = strTBName
    ctl.ControlSource = strFieldName
    Set ctl = Nothing
    DoCmd.Close acForm, strFormName, acSaveYes
    DoCmd.OpenForm strFormName, acNormal, , , , acWindowNormal
   
End Function

Add the function call to your code like this:
....
tdf.Fields.Append fld
AddTextBox fld.Name, "frmYourFormName", "txtYourNewTextBoxName"
0
 

Author Comment

by:cfg1980
ID: 18851996
That works great....except I need it to update a form that is not in the same database.  Essentially I'm trying to build an "updater" file for our users.  They would open my updater file and it would add the field to the form on their Access DB.

So is there a "DoCmd.OpenForm" equivalent that would open the form for editing in another DB?
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 61

Accepted Solution

by:
mbizup earned 250 total points
ID: 18852235
Use an access object for the other database and add the databasepath to the parameter list:

Function addTextBox(strFieldName as string, strDatabasePath, strFormName as string, strTBName)
Set objAccess = CreateObject("Access.Application")
objAccess.OpenCurrentDatabase strDatabasePath
objAccess.Visible = True

    Dim ctl As Control
    objAccess.DoCmd.OpenForm  strFormName, acDesign, , , , acHidden
    Set ctl = objAccess.CreateControl(strFormName, acTextBox, acDetail)
    ctl.name = strTBName
    ctl.ControlSource = strFieldName
    objAccess.DoCmd.Close acForm, strFormName, acSaveYes
    objAccess.DoCmd.OpenForm strFormName, acNormal, , , , acWindowNormal

    Set ctl = Nothing
    Set objAccess = Nothing
   
End Function

Then revise the call to the function accordingly:
AddTextBox fld.Name, "C:\dbapps\YourDatabase.mdb", "frmYourFormName", "txtYourNewTextBoxName"

0
 

Author Comment

by:cfg1980
ID: 18852940
That did the trick.  Thanks for your help!
0
 
LVL 61

Expert Comment

by:mbizup
ID: 18852949
Glad to help :-)
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question