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!

Disable fields based on compound condition 3

Status
Not open for further replies.

aaabuhalime

Instructor
Nov 22, 2012
67
US
Hi,
I have form includes students information, I am trying to disable some fields based on each selection, for example if I select student some fields get greyed out based on one condtion I did that ,I am trying to to add an other condtion to become combined condtion, I need some help to do so, here is what I have

Code:
Select Case Me.WhoIsIt 
Case "Student"
   Wt1.Enabled = True
else
     W1.Enabled = True

End Sub
this code goes behind whoIsIt field, I want to add an other condition status, so when the WhoIsIt is "Student", and Status"Active" I want o disable the fields, and where the code should go behind Status or Who isit after update any thought on how to do that ?

Thanks
 
You can nest Select Case statements and mix in with If... End If. You don't disable fields, but might disable controls.

What have you tried? Do you have some pseudo code?

Duane
Hook'D on Access
MS Access MVP
 
code should go behind Status or Who isit after update any thought on how to do that

Not exactly sure what you are asking, but I will take a guess. Often you will want multiple events to call the same procedure, so normally I do not put code in an event procedure instead I build a stand alone procedure. The event procedure simply calls the stand alone procedure. Multiple event procedures could call the same procedure and possibly pass in a different parameter. This keeps you from writing the same code in multiple procedures.

Example: Two event procedures calling the same procedure

private sub someControl_someEvent()
call standalone(me.someControl)
end sub

private sub someOtherControl_someOtherEvent()
call standalone(me.someOtherControl)
end sub

private sub standAlone(Ctrl as Access.control)
'do a bunch of code
msgbox Ctrl.name
end sub
 
Thank you all for your replies,
Dhookom,
I am trying to follow what you suggested, please correct me if I am wrong,"You can nest Select Case statements and mix in with If... End If. You don't disable fields, but might disable controls"

Code:
Select Case Me.WhoIsIt And Me.Status
Case "Student" And "active"
   Wt1.Enabled = True
else
     W1.Enabled = True

End Sub

is this correct?
 
That's not the syntax I was suggesting. Please provide your logic. Apparently you have two controls. What exact conditions make either one enabled and/or disabled.

Duane
Hook'D on Access
MS Access MVP
 
How about...
Code:
Select Case Me.WhoIsIt
    Case "Student"
        If Me.Status = "Active"
or
Code:
Select Case Me.WhoIsIt
    Case "Student"
        Select Case Me.Status
            Case "Active"

Randy
 
Thank you all very much for all your suggestions all of them are helpful, Randy700 you gave me exactly what I was looking for.
 
One more question please, I want to see how can I do the following, I want to be able to disable the Wt filed once a value entered in this field, so I want to continue using the following criteria,
Code:
Select Case Me.WhoIsIt
    Case "Student"
        Select Case Me.Status
            Case "Active"
 Wt1.Enabled = True
else
     Wt1.Enabled = True
I want also to check if there is a value entered in Wt1 field for that student, if yes then it disables Wt1 field , I am trying the following code in the before update behind Wt1 field
Code:
Private Wt1_BeforeUpdate(Cancel As Integer)
Dim Check As Variant
Check = DLookup("[Wt1]", "queryxx", "[Wt1] = '" & Me.Wt1 & "'")
If Not IsNull(Check) Then
Me.Wt1.Enabled=True
else
Me.Wt1.Enabled=False

End If
End Sub
I know I am missing something, I cant figure out how to link the three condition together, studentId, and Status, and if the value exists.

Any help will be appreciated.
 
I wouldn't do that in an event belonging to the control itself, I'd do it in an event belonging to the form, because the idea is that when the form is loaded, you want to disable certain controls based on the current data. That means I usually use the form's OnLoad event, but others here would know better the nuances of what happens when a form starts up.

I strongly agree with MajP, that it's better to write a separate sub to do all enabling of controls (private sub EnableControls) and call this from all events that need it, because it's likely you'll be enabling and disabling things after filling in data in several different controls.

Incidentally, I find it more readable to have a sub that first enables everything, and then selectively disables each control that's not needed, based on appropriate ifs or cases. I personally find this more obvious-at-a-glance than if-then-else's. I.e.:
Code:
If Not IsNull(Check) Then
Me.Wt1.Enabled=True
else
Me.Wt1.Enabled=False

End If

becomes

Code:
Me.Wt1.Enabled=True
If IsNull(Check) Then Me.Wt1.Enabled=False
(Is it just me, or do I rarely find myself using "me", as nearly always the form's code understands what belongs to it anyway??)
 
Even a more concise way:
Code:
If Not IsNull(Check) Then
Me.Wt1.Enabled=True
else
Me.Wt1.Enabled=False

End If
becomes
Code:
Me.Wt1.Enabled = Not IsNull(Check)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you all for your helpful suggestions, here what I have been doing for some reason it is not working, here is the scenario that I am trying to achieve, I want to check if there is a value entered in Wt1 field for that student, WhoIsIt is a student, if yes then Wt1 field get disabled
Code:
Private Wt1_BeforeUpdate(Cancel As Integer)
 If DLookup("[SID]", "qryAllxxs", "[Wt1]='" & Me!Wt1 & "' AND [SID]='" & Me!SID & "'") > 0 And Me.WhoIsIt = "Student" Then
           Wt1.Enabled = False
   Wt2.Enabled = False
   Wt3.Enabled = False

Else
        Wt1.Enabled = True
   Wt2.Enabled = True
   Wt3.Enabled = True

    End If
   
End Sub
, I am placing the previous code in the before update behind Wt1 field, but once I put there it disables all the fields after wt1, then I can't change/save anything, is this right place to put the code, what am I missing here.

Any thoughts will appreciated,
 
aaabuhalime, like I said before, no, this isn't the right place to put the code, in my view. I would write a separate sub that checks the values and disables controls appropriately, and I would call it in the form's OnLoad event, and in the afterUpdate event of any control which, when new data are entered, changes the situation. That way if you open the form already showing data from a student, the appropriate controls will be disabled. If you open the form blank, they will be enabled until some data have been typed in, and the data will become fixed as soon as you've done it (although personally I don't like things that prevent a user from back-tracking if they make a mistake). It must be possible to save the data in the form if it's linked to a table, provided you don't have a situation like a primary key that isn't an autonumber (and therefore requires you to set it explicitly to a unique value) and you don't actually provide any way to set it.

Tell me if I'm writing rubbish, someone!

By the way, PHV, yes, I very much like your approach and use it sometimes too. The only reasons I tend to use the longer version more frequently are (a) I'm sometimes setting a few properties that don't have logical values, for example I may change a colour of something, or change the source for a drop-down list of options; and (b) I'm frequently disabling several controls based on "Check", and find it useful to see them grouped.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top