Solved

MS Access/VBA Add Field to Form

Posted on 2007-04-04
6
693 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
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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