Change control type in VBA?

G

Guest

Is there a way to change a combo box to a text box in VBA code?

what I want to do is use the same form for viewing/editing records in edit
mode (in which case I want combo boxes for a couple of the fields in order to
assist the user in finding the record they want faster), and also in add new
mode (in which case I only want text boxes, since the user will be adding new
data in these fields and does not have the need to look anything up in a
combo or list box)

I tried using:

Private Sub Form_Open(Cancel As Integer)

If Me.AllowAdditions = True Then
Me.cboLabName.ControlType = acTextBox
End If

End Sub

But I got the following error message: "To set this property open form in
report or design view"

Is there a way to do something like this in code?

Any ideas greatly appreciated, thanks.
 
B

Brendan Reynolds

Like the message says, you have to open the form in design mode to change
control types. A better solution would be to add both the text boxes and
combo boxes to the form, and toggle their Visible property. Something like
....

If Me.AllowAdditions = True Then
Me!NameOfCombo.Visible = False
Me!NameOfTextBox.Visible = True
Else
Me!NameOfComboBox.Visible = True
Me.NameOfTextBox.Visible = False
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top