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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add a TextBox to a Form Programically

Status
Not open for further replies.

EnS

Programmer
Oct 29, 2002
58
US
I'm not sure if this is the right place to submit this question, but I am unable to find anything in the FAQ's or archive threads.

Here is the situation. Say I have a form with one button on it (cmdAddTextBox) and say that when the user selects the button, the code within the OnClick Event programically creates, adds, and positions the textbox to the form. Also, say that there would be no limit, within reason, to the number of textboxes they may create. Say also that the Access 2000 database which contains this form uses ADO.

Is this possible?

Thanks in advance. ERM
 
sure, i don't see why this isn't possible, although the placement might be a little awkward.

i'd suggest make function that has a structure that defines your textbox that is based on a location value that changes as each one is created so that they are directly below each other as the user clicks the button.

either that or an array of controls, and position them and make them visible as the user clicks the button. that's my guess.

hope it helps.

Cruz'n and Booz'n always.
This post shows what little I do at work.
 
hwkranger,

Thanks for the rapid reply. I've been working with this for quite some time now, and I havn't been able to find a function within Access 2k that will add a control to the form. I have found the CreateControl property of the Applicaiton Object, but I get an error informing me that I can not create a control at runtime.

If you have any examples, I would very much like to have a peek at them if you wouldn't mind.

Or, how would I go about creating a control array in access?

Thanks again for the help. ERM
 
Hi ERM!

Actually, your Access error message is quite correct. You can't create a control on a form unless it is in design view and, in design view, the form cannot be running code so you cannot add a control to a form using code in or initiated by that form. Your best option is to design a separate form on the fly then open it for the user.

hth
Jeff Bridgham
bridgham@purdue.edu
 
Ok, with that being said, let me ask this:

Is there a way to open a form in design view while it is hidden from the user? I am thinking about trying this: Create a form with the button on it (cmdAddTextBox) and a subform that will contrain the controls on them (sbfrmControlList). When the user clicks on the command button, I could then remove the RecordSource of the subform, or change is to reflect (""), open the subform in design view, hiden from the user, add the appropriate control, close and save the form, then finally reset subform RecordSource back to sbfrmControlList. I hope this makes sense, and that I'm not being too unrealistic.

Thanks for the advice. ERM
 
Hi!

I don't know if you can do that with a subform or not, but to open a form in design view and hidden you do this:

DoCmd.OpenForm "FormName", acDesign, , , , acHidden

Let us know if it works.

hth
Jeff Bridgham
bridgham@purdue.edu
 
Thanks, I give it a try and let you all know how it works out. ERM
 
F.Y.I.

Well, I think I cracked this one. First I created an unbound subform (ctrlSubForm) onto Form1 in design view using drag and drop from the tool bar. Second, I created two blank forms, one for the background (sbfrmBackGround) and one for the record entry rows (sbfrmRecEntry). Sorry for its length.

I entered the following code on to Form1 Class_Module:

********** Begin Code ********************
Option Compare Database

Dim row As Integer, rows As Integer

Private Sub Form_Open(Cancel As Integer)

ctrlRecEntry.SourceObject = "sbfrmBackGround"

End Sub

Private Sub cmdAddRec_Click()

'set subform to blank back ground
ctrlRecEntry.SourceObject = "sbfrmBackGround"

'open record subform in design view to create and format controls
DoCmd.OpenForm "sbfrmRecEntry", acDesign, , , , acHidden

'call row creation sub
'row and rows are predetermined by user
Call insertRecRow(row, rows)

'close and save record subform
DoCmd.Close acForm, "sbfrmRecEntry", acSaveYes

'reset subform source object to record subform
ctrlRecEntry.SourceObject = "sbfrmRecEntry"

End Sub

Private Sub insertRecRow(row As Integer, rows As Integer)

Dim top As Integer, a As Byte, txtbox As Control, strsql As String

'set sql for retreival of data for combo box
strsql = "SELECT * FROM OUTAGE_INFO ORDER BY OUTAGE_DESC"

'create unbound text and combo boxes for record entry rows
top = row * 240

For a = row To rows
'create and format *a* number of rows
Set txtbox = CreateControl("sbfrmRecEntry", acTextBox, acDetail, "", "", 45, top, 620, 240)
With txtbox
.Name = "txtFC" & a
.TextAlign = 2
.Format = "###0"
End With
Set txtbox = CreateControl("sbfrmRecEntry", acTextBox, acDetail, "", "", 660, top, 620, 240)
With txtbox
.Name = "txtATM" & a
.TextAlign = 2
.Format = "###0"
End With
Set txtbox = CreateControl("sbfrmRecEntry", acTextBox, acDetail, "", "", 1260, top, 840, 240)
With txtbox
.Name = "txtWorkofDate" & a
.TextAlign = 2
.Format = "mm/dd/yyyy"
End With
Set txtbox = CreateControl("sbfrmRecEntry", acTextBox, acDetail, "", "", 2100, top, 840, 240)
With txtbox
.Name = "txtResolvedDate" & a
.TextAlign = 2
.Format = "mm/dd/yyyy"
End With
Set txtbox = CreateControl("sbfrmRecEntry", acComboBox, acDetail, "", "", 2940, top, 1080, 240)
With txtbox
.Name = "cboOutage" & a
.RowSourceType = "Value List"
'populateList(variable as string) - external function to populate combo box value list
.RowSource = populateList(strsql)
.BoundColumn = 1
.ColumnCount = 2
.ColumnWidths = "0;1080"
End With
Set txtbox = CreateControl("sbfrmRecEntry", acTextBox, acDetail, "", "", 4042, top, 840, 240)
With txtbox
.Name = "txtResearchItem1" & a
.TextAlign = 2
End With
Set txtbox = CreateControl("sbfrmRecEntry", acTextBox, acDetail, "", "", 4860, top, 840, 240)
With txtbox
.Name = "txtResearchItem2" & a
.TextAlign = 2
End With
Set txtbox = CreateControl("sbfrmRecEntry", acTextBox, acDetail, "", "", 5699, top, 840, 240)
With txtbox
.Name = "txtResearchItem3" & a
.TextAlign = 2
End With
Set txtbox = CreateControl("sbfrmRecEntry", acTextBox, acDetail, "", "", 6540, top, 840, 240)
With txtbox
.Name = "txtDocItem1" & a
.TextAlign = 2
End With
Set txtbox = CreateControl("sbfrmRecEntry", acTextBox, acDetail, "", "", 7380, top, 840, 240)
With txtbox
.Name = "txtDocItem2" & a
.TextAlign = 2
End With
Set txtbox = CreateControl("sbfrmRecEntry", acTextBox, acDetail, "", "", 8219, top, 840, 240)
With txtbox
.Name = "txtDocItem3" & a
.TextAlign = 2
End With
Set txtbox = CreateControl("sbfrmRecEntry", acTextBox, acDetail, "", "", 9060, top, 840, 240)
With txtbox
.Name = "txtDocItem4" & a
.TextAlign = 2
End With
Set txtbox = CreateControl("sbfrmRecEntry", acTextBox, acDetail, "", "", 9900, top, 840, 240)
With txtbox
.Name = "txtDocItem5" & a
.TextAlign = 2
End With
Set txtbox = CreateControl("sbfrmRecEntry", acTextBox, acDetail, "", "", 10739, top, 840, 240)
With txtbox
.Name = "txtScore" & a
.TextAlign = 2
.Format = "##0"
End With
'set next rows top position
top = top + 240
Next a


End Sub

********** End Code ********************
ERM
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top