Results 1 to 8 of 8
  1. #1
    charly.csh is offline Competent Performer
    Windows 8 Access 2007
    Join Date
    Nov 2014
    Posts
    186

    Conditional If with two conditions and Dlookup

    Hi everyone!

    I have a question regarding a code;

    it starts with my table [Work_Team] with several fields as [Name] [Department] and [ProductionLine]; into my Main_Form I have definided this code:



    '************************************
    Dim Warehouse1 As String
    Dim Warehouse2 As String

    If Me.WT_Warehouse = 0 Then 'This is a checkbox

    'In this case my event stops in the line below; since person from warehouse has no "Production line" so it points out this line in yellow. What I want is to go to next ElseIf and use 'the criteria of variable Warehouse2

    'I am using two criteria because will be other departments that they should have a ProductionLine and should take the first criteria of Warehouse1

    Warehouse1 = DLookup("[Name]", "[Work_Team]", "[Department]= 'Warehouse' And [ProductionLine]= '" & Me.Line_Production & "'")

    Me.TeamM = Replace(Me.TeamM, Warehouse1, "")
    Me.TeamM = Replace(Me.TeamM, Chr(10), "")
    Me.TeamM = Replace(Me.TeamM, Chr(13), "")

    ElseIf Me.WT_Warehouse = 0 Then

    Warehouse2 = DLookup("[Name]", "[Work_Team]", "[Department]= 'Warehouse' ")

    Me.TeamM = Replace(Me.TeamM, Warehouse2, "")
    Me.TeamM = Replace(Me.TeamM, Chr(10), "")
    Me.TeamM = Replace(Me.TeamM, Chr(13), "")

    End If

    'YOUR HELP TO CORRECT THIS IS HIGHLY APPRECIATED!!

  2. #2
    A S MANN is offline Advanced System Analyst
    Windows XP Access 2007
    Join Date
    Oct 2010
    Location
    India
    Posts
    163
    Dim Warehouse1, StrWarehouse1 As String
    Dim Warehouse2,StrWarehouse2 As String

    StrWarehouse1 = DCount("[Name]", "[Work_Team]", "[Department]= 'Warehouse' And [ProductionLine]= '" & Me.Line_Production & "'")


    If Me.WT_Warehouse = 0 And StrWarehouse1 >= 1 Then
    Me.TeamM = Replace(Me.TeamM, Warehouse1, "")
    Me.TeamM = Replace(Me.TeamM, Chr(10), "")
    Me.TeamM = Replace(Me.TeamM, Chr(13), "")

    ElseIf Me.WT_Warehouse = 0 And StrWarehouse1 = 0 Then

    Warehouse2 = DLookup("[Name]", "[Work_Team]", "[Department]= 'Warehouse' ")

    Me.TeamM = Replace(Me.TeamM, Warehouse2, "")
    Me.TeamM = Replace(Me.TeamM, Chr(10), "")
    Me.TeamM = Replace(Me.TeamM, Chr(13), "")

    End If
    Try Above code

  3. #3
    charly.csh is offline Competent Performer
    Windows 8 Access 2007
    Join Date
    Nov 2014
    Posts
    186
    Wow it works pretty good!

    Just one question...
    The variable Warehouse1 is not defined as anything into the code; does it not affect?

    ***********************************
    Dim Warehouse1, StrWarehouse1 As String

    Dim Warehouse2,StrWarehouse2 As String

    StrWarehouse1 = DCount("[Name]", "[Work_Team]", "[Department]= 'Warehouse' And [ProductionLine]= '" & Me.Line_Production & "'")

    Me.TeamM = Replace(Me.TeamM, Warehouse1, "")

  4. #4
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Note:
    Code:
    Dim Warehouse1, StrWarehouse1 As String
    Dim Warehouse2,StrWarehouse2 As String
    Variables "StrWarehouse1" and "StrWarehouse2" are declared as type String
    Variables "Warehouse1" and "Warehouse2" are declared as type Variant.
    You MUST Explicitly declare variable types, otherwise they default to type Variant.


    Code:
    Dim Warehouse1 As String, StrWarehouse1 As String
    Dim Warehouse2 As String, StrWarehouse2 As String
    ------------------------------------------------------------------------------------------

    Code:
    StrWarehouse1 = DCount("[Name]", "[Work_Team]", "[Department]=  'Warehouse' And [ProductionLine]= '" & Me.Line_Production & "'")
    "Name" is a reserved word in Access and shouldn't be used as object names. Plus, it is not very descriptive - NAME of what??? Your dog's name? Street name you live on?

  5. #5
    A S MANN is offline Advanced System Analyst
    Windows XP Access 2007
    Join Date
    Oct 2010
    Location
    India
    Posts
    163
    Yes You are right. Correction
    If Me.WT_Warehouse = 0 And StrWarehouse1 >= 1 Then
    Warehouse1 = DLookup("[Name]", "[Work_Team]", "[Department]= 'Warehouse' And [ProductionLine]= '" & Me.Line_Production & "'")
    Rest is same. Kindly Try.

  6. #6
    charly.csh is offline Competent Performer
    Windows 8 Access 2007
    Join Date
    Nov 2014
    Posts
    186
    Hi A S MANN,

    I was reading my code and maybe I needed to clarify some stuff and in fact I put it into incomplete;
    could you help me please to correct this?

    if the person assigned to the Warehouse has No production line, the "event" take the person by default from warehouse but If I assigned into my table a production line for the warehouse's guy the "event" would look for the guy where the department is Warehouse AND Production_Line coincide with Me.Line_Production.

    Also when I unselect the checkbox this guy should be removed.

    This is my full code

    ************************************************

    Dim Warehouse1 As String
    Dim StrWarehouse1 As String
    Dim Warehouse2 As String
    Dim StrWarehouse2 As String

    'Clicking on the Warehouse checkbox and the field TeamM is empty

    If Me.WT_Warehouse = -1 And Len(Me.TeamM & vbNullString) = 0 Then

    'Here the field "Name" from the table Work_Team is the person responsible of the Warehouse department and it would work if this guy has a production line assigned, if not, should move to the next condition

    Me.TeamM = DLookup("[Name]", "[Work_Team]", "[Department]= 'Warehouse' And [Line_Production]= '" & Me.Line_Production & "'")

    'If the line below is empty it should move to the line below; I do not know if this is the issue...

    ElseIf Me.TeamM = DLookup("[Name]", "[Work_Team]", "[Department]= 'Warehouse'") Then

    StrWarehouse1 = DCount("[Name]", "[Work_Team]", "[Department]= 'Warehouse' And [LineProduction]= '" & Me.Line_Production & "'")

    'Here should start the next condition... If I unclick on the checkbox to eliminate from the "TeamM" textbox the guy from warehouse

    ElseIf Me.WT_Warehouse = 0 And StrWarehouse1 >= 1 Then

    Me.TeamM = Replace(Me.TeamM, StrWarehouse1, "")
    Me.TeamM = Replace(Me.TeamM, Chr(10), "")
    Me.TeamM = Replace(Me.TeamM, Chr(13), "")

    ElseIf Me.WT_Warehouse = 0 And StrWarehouse1 = 0 Then

    Warehouse2 = DLookup("[Name]", "[Work_Team]", "[Department]= 'Warehouse' ")

    Me.TeamM = Replace(Me.TeamM, Warehouse2, "")
    Me.TeamM = Replace(Me.TeamM, Chr(10), "")
    Me.TeamM = Replace(Me.TeamM, Chr(13), "")

    End If

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,954
    StrWarehouse1 is declared as a String yet your expression tests for a numeric value. In another place you set the variable to a number with DCount(). Declare the variable as an Integer.

    Only Variant datatype can hold Null. Other variable types will error if try to set with Null. DLookup() will return Null if no match.

    Last edited by June7; 05-03-2020 at 09:55 AM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    A S MANN is offline Advanced System Analyst
    Windows XP Access 2007
    Join Date
    Oct 2010
    Location
    India
    Posts
    163
    i could not figure out your requirement fully and your code.
    Kindly use Dcount () in all conditions in place of Dlookup () If conditions statements.
    Use DLookUp () in Set Value in Field Me.TeamM .
    DlookUp will give Error if No Records are found so Use DCount() =0 to define it.

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

Similar Threads

  1. Conditional Query with conditional results
    By Jeff_in_KCMO in forum Queries
    Replies: 4
    Last Post: 09-10-2014, 01:44 PM
  2. Dlookup with conditional problem
    By Hasher in forum Access
    Replies: 5
    Last Post: 08-04-2014, 06:10 AM
  3. Replies: 1
    Last Post: 04-30-2014, 05:26 PM
  4. Replies: 6
    Last Post: 05-26-2013, 09:10 PM
  5. Conditional Formatting with more than 3 conditions
    By cactuspete13 in forum Programming
    Replies: 3
    Last Post: 12-10-2012, 01:03 PM

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