Results 1 to 7 of 7
  1. #1
    em07189 is offline Novice
    Windows 7 Access 2007
    Join Date
    Jan 2010
    Posts
    10

    if...then conditions ???


    Hi everyone!

    it's possible to do a lookup list with a if condition in access.

    for example:

    if the value of a field is "a" then link to the table "x"
    if the value of a field is "b" then link to the table "y"

    thanks.

  2. #2
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    Sure e.g.

    I have two tables:

    Table1={p_id,F_Name,age,Sex,Designation} Senior managers
    Table2={p_id,F_Name,age,Sex,Designation} Junior Managers


    Suppose I have a form (Data_entry) with p_id, Name, Designation

    now I have to lookup the age of the person. Now if the designation is senior manager the I want to lookup table1 and when junior managers Table2. In this example we will lookup age from the tables based on an if condition on designation.

    Dim intAge as integer

    Select Case Me.Designation
    Case is ="Senior Manager"
    intAge=Dlookup("[age]","Table1","p_id"="& Forms!Data_entry!p_id)
    Case is = "Junior Manager"
    intAge=Dlookup("[age]","Table2","p_id"="& Forms!Data_entry!p_id)
    End Select

    This is using Select case. Now with if

    Dim intAge as integer
    if Me.Designation="Senior Manager" then
    intAge=Dlookup("[age]","Table1","p_id"="& Forms!Data_entry!p_id)
    elseif Me.Designation="Junior Manager" Then
    intAge=Dlookup("[age]","Table2","p_id"="& Forms!Data_entry!p_id)
    end if

    Now this is another way of doing it:
    Dim intAge as integer
    intAge=iif(Me.Designation="Senior Manager",Dlookup("[age]","Table1","p_id"="& Forms!Data_entry!p_id),iif(Me.Designation="Junior Manager",Dlookup("[age]","Table2","p_id"="& Forms!Data_entry!p_id)))

    These are the following ways how you can use if with lookup.

    if this solves your problem please mark the thread to be solved.

  3. #3
    em07189 is offline Novice
    Windows 7 Access 2007
    Join Date
    Jan 2010
    Posts
    10
    Hi maximus!

    were do you enter this code? is this visual basic?

    I'm a novice in Access.

    Quote Originally Posted by maximus View Post
    Sure e.g.

    I have two tables:

    Table1={p_id,F_Name,age,Sex,Designation} Senior managers
    Table2={p_id,F_Name,age,Sex,Designation} Junior Managers


    Suppose I have a form (Data_entry) with p_id, Name, Designation

    now I have to lookup the age of the person. Now if the designation is senior manager the I want to lookup table1 and when junior managers Table2. In this example we will lookup age from the tables based on an if condition on designation.

    Dim intAge as integer

    Select Case Me.Designation
    Case is ="Senior Manager"
    intAge=Dlookup("[age]","Table1","p_id"="& Forms!Data_entry!p_id)
    Case is = "Junior Manager"
    intAge=Dlookup("[age]","Table2","p_id"="& Forms!Data_entry!p_id)
    End Select

    This is using Select case. Now with if

    Dim intAge as integer
    if Me.Designation="Senior Manager" then
    intAge=Dlookup("[age]","Table1","p_id"="& Forms!Data_entry!p_id)
    elseif Me.Designation="Junior Manager" Then
    intAge=Dlookup("[age]","Table2","p_id"="& Forms!Data_entry!p_id)
    end if

    Now this is another way of doing it:
    Dim intAge as integer
    intAge=iif(Me.Designation="Senior Manager",Dlookup("[age]","Table1","p_id"="& Forms!Data_entry!p_id),iif(Me.Designation="Junior Manager",Dlookup("[age]","Table2","p_id"="& Forms!Data_entry!p_id)))

    These are the following ways how you can use if with lookup.

    if this solves your problem please mark the thread to be solved.

  4. #4
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    My question is where do you want to use it tell me and I will tell you how.

    These codes can be used in the afterupdate events of controls like text boxes and comboboxes and even on onclick events of text boxes.

  5. #5
    em07189 is offline Novice
    Windows 7 Access 2007
    Join Date
    Jan 2010
    Posts
    10
    Hi Manimus!

    I want to use it in a field of a table that is a like listbox .

    but this code is done in vbA with a form?



    Quote Originally Posted by maximus View Post
    My question is where do you want to use it tell me and I will tell you how.

    These codes can be used in the afterupdate events of controls like text boxes and comboboxes and even on onclick events of text boxes.

  6. #6
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    Now your requirement is clear

    Suppose I have Three tables

    Table1={p_id,F_Name,age,Sex,Designation}
    Senior_manager_salary={p_id,Salary}
    Junior_manager_salary={p_id,Salary}


    if designation is senior manager you will lookup up Senior_manager_salary or Junior_manager_salary.
    Create a query in the design view from table Table1.
    In a blank colunm type:

    Salary:iif([Designation]="Senior Manager",Dlookup("[Salary]","Senior_manager_salary","[Senior_manager_salary].[p_id]="&[Table1].[p_id]),iif([Designation]="Junior Manager",Dlookup("[Salary]","junior_manager_salary","[Junior_manager_salary].[p_id]="&[Table1].[p_id])))

    if this solves your problem please mark this thread solved.

  7. #7
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    were you able to solve your problem.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. iif statement with multiple conditions
    By ragsgold in forum Queries
    Replies: 7
    Last Post: 08-24-2011, 05:38 PM
  2. Conditions / Expressions
    By Mark344 in forum Access
    Replies: 1
    Last Post: 02-19-2010, 08:15 AM
  3. Query on multipl conditions.
    By Goldenbird in forum Queries
    Replies: 0
    Last Post: 06-28-2009, 11:45 PM
  4. Expression with multiple conditions
    By techexpressinc in forum Queries
    Replies: 3
    Last Post: 06-19-2009, 08:33 PM
  5. Iff Statement Meeting 2 Conditions
    By newbie in forum Access
    Replies: 11
    Last Post: 06-11-2009, 11:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums