Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you add a field to an existing form NOT a newly created form

Status
Not open for further replies.

chillwa

Programmer
Jun 16, 2008
8
US


How can I add a field to an existing form NOT a newly created form using VBA?

I currently have VBA code creating a new field in a table.

This table has an associated form.
I want this newly created table field to also appear in the form automatically through vba.


I tried to create a control to add to the form, but I keep getting an error message that says
"Run-time error '3799' Could not find Field 'Text14' "




My form is named "Table1" (for test purposes)
The field I am trying to add from my table is "field4"


I do not know why vba is looking for field "Text14", when I am adding "field4"



Here is my stripped down code:

Dim frm As Form
Dim ctrltext As Control
DoCmd.OpenForm "Table1", acDesign
Set ctrltext = CreateControl("Table1", acTextBox, acDetail, "", "field4")
DoCmd.Restore
DoCmd.Close
End Sub


The below code works with creating a NEW form, but does not work with an existing form

Sub NewControls()
Dim frm As Form
Dim ctlLabel As Control, ctlText As Control
Dim intDataX As Integer, intDataY As Integer
Dim intLabelX As Integer, intLabelY As Integer

' Create new form with Orders table as its record source.
Set frm = CreateForm
frm.RecordSource = "Orders"
' Set positioning values for new controls.
intLabelX = 100
intLabelY = 100
intDataX = 1000
intDataY = 100
' Create unbound default-size text box in detail section.

Set ctlText = CreateControl(frm.Name, acTextBox, , "", "", _
intDataX, intDataY)
' Create child label control for text box.
Set ctlLabel = CreateControl(frm.Name, acLabel, , _
ctlText.Name, "NewLabel", intLabelX, intLabelY)

' Restore form.
DoCmd.Restore
End Sub


Any help would be appreciated
 
If you wish to use an existing form, you must open it:

Code:
' Open form.
DoCmd.OpenForm "frmForm", acDesign
Set frm = Forms!frmForm

'Presumably you already have a recordsource
'so you don't need this:
'frm.RecordSource = "Orders"
 
Hi, I already opened the form in design mode, see above code,

but I keep getting the error message "Run-time error '3799' Could not find Field 'Text14' "

My code is the first code (the second code is just some generic code that works for new forms - it was an example from Microsoft)

Thanks,
 
Ok, got it. It is best to use [ignore]
Code:
[/ignore] tags.

What line is giving you this error? Your code works for me.
 
this line is giving me the code
Set ctrltext = CreateControl("Table1", acTextBox, acDetail, "", "field4")




I'm currently working on Access 2002, maybe I need to switch to Access 2003 and try it.


 
I suggest you try with a new test form and database.
 
Well, I tried it on a new computer with a new database in version 2003 and I get the same error message. This time I get the error message, "can't find field5"

Before I was getting, "can't find field13" then I got the error message "can't find field14" with my first application.


Oh well, thanks for helping

 
Please try the code on a fresh form, no controls, no record source and see if you still get an error.
 
Well, I finally got it to work.

I took the code out of the form and added it to a module and it worked.

Here is the code I used




Sub NewControlstable1()

Dim frm As Form
Dim ctlLabel As Control, ctlText As Control
Dim intDataX As Integer, intDataY As Integer
Dim intLabelX As Integer, intLabelY As Integer

DoCmd.OpenForm "Table1", acDesign


' Set positioning values for new controls.

intLabelX = 100
intLabelY = 100
intDataX = 1000
intDataY = 100

' Create unbound default-size text box in detail
section.

Set ctlText = CreateControl("Table1",
acTextBox, , "Table1", "Field4", _
intDataX, intDataY)


' Create child label control for text box.

Set ctlLabel = CreateControl("Table1", acLabel, , _
ctlText.Name, "Field4", intLabelX, intLabelY)


' Restore form.
DoCmd.Restore

End Sub
 
I forgot to say, I also think the problem may have been that I did not specify the form name as the parent for the new textbox control.

I changed two things at once, so I really didn't isolate the problem.

But it works
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top