Display selected controls (1 Viewer)

Gismo

Registered User.
Local time
Today, 18:46
Joined
Jun 12, 2017
Messages
1,298
Hi All,

Please could assist in below code? I recon I have a conflict in the code as some controls should be displayed depending on previous selected data but in most cases it does not display. is there another way to white these codes?

Code:
Private Sub Form_Current()
If Nz(Me.ControlledItem, "") = "Military" And Nz(Me.Country, "") <> "ZA" And Nz(Me.ImportExport, "") = "2" Then
      Me.CP.Visible = True
      Me.EUC.Visible = True
      Me.EXP.Visible = True
      Me.IMP.Visible = False
      Me.TT.Visible = False
      Me.RC.Visible = False
      Me.CPAttachment.Visible = True
      Me.CPAttached.Visible = True
      Me.EUCAttachment.Visible = True
      Me.EUCAttached.Visible = True
      Me.EXPAttachment.Visible = True
      Me.EXPAttached.Visible = True
      Me.IMPAttachment.Visible = False
      Me.IMPAttached.Visible = False
      Me.TTAttachment.Visible = False
      Me.TTAttached.Visible = False
      Me.RCAttachment.Visible = False
      Me.RCAttached.Visible = False
      Me.CPL.Visible = True
      Me.EUCL.Visible = True
      Me.EXPL.Visible = True
      Me.IMPL.Visible = False
      Me.TTL.Visible = False
      Me.RCL.Visible = False
      Me.Refresh
    Else
      Me.CP.Visible = False
      Me.EUC.Visible = False
      Me.EXP.Visible = False
      Me.IMP.Visible = False
      Me.TT.Visible = False
      Me.RC.Visible = False
      Me.CPAttachment.Visible = False
      Me.EUCAttachment.Visible = False
      Me.EXPAttachment.Visible = False
      Me.IMPAttached.Visible = False
      Me.TTAttachment.Visible = False
      Me.RCAttachment.Visible = False
      Me.CPAttached.Visible = False
      Me.EUCAttached.Visible = False
      Me.EXPAttached.Visible = False
      Me.IMPAttachment.Visible = False
      Me.TTAttachment.Visible = False
      Me.RCAttachment.Visible = False
      Me.CPL.Visible = False
      Me.EUCL.Visible = False
      Me.EXPL.Visible = False
      Me.IMPL.Visible = False
      Me.TTL.Visible = False
      Me.RCL.Visible = False
      Me.Refresh
     End If
     
     If Nz(Me.ControlledItem, "") = "Dual" And Nz(Me.Country, "") <> "ZA" And Nz(Me.ImportExport, "") = "2" Then
     Me.CP.Visible = True
      Me.EUC.Visible = True
      Me.EXP.Visible = True
      Me.IMP.Visible = False
      Me.TT.Visible = False
      Me.RC.Visible = False
      Me.CPAttachment.Visible = True
      Me.CPAttached.Visible = True
      Me.EUCAttachment.Visible = True
      Me.EUCAttached.Visible = True
      Me.EXPAttachment.Visible = True
      Me.EXPAttached.Visible = True
      Me.IMPAttachment.Visible = False
      Me.IMPAttached.Visible = False
      Me.TTAttachment.Visible = False
      Me.TTAttached.Visible = False
      Me.RCAttachment.Visible = False
      Me.RCAttached.Visible = False
      Me.CPL.Visible = True
      Me.EUCL.Visible = True
      Me.EXPL.Visible = True
      Me.IMPL.Visible = False
      Me.TTL.Visible = False
      Me.RCL.Visible = False
      Me.Refresh
    Else
      Me.CP.Visible = False
      Me.EUC.Visible = False
      Me.EXP.Visible = False
      Me.IMP.Visible = False
      Me.TT.Visible = False
      Me.RC.Visible = False
      Me.CPAttachment.Visible = False
      Me.EUCAttachment.Visible = False
      Me.EXPAttachment.Visible = False
      Me.IMPAttached.Visible = False
      Me.TTAttachment.Visible = False
      Me.RCAttachment.Visible = False
      Me.CPAttached.Visible = False
      Me.EUCAttached.Visible = False
      Me.EXPAttached.Visible = False
      Me.IMPAttachment.Visible = False
      Me.TTAttachment.Visible = False
      Me.RCAttachment.Visible = False
      Me.CPL.Visible = False
      Me.EUCL.Visible = False
      Me.EXPL.Visible = False
      Me.IMPL.Visible = False
      Me.TTL.Visible = False
      Me.RCL.Visible = False
      Me.Refresh
     End If
     
     
     If Nz(Me.ControlledItem, "") = "Military" And Nz(Me.Country, "") <> "ZA" And Nz(Me.ImportExport, "") = "1" Then
      Me.CP.Visible = False
      Me.EUC.Visible = False
      Me.EXP.Visible = False
      Me.IMP.Visible = True
      Me.TT.Visible = False
      Me.RC.Visible = False
      Me.CPAttachment.Visible = False
      Me.CPAttached.Visible = False
      Me.EUCAttachment.Visible = False
      Me.EUCAttached.Visible = False
      Me.EXPAttachment.Visible = False
      Me.EXPAttached.Visible = False
      Me.IMPAttachment.Visible = True
      Me.IMPAttached.Visible = True
      Me.TTAttachment.Visible = False
      Me.TTAttached.Visible = False
      Me.RCAttachment.Visible = False
      Me.RCAttached.Visible = False
      Me.CPL.Visible = False
      Me.EUCL.Visible = False
      Me.EXPL.Visible = False
      Me.IMPL.Visible = True
      Me.TTL.Visible = False
      Me.RCL.Visible = False
      Me.Refresh
    Else
      Me.CP.Visible = False
      Me.EUC.Visible = False
      Me.EXP.Visible = False
      Me.IMP.Visible = False
      Me.TT.Visible = False
      Me.RC.Visible = False
      Me.CPAttachment.Visible = False
      Me.EUCAttachment.Visible = False
      Me.EXPAttachment.Visible = False
      Me.IMPAttached.Visible = False
      Me.TTAttachment.Visible = False
      Me.RCAttachment.Visible = False
      Me.CPAttached.Visible = False
      Me.EUCAttached.Visible = False
      Me.EXPAttached.Visible = False
      Me.IMPAttachment.Visible = False
      Me.TTAttachment.Visible = False
      Me.RCAttachment.Visible = False
      Me.CPL.Visible = False
      Me.EUCL.Visible = False
      Me.EXPL.Visible = False
      Me.IMPL.Visible = False
      Me.TTL.Visible = False
      Me.RCL.Visible = False
      Me.Refresh
    
     End If
     
     If Nz(Me.ControlledItem, "") = "Military" And Nz(Me.Country, "") <> "ZA" And Nz(Me.ImportExport, "") = "3" Then
      Me.CP.Visible = False
      Me.EUC.Visible = False
      Me.EXP.Visible = False
      Me.IMP.Visible = False
      Me.TT.Visible = True
      Me.RC.Visible = False
      Me.CPAttachment.Visible = False
      Me.CPAttached.Visible = False
      Me.EUCAttachment.Visible = False
      Me.EUCAttached.Visible = False
      Me.EXPAttachment.Visible = False
      Me.EXPAttached.Visible = False
      Me.IMPAttachment.Visible = False
      Me.IMPAttached.Visible = False
      Me.TTAttachment.Visible = True
      Me.TTAttached.Visible = True
      Me.RCAttachment.Visible = False
      Me.RCAttached.Visible = False
      Me.CPL.Visible = False
      Me.EUCL.Visible = False
      Me.EXPL.Visible = False
      Me.IMPL.Visible = False
      Me.TTL.Visible = True
      Me.RCL.Visible = False
      Me.Refresh
    Else
      Me.CP.Visible = False
      Me.EUC.Visible = False
      Me.EXP.Visible = False
      Me.IMP.Visible = False
      Me.TT.Visible = False
      Me.RC.Visible = False
      Me.CPAttachment.Visible = False
      Me.EUCAttachment.Visible = False
      Me.EXPAttachment.Visible = False
      Me.IMPAttached.Visible = False
      Me.TTAttachment.Visible = False
      Me.RCAttachment.Visible = False
      Me.CPAttached.Visible = False
      Me.EUCAttached.Visible = False
      Me.EXPAttached.Visible = False
      Me.IMPAttachment.Visible = False
      Me.TTAttachment.Visible = False
      Me.RCAttachment.Visible = False
      Me.CPL.Visible = False
      Me.EUCL.Visible = False
      Me.EXPL.Visible = False
      Me.IMPL.Visible = False
      Me.TTL.Visible = False
      Me.RCL.Visible = False
      Me.Refresh
     End If
     
      If Nz(Me.ControlledItem, "") = "Dual" And Nz(Me.Country, "") <> "ZA" And Nz(Me.ImportExport, "") = "3" Then
    Me.CP.Visible = False
      Me.EUC.Visible = False
      Me.EXP.Visible = False
      Me.IMP.Visible = False
      Me.TT.Visible = True
      Me.RC.Visible = False
      Me.CPAttachment.Visible = False
      Me.CPAttached.Visible = False
      Me.EUCAttachment.Visible = False
      Me.EUCAttached.Visible = False
      Me.EXPAttachment.Visible = False
      Me.EXPAttached.Visible = False
      Me.IMPAttachment.Visible = False
      Me.IMPAttached.Visible = False
      Me.TTAttachment.Visible = True
      Me.TTAttached.Visible = True
      Me.RCAttachment.Visible = False
      Me.RCAttached.Visible = False
      Me.CPL.Visible = False
      Me.EUCL.Visible = False
      Me.EXPL.Visible = False
      Me.IMPL.Visible = False
      Me.TTL.Visible = True
      Me.RCL.Visible = False
      Me.Refresh
    Else
      Me.CP.Visible = False
      Me.EUC.Visible = False
      Me.EXP.Visible = False
      Me.IMP.Visible = False
      Me.TT.Visible = False
      Me.RC.Visible = False
      Me.CPAttachment.Visible = False
      Me.EUCAttachment.Visible = False
      Me.EXPAttachment.Visible = False
      Me.IMPAttached.Visible = False
      Me.TTAttachment.Visible = False
      Me.RCAttachment.Visible = False
      Me.CPAttached.Visible = False
      Me.EUCAttached.Visible = False
      Me.EXPAttached.Visible = False
      Me.IMPAttachment.Visible = False
      Me.TTAttachment.Visible = False
      Me.RCAttachment.Visible = False
      Me.CPL.Visible = False
      Me.EUCL.Visible = False
      Me.EXPL.Visible = False
      Me.IMPL.Visible = False
      Me.TTL.Visible = False
      Me.RCL.Visible = False
      Me.Refresh
     End If
     
     If Nz(Me.ControlledItem, "") <> "Civil" And Nz(Me.Country, "") = "ZA" Then
    Me.CP.Visible = False
      Me.EUC.Visible = False
      Me.EXP.Visible = False
      Me.IMP.Visible = False
      Me.TT.Visible = False
      Me.RC.Visible = True
      Me.CPAttachment.Visible = False
      Me.CPAttached.Visible = False
      Me.EUCAttachment.Visible = False
      Me.EUCAttached.Visible = False
      Me.EXPAttachment.Visible = False
      Me.EXPAttached.Visible = False
      Me.IMPAttachment.Visible = False
      Me.IMPAttached.Visible = False
      Me.TTAttachment.Visible = False
      Me.TTAttached.Visible = False
      Me.RCAttachment.Visible = True
      Me.RCAttached.Visible = True
      Me.CPL.Visible = False
      Me.EUCL.Visible = False
      Me.EXPL.Visible = False
      Me.IMPL.Visible = False
      Me.TTL.Visible = False
      Me.RCL.Visible = True
      Me.Refresh
    Else
      Me.CP.Visible = False
      Me.EUC.Visible = False
      Me.EXP.Visible = False
      Me.IMP.Visible = False
      Me.TT.Visible = False
      Me.RC.Visible = False
      Me.CPAttachment.Visible = False
      Me.EUCAttachment.Visible = False
      Me.EXPAttachment.Visible = False
      Me.IMPAttached.Visible = False
      Me.TTAttachment.Visible = False
      Me.RCAttachment.Visible = False
      Me.CPAttached.Visible = False
      Me.EUCAttached.Visible = False
      Me.EXPAttached.Visible = False
      Me.IMPAttachment.Visible = False
      Me.TTAttachment.Visible = False
      Me.RCAttachment.Visible = False
      Me.CPL.Visible = False
      Me.EUCL.Visible = False
      Me.EXPL.Visible = False
      Me.IMPL.Visible = False
      Me.TTL.Visible = False
      Me.RCL.Visible = False
      Me.Refresh
     End If
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:46
Joined
May 7, 2009
Messages
19,247
what datatype is ImportExport, numeric?
if it is you can use:

... And Trim(Me.ImportExport & "") = "3" Then
 

Gismo

Registered User.
Local time
Today, 18:46
Joined
Jun 12, 2017
Messages
1,298
what datatype is ImportExport, numeric?
if it is you can use:

... And Trim(Me.ImportExport & "") = "3" Then

Import/Export is numeric but nut sure if that will assist in the conflict
 

Gismo

Registered User.
Local time
Today, 18:46
Joined
Jun 12, 2017
Messages
1,298

isladogs

MVP / VIP
Local time
Today, 16:46
Joined
Jan 14, 2017
Messages
18,242
No it's very simple.
If you have 20 controls all with the same tag value "A" (set in the property sheet), then one line of code will make all of them visible.
Code:
ShowControls True, "A"

Similarly
Code:
ShowControls False, "X"
will hide all controls with tag X

Much quicker than what you do now.
 

Gismo

Registered User.
Local time
Today, 18:46
Joined
Jun 12, 2017
Messages
1,298
thank you, will give it a shot
 

missinglinq

AWF VIP
Local time
Today, 11:46
Joined
Jun 20, 2003
Messages
6,423
Using the Tag properties, as has been suggested, is absolutely the way to go, with this.

But FYI, using the Nz() function, as you are, in your If clauses, has no use! Every time you use it is in this fashion

Nz(Me.ControlledItem, "") = "Military" and Nz(Me.Country, "") <> "ZA"

and so forth. There's no reason not to simply use

Me.ControlledItem = "Military" and Me.Country <> "ZA"

You use Nz() in cases where having a Control that is Null (empty) can cause problems, and that will never be a problem, here.

If Me.ControlledItem is Null, comparing it to "Military" will return False...because Null is not equal to "Military"


Likewise, if Me.Country is Null, testing that it is not equal to "ZA" will return True...because Null is not equal to "ZA".

Linq ;0)>
 

Gismo

Registered User.
Local time
Today, 18:46
Joined
Jun 12, 2017
Messages
1,298
No it's very simple.
If you have 20 controls all with the same tag value "A" (set in the property sheet), then one line of code will make all of them visible.
Code:
ShowControls True, "A"

Similarly
Code:
ShowControls False, "X"
will hide all controls with tag X

Much quicker than what you do now.

I looked at you sample database, just not sure if I will be able to accomplish this.

attached is my table, I recon I have too many fields to tie back too. as well as I am not familiar with the tag function. any other suggestions would be appreciated

Capture.JPG
 

Gismo

Registered User.
Local time
Today, 18:46
Joined
Jun 12, 2017
Messages
1,298
just another quick question on hiding controls. say I have 6 controls on a tab, when one or two is hidden, is it possible to display the controls without any gaps / spaces between hidden controls?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:46
Joined
Feb 19, 2002
Messages
43,314
Another alternative is using tab pages to group fields and making a tab visible/invisible

However, your table does not appear to be properly normalized. Normalizing the table so that rather than having columns of Y/N fields, you have a child table and the table has only rows for the applicable attributes.
 

Gismo

Registered User.
Local time
Today, 18:46
Joined
Jun 12, 2017
Messages
1,298
I thought about the tab option as you mentioned, but that would mean that i would have 12 tabs as these are the possibilities from the table.

if only I understood the tag function, then also i dont think it will work in this setup as I dont really have a uniqe referance to tie back to as you saw in the table
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:46
Joined
Feb 19, 2002
Messages
43,314
Then, look into normalizing the table. You will end up with a main form and a subform and you won't need any code to manage showing/hiding controls because the subform will have only rows for the attributes that you need for each type of parent record.
 

isladogs

MVP / VIP
Local time
Today, 16:46
Joined
Jan 14, 2017
Messages
18,242
Gismo
Having just looked at your screenshot, I also think restructuring your table(s) would be a good idea.

However, you indicated you didn't understand the use of tag.
Tag isn't a function. It is a property of controls in forms and reports in the same sense that fore colour and font size are properties which can be set from the property sheet
I suggest you look at the example I provided to see how it can be used with groups of controls. If you have specific questions, ask.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:46
Joined
May 7, 2009
Messages
19,247
missinglinq, that is why the OP add NZ() there, so he can test it to some text.

you can't use Tag on this situation because there are Conditions to be met and the conditions are not Static.
 

isladogs

MVP / VIP
Local time
Today, 16:46
Joined
Jan 14, 2017
Messages
18,242
missinglinq, that is why the OP add NZ() there, so he can test it to some text.

you can't use Tag on this situation because there are Conditions to be met and the conditions are not Static.

Yes of course he can use the tag property.
In this case, you just use it in each part of the If..Else... or in Select Case statements
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:46
Joined
May 7, 2009
Messages
19,247
if you would give an example based on the ops code.
but i think not needed, you're just making extra code to include the Tag.
 

isladogs

MVP / VIP
Local time
Today, 16:46
Joined
Jan 14, 2017
Messages
18,242
@arnelgp
As requested in post #18, here's an example based on the OP's code from post #1 .... before it is normalised. Of course, the table needs normalising BEFORE using the tag property as suggested

First group the controls according to common requirements.
I think 4 groups are needed
I believe the following works, but I did it quickly so there may be errors.
The idea is what I'm trying to get over here

Code:
Tag "A"
CP, EUC, EXP, CPAttachment, CPAttached, EUCAttachment, EUCAttached,
EXPAttachment, EXPAttached, CPL, EUCL, EXPL

Tag "IMP"
IMP, IMPAttachment, IMPAttached, IMPL 

Tag "TT"
TT, TTAttachment, TTAttached, TTL

Tag "RC"
RC, RCAttachment, RCAttached, RCL


Now using my ShowControls function, code could be instantly reduced to this:

Code:
Private Sub Form_Current()

    If Nz(Me.ControlledItem, "") = "Military" And Nz(Me.Country, "") <> "ZA" And Nz(Me.ImportExport, "") = "2" Then
	ShowControls True, "A"
	ShowControls False, "IMP", "TT", "RC"
      	Me.Refresh
    Else
	ShowControls False, "A", "IMP", C", "RC"
      	Me.Refresh
    End If
     
    If Nz(Me.ControlledItem, "") = "Dual" And Nz(Me.Country, "") <> "ZA" And Nz(Me.ImportExport, "") = "2" Then
	ShowControls True, "A"
	ShowControls False, "IMP", "TT", "RC"
      	Me.Refresh
    Else
	ShowControls False, "A", "IMP", "TT", "RC"
      	Me.Refresh
     End If     
     
    If Nz(Me.ControlledItem, "") = "Military" And Nz(Me.Country, "") <> "ZA" And Nz(Me.ImportExport, "") = "1" Then
	ShowControls True, "IMP"
	ShowControls False, "A", "TT", "RC"
      	Me.Refresh
    Else
      	ShowControls False, "A", "IMP", "TT", "RC"
      	Me.Refresh
    End If
     
    If Nz(Me.ControlledItem, "") = "Military" And Nz(Me.Country, "") <> "ZA" And Nz(Me.ImportExport, "") = "3" Then
	ShowControls True, "TT"
	ShowControls False, "A", "IMP", "RC"  
      	Me.Refresh
    Else
	ShowControls False, "A", "IMP", "TT", "RC"
      	Me.Refresh
     End If
     
      If Nz(Me.ControlledItem, "") = "Dual" And Nz(Me.Country, "") <> "ZA" And Nz(Me.ImportExport, "") = "3" Then
	ShowControls True, "TT"
	ShowControls False, "A", "IMP", "RC"
   	Me.Refresh
    Else
      ShowControls False, "A", "IMP", "TT", "RC"
      Me.Refresh
     End If
     
     If Nz(Me.ControlledItem, "") <> "Civil" And Nz(Me.Country,"") = "ZA" Then
	ShowControls True, "RC"
	ShowControls False, "A", "IMP", "TT"	
      	Me.Refresh
    Else
      	ShowControls False, "A", "IMP", "TT", "RC"
      	Me.Refresh
    End If

End Sub

Which in my view is not 'making extra code to include the Tag'
But the above code is far from optimised. This is better:

Code:
Private Sub Form_Current()
     'set all controls hidden initially
	ShowControls False, "A", "IMP", C", "RC"

    	If Nz(Me.ControlledItem, "") = "Military" And Nz(Me.Country, "") <> "ZA" And Nz(Me.ImportExport, "") = "2" Then ShowControls True, "A"
     
    	If Nz(Me.ControlledItem, "") = "Dual" And Nz(Me.Country, "") <> "ZA" And Nz(Me.ImportExport, "") = "2" Then ShowControls True, "A"
     
    	If Nz(Me.ControlledItem, "") = "Military" And Nz(Me.Country, "") <> "ZA" And Nz(Me.ImportExport, "") = "1" Then ShowControls True, "IMP"
     
    	If Nz(Me.ControlledItem, "") = "Military" And Nz(Me.Country, "") <> "ZA" And Nz(Me.ImportExport, "") = "3" Then ShowControls True, "TT"
     
      	If Nz(Me.ControlledItem, "") = "Dual" And Nz(Me.Country, "") <> "ZA" And Nz(Me.ImportExport, "") = "3" Then ShowControls True, "TT"
     
     	If Nz(Me.ControlledItem, "") <> "Civil" And Nz(Me.Country, "") = "ZA" Then ShowControls True, "RC"

    	Me.Refresh

End Sub

And then of course there are several unnecessary Nz functions, so

Code:
Private Sub Form_Current()
     'set all controls hidden initially
	ShowControls False, "A", "IMP", C", "RC"

    	If Me.ControlledItem = "Military" And Nz(Me.Country, "") <> "ZA" And Me.ImportExport = "2" Then ShowControls True, "A"
     
    	If Me.ControlledItem = "Dual" And Nz(Me.Country, "") <> "ZA" And Me.ImportExport = "2" Then ShowControls True, "A"
     
    	If Me.ControlledItem = "Military" And Nz(Me.Country, "") <> "ZA" And Me.ImportExport = "1" Then ShowControls True, "IMP"
     
    	If Me.ControlledItem = "Military" And Nz(Me.Country, "") <> "ZA" And Me.ImportExport = "3" Then ShowControls True, "TT"
     
      	If Me.ControlledItem = "Dual" And Nz(Me.Country, "") <> "ZA" And Me.ImportExport = "3" Then ShowControls True, "TT"
     
     	If Nz(Me.ControlledItem, "") <> "Civil" And Me.Country = "ZA" Then ShowControls True, "RC"

    	Me.Refresh

End Sub

Conditions 1 & 2 could be merged as could conditions 4 & 5.

Code:
Private Sub Form_Current()
     'set all controls hidden initially
	ShowControls False, "A", "IMP", C", "RC"

    	If (Me.ControlledItem = "Military" Or Me.ControlledItem = "Dual") And Nz(Me.Country, "") <> "ZA" And Me.ImportExport = "2" Then ShowControls True, "A"     
     
    	If Me.ControlledItem = "Military" And Nz(Me.Country, "") <> "ZA" And Me.ImportExport = "1" Then ShowControls True, "IMP"

	If (Me.ControlledItem = "Military" Or Me.ControlledItem = "Dual") And Nz(Me.Country, "") <> "ZA" And Me.ImportExport = "3" Then ShowControls True, "TT"   
     
     	If Nz(Me.ControlledItem, "") <> "Civil" And Me.Country = "ZA" Then ShowControls True, "RC"

    	Me.Refresh

End Sub

I might use Select Case statements to reduce this further but hopefully I've made the point by now
Looking at the new simplified structure, there appear to be combinations not covered in the code, but that's for the OP to worry about
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:46
Joined
Feb 19, 2002
Messages
43,314
Or rather than fixing the underlying problem, just throw code at it.
 

Users who are viewing this thread

Top Bottom