Results 1 to 3 of 3
  1. #1
    Parminder Singh is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    May 2017
    Posts
    1

    DLOOKUP with IF Function in MS ACCESS DB

    Hello Guys,

    Can anyone look into this code for me?
    Not getting IF statement working!
    .................................................. ........................................


    Private Sub Machine_drawing_Number_AfterUpdate()
    Dim MachNum As String
    Dim Mat As String
    Dim Tim As String
    Dim stLinkCriteria2 As String
    Dim Result As String


    Mat = Me.Combo222.Value
    Result = Me.Machine_drawing_Number.Value
    stLinkCriteria2 = "[Combo222]= " & "'" & Mat & "'"


    MachNum = DLookup("[Casting drawing Number]", "SlidingStem_Table", "[Machine_drawing_Number]= '" & Me.Machine_drawing_Number & "'")
    Tim = DLookup("[Casting drawing Number]", "SlidingStem_Table", stLinkCriteria2)


    If MachNum = Tim Then
    MsgBox "This Machine Drawing Number : " & Result & " has already been entered in database with same Material" _
    & vbCr & vbCr & "Please check the Machine number again.", vbInformation, "Duplicate information."
    Me.Undo
    Else
    MsgBox "You can go ahead!"

    End If

    End Sub

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    step through the code and check you are getting the expected values

    however this looks wrong

    stLinkCriteria2 = "[Combo222]= " & "'" & Mat & "'"

    implies you have a field in your SlidingStem_Table called Combo222

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,644
    Should post lengthy code within CODE tags to retain indentation and easier readability.

    What does 'not work' mean - error message, wrong results, nothing happens?

    DLookup returns Null if no match. Cannot do comparison with Null. Review http://allenbrowne.com/casu-12.html

    The only variable type that can hold Null is Variant. Your code will error if DLookup returns Null because String variable cannot hold Null.

    stLinkCriteria2 makes no sense. I presume Combo222 is combobox on form, not a field in table, so how can the DLookup apply filter criteria? Replace with correct field name.

    Try both criteria together.

    If Not IsNull(DLookup("[Casting drawing Number]", "SlidingStem_Table", "[Machine_drawing_Number]= '" & Result & "' AND [Material]='" & Mat & "'")) Then

    Example with DCount, which returns 0 if no match.

    If DCount("*", "SlidingStem_Table", "[Machine_drawing_Number]= '" & Result & "' AND [Material]='" & Mat & "'") > 0 Then
    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.

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

Similar Threads

  1. DLookup function - need help
    By Lukael in forum Programming
    Replies: 4
    Last Post: 03-13-2016, 05:34 AM
  2. Dlookup Function
    By balajigade in forum Access
    Replies: 2
    Last Post: 09-10-2015, 01:55 AM
  3. Replies: 5
    Last Post: 11-17-2013, 04:20 PM
  4. Need Help With DLookUp Function in Access 2010
    By fmartz in forum Programming
    Replies: 2
    Last Post: 09-05-2012, 09:34 AM
  5. Replies: 10
    Last Post: 08-29-2012, 06:45 AM

Tags for this Thread

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