Results 1 to 2 of 2
  1. #1
    JrMontgom is offline Competent Performer
    Windows Vista Access 2010 32bit
    Join Date
    Sep 2012
    Location
    Vero Beach, FL USA
    Posts
    124

    How to use DLookup with string variable Criteria

    I am trying to use DlookUp where the criteria is a string expression.


    The table I am using is tblCEFNames with fields [FundCompare], string;[PercentPortFolio] as Number and [ID] AutoNumber field

    Code:
    Dim strFName As String
    Dim intPercent As Integer
    Dim strFundComp As String
    Dim strCrit As String
    Dim intID As Integer
    
    intID = txtSubFundName.Column(0)
    'Debug.Print Forms!frmCEFList.Form![frmCEFNames subform]!SubFundName.Value
    'Get Fund name from combobox  column 1
    strFName = txtSubFundName.Column(1)
    
    
    'Create Criteria string 
    strCrit = "[ID] = " & intID & ""
    
    
    'ERROR Runtime error '94' Invalid Use of Null
    strFundComp = DLookup("[FundCompare]", "tblCefNames", "[ID] = 1")
    'same error:(
    strFundComp = DLookup("[FundCompare]", "tblCEFnames", strCrit)
    
    
    intPercent = DLookup("[PercentPortfolio]", "tblCEFnames", strCrit)
    I would really appreciate any help on this problem as It sure looked easy in the Help file but the examples skirted using a string criteria.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    What you have should work. What happens?

    However, those variables cannot handle Null. If DLookup does not find a match, returns Null. Might want to handle possibility.


    intPercent = Nz(DLookup("[PercentPortfolio]", "tblCEFnames", strCrit), 0)
    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. Using Variable String to Set a Form object
    By neo651 in forum Programming
    Replies: 17
    Last Post: 07-08-2013, 12:59 PM
  2. Replies: 3
    Last Post: 05-28-2013, 12:53 PM
  3. Extract Value from Variable in String
    By nguyenak in forum Programming
    Replies: 3
    Last Post: 05-24-2012, 03:50 PM
  4. DLOOKUP criteria using variable
    By Thumbs in forum Programming
    Replies: 14
    Last Post: 02-21-2012, 08:08 AM
  5. Using a string variable to specify a control
    By Gerry in forum Programming
    Replies: 3
    Last Post: 04-14-2010, 02:28 PM

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