Solved

MS Access/VBA Add Field to Form

Posted on 2007-04-04
6
685 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
  • 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Suggested Solutions

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now