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

Tab Control On change event does not execute code to disable textbox 1

Status
Not open for further replies.

polnichek

Technical User
Dec 13, 2005
35
0
0
CA
I have a Main Form called frmPurchaseRequest that has a tab control (TabCtl0) on it with three tabs. Each tab uses the same sub form to capture data entry (context oriented each tab represents a different way of acquiring goods and services). the intention is to have a client click on a tab and the sub form would display different text boxes and controls depending on the tab opened. Unfortunately I cant get the ON Change event to enable or disable the controls properly. At the moment if you click on the desired tab in this case HC1000 Purchase Request (pagHc1000) it does nothing, but when you click on the previous tab LPO-CreditCard (pagLPO) (it disables the textbox (txtPO_Number) I wanted it to disable when you click on HC1000 Purchase Request.

'Code to execute:
Private Sub TabCtl0_Change()

Dim intCtrlVal As Integer
intCtrlVal = Me.TabCtl0.Value


'intCtrlVal = 3
Select Case intCtrlVal
Case 0
'code to execute if LPO-Credit (pagLPO) Card is selected
'this executes properly
MsgBox " you have selected LPO Credit Card"


Case 1

MsgBox " you have HC 1000 Purchase Request"
'code to execute if HC1000 (pagHc1000) is selected

'below does not in the app this code is all one line
Forms![frmPurchaseRequest]! [frmPurchReq_Sub].Form!txtPO_Number.Enabled = False

Case 2

'executes properly
MsgBox " you have selected CFA"
'code to execute if CFA is selected

Case Else
MsgBox "There has been an error!"

End Select

End Sub

'End of code to execute

I can post the database file if necessary


Polnichek
 
Here is the issue. You have actually got into a more advance topic of "multiple instances of the same form.

You have two subform controls, one on each page. The subfrm control houses the subform. It is actually the source object of the subform control that is the subform. The source object for each subform control is frmPurchReq_Sub.

Since you have two subform controls, both with the same source object this creates two instances of the the form frmPurchReq_Sub.

To make this a whole lot easier give the subform controls clear names.

I called them
subFrmCtlLPO
subFrmCtlHC100

Now in code you can modify each form, and you do not even have to worry about events because each one can be modified and treated independantly.

so in the on open event of the form.

dim subFrmLPO as access.form
dim subFrmHC100 as access.form

set subFrmLPO = Forms![frmPurchaseRequest]![subFrmCtlLPO].form
set subFrmHC100 = Forms![frmPurchaseRequest]![subFrmCtlLPO].form

with SubFrmLPO
'can set any properties here
end with
with SubFrmHC100
'can set any properties here
.txtPO_Number.Enabled = false
.txtPO_Number.visible = false
end with

The problem you were having is that you where in fact changing the properties of the form on the first tab when moving to the second tab.
your code was this
Forms![frmPurchaseRequest]![frmPurchReq_Sub].Form!txtPO_Number.Enabled
but the sub form control on the second tab had the name child4
should have been
Forms![frmPurchaseRequest]![Child4].Form!txtPO_Number.Enabled
 
BTW this answers a common question. Why you cannot refer to a subform just by its name, instead you have to go through the subform control?

This is exactly why, because what would happen if you have two instances of the same form like you have. They would have the same name and there would be no way to distinguish which one you are referring to.

Further this has to do with object oriented programming and design. You in fact can open multiple instances of any form at the same time and show different properties for each of the instances. Rarely used, but can be extremely useful.
 
Here is an example. I added some more formatting just to show that you can treat each as a seperate form. If you put a third subform control on the third tab then give that subform control a good name, and if you reference it through the subform control you can give that it own properties.
 
FYI. So if you have multiple instances of a subform and you want to have different code based on which instance is selected, then the code is something like this using an object comparison ([object] is [some other object]):

if me is Forms![frmPurchaseRequest]![subFrmCtlHC100].Form then
do something
elseif me is Forms![frmPurchaseRequest]![subFrmCtlHCLPO].Form then
do something else
end if
 
hI MajP,

thank you for your post. I feel rather silly, I should have known better. You are right I was attempting to open multiple instances of the same form at once and modify properties of the controls on that form. I like your first suggestion I am assuming the code you are suggesting goes in the load event of the main form (frmPurchaseRequest)? Finally just to be clear, I need two separate sub forms with distinct names?

Thank you again for all of your help.



Polnichek
 
You are right I was attempting to open multiple instances of the same form at once and modify properties of the controls on that form.
You can do this, but each one is its own instance so you just have to refer to the correct instance. You have to reference each one through its own subform control. You were just referring the first subform when you meant the second, because you were using the name of the first subform control.

Lets be very clear on terminology, although most people are not.

Sub Form Control: The control placed on the form in which a "subform" goes
Sub Form: The form that goes in a subform control. This is done by setting the source object of a subform control.

(Access makes this a little confusing because it defaults the name of the subform control to the name of the source object, but they are two things)


If you place two or more subform controls on a form they can have the same source object (Form). When you open the main form each "subform" within the subform controls is actually an "instance" of the same form. You can control each instance independantly. However you have to refer to each one correctly through its subform control

forms!MainFormName.subFormControlName.form

You do not have to create two different Forms to use as the source object (That is probably what you mean by "subform"). Each one is a different instance and you can modify/control the correct instance if you refer to it correctly.

Now most people would probably make two different source objects, and this is fine. I like your original approach better because i can manage just one form and tweak it at runtime. Configuration management is easier; however, conceptually it is more advanced.

When you are in design view if you click on the very outside of the "subform" you are selecting the actual subform control. If you click inside of that control you will get the form (source object).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top