Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Forms Programming / January 2005

Tip: Looking for answers? Try searching our database.

Control with condition

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Frankie - 10 Jan 2005 22:01 GMT
I have a form with a subform : on the form there is a control (textbox) wich
value should depend upon  the checking of another control 's value that's
located on the subform (If the value is Null then value1otherwise value2). I
think the IIf function is the one to use but I can't get it to work.
I have also tried the If....Then...esle to no avail.
Can someone please help me with the best way to do it. ??
Version : Access 2002
Thanks in advance
AlCamp - 11 Jan 2005 04:40 GMT
Frankie,
  You should have shown your code, so we could see the elements involved.
  Use your own names...
=IIF(IsNull([Forms]![frmMainForm]![frmSubForm].Form![TextBox]),1,2)
hth
Al Camp

>I have a form with a subform : on the form there is a control (textbox)
>wich
[quoted text clipped - 6 lines]
> Version : Access 2002
> Thanks in advance
Penguin - 11 Jan 2005 05:22 GMT
The text box on the main forms Control Source should look like this:

=IIf([Forms]![FormName]![ControlName].[Form]![SubFormControlName] Is
Null,"ValueYouWant")

Hope this helps

>I have a form with a subform : on the form there is a control (textbox) wich
>value should depend upon  the checking of another control 's value that's
[quoted text clipped - 4 lines]
>Version : Access 2002
>Thanks in advance
Frankie - 11 Jan 2005 09:21 GMT
> The text box on the main forms Control Source should look like this:
>
[quoted text clipped - 13 lines]
>
> Thanks to both of you for your help.
I am still confused :
A-your solution seems to differ
B- When I write either one (as code) in the BeforeUpdate event  of the
textbox [Etat] of main form [F_Interimaires] I keep getting syntax error
Here is what  I put within Private Sub procedure:
Penguin proposal -IIf([Forms]![F_Interimaires]![Etat].[Form]![IDContrat]
IsNull,"Candidat","Interimaire")
Al proposal -  =IIF(IsNull([Forms]![F_Interimaires]![F_FicheContrat
sous-formulaire].Form![IDContrat),"Candidat","Interimaire")
Is there something I should add within  Private Sub  like  a Call function
and if so how do I write it ??
Also, I am based in Paris so please take the time difference into account  
when expecting an answer  from me .
Waiting to read from you soon
tina - 11 Jan 2005 10:00 GMT
actually, Al and Penguin gave you basically the same formula. in Penguin's
expression where he refers to

   [Forms]![FormName]![ControlName].

he's refering to the subform control, on the main form, that the subform
"sits inside of". it's always confusing to try to explain that in writing
(at least it is for me!  <g>).

Solution A
*if the textbox control on your main form is unbound (the ControlSource
property is blank)*, then you can put the IIf() function directly in the
ControlSource - which is what Al and Penguin assumed is the case. the
following should work, and automatically update when the value of the
control in the subform is changed, as

=IIF([Forms]![F_Interimaires]![F_FicheContrat
sous-formulaire].Form![IDContrat) Is Null,"Candidat","Interimaire")

note:  you have to use the name of the subform control (on the main form),
rather than the name of the subform itself - sometimes the two names are
identical, and sometimes they're not. to get the correct subform control
name, do the following:

1. open your main form in design view.
2. click once on the subform there in design view, to select it.
3. in the Properties box, Other tab, look at the Name property.
4. that's your subform control name.

Solution B
*if the textbox control on the main form is bound to a field in the form's
RecordSource*, and you're intending to save the result of the IF expression
into that field, then you would want to put the code into the AfterUpdate
event procedure of the control on the subform, as

   If IsNull(Me!IDContrat) Then
       Me.Parent!Etat = "Candidat"
   Else
       Me.Parent!Etat = "Interimaire"
   End If

hth

> > The text box on the main forms Control Source should look like this:
> >
[quoted text clipped - 27 lines]
> when expecting an answer  from me .
> Waiting to read from you soon
AlCamp - 11 Jan 2005 14:25 GMT
Frankie,
  You wrote...
Al proposal -  =IIF(IsNull([Forms]![F_Interimaires]![F_FicheContrat
sous-formulaire].Form![IDContrat),"Candidat","Interimaire")
   (You are missing a right bracket on [IDContrat].)

   I don't think Penguin's solution will work, I've never seen the IsNull
function expressed that way, and as far as I know, every IIF syntax must
denote a True and False...
   IIf(expr, truepart, falsepart)
   I haven't tested his solution... my apoplogies if I'm mistaken

   The IIF calculation I posted is meant to be the Control Source of an
unbound field on the main form.  Since IDContrat (null or not) always
determines Candidat or Interimaire, it is not necessary to save that
calculation in [Etat]... you just always "display" the calculation on the
main form.
   In other words, since you always have IDContrat, you can always
recalculate Etat "on the fly".

   Now, if you must save "Candidat" or "Interimaire" in [Etat] in your
table, then "bind" [Etat] to the Etat field, and use the AfterUpdate event
of  IDContrat itself with this code...

   If IsNull(IDContrat) Then
       [Forms]![F_Interimaires]![Etat] = "Candidat"
   Else
       [Forms]![F_Interimaires]![Etat] = "Interimaire"
   End If

will update the field with the correct value.

hth
Al Camp

>> The text box on the main forms Control Source should look like this:
>>
[quoted text clipped - 30 lines]
> when expecting an answer  from me .
> Waiting to read from you soon
Marshall Barton - 11 Jan 2005 17:30 GMT
>    I don't think Penguin's solution will work, I've never seen the IsNull
>function expressed that way, and as far as I know, every IIF syntax must
>denote a True and False...
>    IIf(expr, truepart, falsepart)
>    I haven't tested his solution... my apoplogies if I'm mistaken

FYI, The  xx Is Null syntax will work fine in SQL (where it
is standard, preferred, and more efficient then IsNull()),
in control source expressions and the Eval() function.
However that syntax is not recognized in the VBA environment
(where it would conflict with the IS operator).

Most of the time it doesn't make much difference, but it is
still important to keep the two separate and different
expression environments clear in your mind.

Another SQL operator that can be quite useful in a control
source expression is IN.  For example:

    =IIf(xx In(2,5,7), True, False)

Unfortunately, VBA never heard of IN so you'd have to use
several lines of code to achieve the same result (or wrap
the above in the Eval function).

Signature

Marsh
MVP [MS Access]

AlCamp - 12 Jan 2005 13:59 GMT
Marshall,
  Thanks for the clarification.
>>    I haven't tested his solution... my apoplogies if I'm mistaken.
  Glad I hedged my opinion!
Thanks
Al Camp

>>    I don't think Penguin's solution will work, I've never seen the IsNull
>>function expressed that way, and as far as I know, every IIF syntax must
[quoted text clipped - 20 lines]
> several lines of code to achieve the same result (or wrap
> the above in the Eval function).
Frankie - 11 Jan 2005 17:41 GMT
> Frankie,
>    You wrote...
[quoted text clipped - 67 lines]
>
> Since [Etat] is bound to a table  I opted  for  AfterUpdate Event  on  [IDContrat] on subform with your solution or Tina's.  The problem is that it still does not work  and  I really don't get it!!!
The subform is used on another form, could that make a difference ??
Thanks for your continuing support
tina - 12 Jan 2005 00:26 GMT
> The subform is used on another form, could that make a difference ??

do you mean the subform is used on another main form *in addition to* form
F_Interimaires? or another main form *instead of* form F_Interimaires?

please explain your setup in a little more depth.

> > Frankie,
> >    You wrote...
[quoted text clipped - 67 lines]
> >
> > Since [Etat] is bound to a table  I opted  for  AfterUpdate Event  on  [IDContrat] on subform with your solution or Tina's.  The problem is that it
still does not work  and  I really don't get it!!!
> The subform is used on another form, could that make a difference ??
> Thanks for your continuing support
Frankie - 12 Jan 2005 07:05 GMT
> > The subform is used on another form, could that make a difference ??
>
[quoted text clipped - 88 lines]
>
> Tina,
The subform [F_FicheContrat sous-formulaire] is used on 2 main forms
[F_Interimaires] and [F_SuivimodMission].
It is set  to "read only" on [F_Interimaires] (no mod, no add etc..) and
open to mod and addition on [F_SuiviModMission]. This subform has been
created first within [F_SuiviModMission ].
Also, the main form [F_Interimaires] has another subform in addition to
[F_FicheContrat sous-formulaire].
Hope this helps.

Hope this help
tina - 12 Jan 2005 22:40 GMT
okay, using the subform on two different main forms, and on main form
F_Interimaires, the subform is read-only. both important pieces of the
puzzle. i have to wonder why you're changing a value in the main form record
from a read-only value in the subform, but we'll go with that.

since you're not changing the value in the control on the subform, using the
AfterUpdate event won't work. instead, try adding the following code to the
subform's Current event procedure, as

       If Me.Parent.Name = "F_Interimaires" Then
               If IsNull(Me!IDContrat) Then
                   Me.Parent!Etat = "Candidat"
               Else
                   Me.Parent!Etat = "Interimaire"
               End If
       End If

the above code checks to see if the subform is open in main form
F_Interimaires. if it is, then the update to textbox Etat takes place. if
instead the subform is open in main form F_SuiviModMission, then the update
will not take place.

since the update code runs from the subform's Current event, you also need
to consider the following:  are there multiple records in the subform? do i
want the value in Etat to be changed each time i move between multiple
records on the subform? once the value has been set in Etat, do i want it to
be changed every time i move between records on the main form? every time i
open the main form?

the last two things mentioned above *will* happen automatically, whether the
subform has one or multiple records, unless you write code to stop it. you
have to figure out exactly what you want to happen, and when, in order to
control the situation.

hth

> > > The subform is used on another form, could that make a difference ??
> >
[quoted text clipped - 98 lines]
>
> Hope this help
Frankie - 13 Jan 2005 09:43 GMT
> okay, using the subform on two different main forms, and on main form
> F_Interimaires, the subform is read-only. both important pieces of the
[quoted text clipped - 151 lines]
>
> Tina,
As you may have noticed I 'm not an expert  with Access VBA code.
This project I'm working on is getting to a point  where good code writing
becomes essential to make the whole database run smoothly.
Project detail :
Development of DB for Temporary jobs Agency activities including prospects
management, Temp WorKers management , Sales force management.
I have built all the necessary forms and tables with all kind of queries and
put fake data to make some tests.
For example,  the main form [F_Interimaires] contains all kind of info which
relate to the temporary workers in search of Temp jobs.
The main form [F_SuiviModMissions] contains info about  "Missions' meaning
temporary job positions to be filled in a determined area.
In order to avoid "doublons" (same lenghty infos on multiple fomrs) I first
created the subform  [F_FicheContrat sous-formulaire] from the main form
[F_SuiviModMission] bound on [IDMission] control box  with [IDContrat] set as
autonum field. So when a Temp Worker qualifies for a given mission, the
[IDContat] increments as soon as a TW is choosen from a listbox
[IDInterimaire]
Now, what I want to see happening is : as soon as contract number gets
created for any given TW (Interimaire), the status of  [Etat] on main form
[F_Interimaires] wich is by default  "Candidat" should change to
"Interimaire". And I thought that by putting subform [F_FicheContrat
sous-formulaire] in "read-only" within main form [F_Interimaires] I could
create a conditional value to [Etat] control box.
Again I am no expert  and I need to learn  code writing if I want to achieve
this DB which I do.  And  I have a lot of conditional values to set on
different forms.
I figure that if manage to do it the right way for one then I 'll be able to
replicate it. Or am i just overestimating myself???
I hope this gives you  a better idea...
tina - 13 Jan 2005 12:20 GMT
i'm afraid you really lost me, Frankie (though i'll readily admit that i'm
not as good as many folks here at visualizing a setup from a written
description).

> I have built all the necessary forms and tables

this statement gives me a bit of a chill, though. proper database design is
to build and relate normalized tables *first*, and then the queries, forms,
etc, will flow logically from that foundation. how about posting your
tables/ relationships setup, in the following type of outline:

tblCustomers
CustID (primary key, called PK)
CustFirstName
CustLastName
etc.

tblOrders
OrderID (PK)
CustID (foreign key from tblCustomers)
OrderDate
etc

tblOrderDetails
OrdDetailID (PK)
OrderID (foreign key from tblOrders)
ProductID (foreign key from tblProducts)
Quantity
etc.

tblProducts
ProductID (PK)
Product Name
etc

tblCustomers [one-to-many] tblOrders
tblOrders [one-to-many] tblOrderDetails
tblProducts [one-to-many] tblOrderDetails

> > okay, using the subform on two different main forms, and on main form
> > F_Interimaires, the subform is read-only. both important pieces of the
[quoted text clipped - 85 lines]
> > > > > > >> The text box on the main forms Control Source should look like
> > this:

=IIf([Forms]![FormName]![ControlName].[Form]![SubFormControlName]
> > Is
> > > > > > >> Null,"ValueYouWant")
[quoted text clipped - 90 lines]
> replicate it. Or am i just overestimating myself???
> I hope this gives you  a better idea...
Frankie - 13 Jan 2005 17:41 GMT
> i'm afraid you really lost me, Frankie (though i'll readily admit that i'm
> not as good as many folks here at visualizing a setup from a written
[quoted text clipped - 264 lines]
>
> Tina,
Here is my table/relationships setup :

tblInterimaires  (Temp Workers)
IDInterimaires (PK)
FirstName
LastName
Departement
Acticity
etc.

tblClients  (Prospects/Customers)
IDClients (PK)
FirstName
LastName
etc.

tblMissions
IDMissions (PK)
IDClients (foreign key to tblClients)
Departement
Activity
etc.

tblContrats
IDContrat (PK)
IDMissions (foreign key to tblMissions)
IDInterimaire (foreign key to tblInterimaires)

tblCommercial    (Sales Force)
IDCommercial  (PK)
FirstName
LastName
etc.

tblSuiviCommercial    (Prospects follow up)
IDSuivi  (PK)
IDCommercial (foreign key to tblCommercial)
IDClients (foreign key to tblClients)
etc;

tblCRH   (Contracts Hours )
IDCRH  (PK)
IDContrat (foreign key to tblContrat)

tbl Interimaires [one-to-many] tblContrat
tblClients [one-to-many] tblMissions
tblMissions [one-to-many] tblContrat
tblCommerciaux [one-to-many] tblSuiviCommercial
tblClients [one-to-many] tblsuiviCommercial
tblContrat [one-to-many] tblCRH
tina - 13 Jan 2005 22:37 GMT
okay, just what i needed. i'll have to study this a bit, and try to apply it
to your forms question. one way or another, i'll post back to you within a
day.

> > i'm afraid you really lost me, Frankie (though i'll readily admit that i'm
> > not as good as many folks here at visualizing a setup from a written
[quoted text clipped - 190 lines]
> > > > > > > > > Al proposal -
> > > > =IIF(IsNull([Forms]![F_Interimaires]![F_FicheContrat

sous-formulaire].Form![IDContrat),"Candidat","Interimaire")
> > > > > > > > > Is there something I should add within  Private Sub  like  a
> > Call
[quoted text clipped - 119 lines]
> tblClients [one-to-many] tblsuiviCommercial
> tblContrat [one-to-many] tblCRH
tina - 14 Jan 2005 10:10 GMT
okay, i think i'm beginning to get the picture. i'll guess that:

your main form F_SuiviModMission is bound to tblMissions (or bound to a
query that includes fields from tblMissions)
your subform F_FicheContrat sous-formulaire is bound to tblContrats

within main form F_SuiviModMission, in subform F_FicheContrat
sous-formulaire, you create new records in tblContrats by entering an
IDInterimaires value from tblInterimaires. in other words, you assigning a
temporary worker to a specific mission belonging to a specific client.

now, i'll assume that there is a field in tblInterimaires that shows the
current status of the temporary worker - either "Candidat" or "Interimaire".
(i'm guessing that more or less means "available for assignment" or
"currently assigned to a job".)

i'll also assume there are fields in tblContrats that hold the date a
particular job was assigned, and another date for when it was closed. or
perhaps a "Closed" checkbox. at any rate, *some value* in each record that
indicates whether the worker's assignment is current, or concluded.

given all the above assumptions, i would forget changing the status of the
worker's record from within main form F_Interimaires. instead, change the
status of the worker to "Interimaire" when the new record is added in
subform F_FicheContrat sous-formulaire, within main form F_SuiviModMission.
and then change the worker's status again when the contract is concluded.
you could do that with an update query, run on the subform's AfterUpdate
event, and referring to the value in field IDInterimaire to update the
correct record in tblInterimaires. something along the lines of

   If Me!ClosedContract = False Then
       CurrentDb.Execute "UPDATE tblInterimaires SET " _
           & "WorkerStatus = 'Interimaires' WHERE IDInterimaires = " _
           & Me!IDInterimaire, dbFailOnError
   ElseIf Me!ClosedContract = True Then
       CurrentDb.Execute "UPDATE tblInterimaires SET " _
           & "WorkerStatus = 'Candidat' WHERE IDInterimaires = " _
           & Me!IDInterimaire, dbFailOnError
   End If

i made up the name WorkerStatus, of course, but the above should give you an
understanding of what i'm getting at. you can modify it to work in your
situation. you have to consider some possible situations here, such as:
once a contract record is created, can the IDInterimaire value be changed
(can you substitute Mary for Lisa, in an existing record)? if yes, you need
to write code to handle changing Lisa's status back to "Candidat", and
changing Mary's status to "Interimaire". once a contract record is created,
can it be deleted? or marked as "void"? in either case, you'd again have to
update the worker's status back to "Candidat".

hopefully my guesses are close enough to give you some ideas of how to
accomplish your task.

> Here is my table/relationships setup :
>
[quoted text clipped - 46 lines]
> tblClients [one-to-many] tblsuiviCommercial
> tblContrat [one-to-many] tblCRH
Frankie - 14 Jan 2005 12:05 GMT
You're guessing right!
I do have  [Date Begin] and [DateEnd] fields on  F_FicheContrat  
sous-formulaire which became F_FicheContrat , a form on its own,  in order  
to make things easier.
And it works fine now. Thanks to you!
Now I have to implement what you propose to deal with TW
availability/unavailibility with the variables you mention and see if it
works.
I might have to get back to you if you don't mind !
Thank you so much for your help.

> okay, i think i'm beginning to get the picture. i'll guess that:
>
[quoted text clipped - 99 lines]
> > tblClients [one-to-many] tblsuiviCommercial
> > tblContrat [one-to-many] tblCRH
tina - 14 Jan 2005 20:32 GMT
you're very welcome. if you do have future issues to resolve, suggest you
start a new thread. this one has gotten pretty long, and with a new thread
you're more likely to get responses from a variety of experienced and expert
developers. good luck!  :)

> You're guessing right!
> I do have  [Date Begin] and [DateEnd] fields on  F_FicheContrat
[quoted text clipped - 110 lines]
> > > tblClients [one-to-many] tblsuiviCommercial
> > > tblContrat [one-to-many] tblCRH
AlCamp - 12 Jan 2005 14:09 GMT
Frankie,
  I tested my last solution...
>>     If IsNull(IDContrat) Then
>>         [Forms]![F_Interimaires]![Etat] = "Candidat"
>>     Else
>>         [Forms]![F_Interimaires]![Etat] = "Interimaire"
>>     End If
on a test database I have (using the same concept with my names and
controls), and it worked fine.  In the subform, when I changed the
[IDContrat] value, a bound field on the main form "reflected" those changes.
  So... there may be something missing from your explanantion of your
"setup."
hth
Al Camp

>> Frankie,
>>    You wrote...
[quoted text clipped - 78 lines]
> The subform is used on another form, could that make a difference ??
> Thanks for your continuing support
Frankie - 12 Jan 2005 22:03 GMT
> Frankie,
>    I tested my last solution...
[quoted text clipped - 95 lines]
>
> It does work !!
Tha
Frankie - 12 Jan 2005 22:05 GMT
> Frankie,
>    I tested my last solution...
[quoted text clipped - 95 lines]
>
> It does work !!
Thank you  for your help.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.