Solved

MS Access/VBA Add Field to Form

Posted on 2007-04-04
6
722 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

628 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