Results 1 to 5 of 5
  1. #1
    muk5063 is offline Novice
    Windows 8 Access 2013
    Join Date
    Oct 2015
    Location
    Earth
    Posts
    5

    dlookup to check record in another table

    I am having troubling making a DLookup to work correctly. A button would be pressed to print the record, before printing it would check if payment details have been entered into another table by looking for the same Case No. I have done the following but it doesn't seem to work. It prints even if a corresponding Case No is not found in 'OurCaseRefNo'. I managed to get it working but realised that when the 'ServicesRec' table had more than one entry with the same Case No (which is possible and OK) it refused to print. So I tried sorting that issue and made a bigger mess. I am quite new to VB code.

    Any help highly appreciated.


    Code:
    Dim stDocName As String
    
    If DLookup("[OurCaseRefNo]", "ServicesRec", "[Case No]=" & "'" & Me![Case No] & "'") > 0 Then
    
    On Error GoTo Err_PrintCaseDetailsReport_Click
            stDocName = "CaseDetails"
            DoCmd.OpenReport stDocName, acNormal
    
    Exit_PrintCaseDetailsReport_Click:
         Exit Sub
    
    Err_PrintCaseDetailsReport_Click:
            MsgBox Err.Description
            Resume Exit_PrintCaseDetailsReport_Click
    
    Else
       
        MsgBox "Entry of record is incomplete. Please enter payment details."
        stDocName = "ServicesRecForm"
        
        stLinkCriteria = "[OurCaseRefNo]=" & "'" & Me![Case No] & "'"
        DoCmd.OpenForm stDocName, , , stLinkCriteria
    
    End If
    Last edited by June7; 12-25-2015 at 03:32 PM. Reason: add CODE tags

  2. #2
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    if you are going to post more than a line or two of code, strongly recommend you get in the habit of using the code tags to preserve indenting, otherwise code is difficult to read and many responders won't bother trying to follow it.

    With regards this bit of code

    If DLookup("[OurCaseRefNo]", "ServicesRec", "[Case No]=" & "'" & Me![Case No] & "'") > 0
    the >0 implies Ourcaserefno is numeric, whereas your critieria implises that Caseno is text.

  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,771
    If DLookup doesn't find anything it will return Null. So:

    If Not IsNull(DLookup("[OurCaseRefNo]", "ServicesRec", "[Case No]='" & Me![Case No] & "'")) Then

    or

    If DLookup("[OurCaseRefNo]", "ServicesRec", "[Case No]='" & Me![Case No] & "'") & "" <> "" Then

    or

    If DCount("[OurCaseRefNo]", "ServicesRec", "[Case No]='" & Me![Case No] & "'") > 0 Then

    If these fields are number type, remove the apostrophe delimiters.


    Advise not to use spaces or special characters/punctuation (underscore is exception) in naming convention.
    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.

  4. #4
    muk5063 is offline Novice
    Windows 8 Access 2013
    Join Date
    Oct 2015
    Location
    Earth
    Posts
    5
    Thank you both for your input. I have tried what you have suggested but to no avail. I face the same problem. I tried adding a breakpoint and added the following before the IF statement to see what was being returned from the dlookup.
    MeCaseNo = Me![case no].Value
    test = DLookup("[OurCaseRefNo]", "ServicesRec", MeCaseNo)
    "034910" was always returned by dlookup and stored in 'test'. That is the 'OurCaseRefNo' of the first record in 'ServicesRec'. While 'MeCaseNo' was '1234515' or '987654' or something else.

    Any ideas? thanks again

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Your WHERE CONDITION argument is incomplete. There is no logical operation. Look at the suggestions again. The statement in the original post was closer to correct.
    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. Replies: 1
    Last Post: 08-09-2015, 10:03 AM
  2. Replies: 9
    Last Post: 12-05-2014, 03:39 PM
  3. Check if record is exits in table
    By adam23262 in forum Access
    Replies: 4
    Last Post: 02-08-2013, 01:00 PM
  4. Replies: 4
    Last Post: 05-09-2012, 07:20 AM
  5. Replies: 6
    Last Post: 06-15-2011, 11:48 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