My Assistant
![]() ![]() |
|
|
Mar 19 2012, 01:38 PM
Post
#1
|
|
|
UtterAccess Guru Posts: 728 From: Arcadia, Florida |
Is there any way to add an OrderBy ="ArrivalDate" to the following dlookup entry? I want to make sure that any and all listings are presented in descending order of ArrivalDate
Me.PhoneLookup = Nz(Trim(Me.PhoneLookup.Text)) varID = DLookup("ID", "Customers", "[Phone]='" & Me.PhoneLookup & "'") thanks ed |
|
|
|
Mar 19 2012, 01:49 PM
Post
#2
|
|
|
UtterAccess VIP Posts: 8,189 From: Pacific NorthWet |
I'm not aware of any syntax for the DLookup() function that does that... (doesn't mean much, though...)
However, if you want to select a set of records and order them, is there a reason you aren't using a query? |
|
|
|
Mar 19 2012, 01:49 PM
Post
#3
|
|
|
UtterAccess Guru Posts: 728 From: Arcadia, Florida |
Ok, I kind of figured it out by creating a query and dlooking at that rather than the main table
Now, is there any way to find a partial number, preferrably ending in that last x number of digits so if the phone number(s) are xxx-xxx-1234 it would find all that ended in 1234 regardless of the first 6 numbers thanks ed |
|
|
|
Mar 19 2012, 01:49 PM
Post
#4
|
|
|
Access Wiki and Forums Moderator Posts: 48,597 From: SoCal, USA |
Hi Ed,
I would say to use a sorted query for your DLookup() or, this function by Allen Browne: ELookup() Just my 2 cents... (IMG:style_emoticons/default/2cents.gif) EDIT: Oops, although it looks like I was slow replying, it would seem that we all posted a reply to the thread at the same time. This post has been edited by theDBguy: Mar 19 2012, 01:50 PM |
|
|
|
Mar 19 2012, 01:54 PM
Post
#5
|
|
|
Access Wiki and Forums Moderator Posts: 48,597 From: SoCal, USA |
Hi Ed,
Ok, I kind of figured it out by creating a query and dlooking at that rather than the main table Now, is there any way to find a partial number, preferrably ending in that last x number of digits so if the phone number(s) are xxx-xxx-1234 it would find all that ended in 1234 regardless of the first 6 numbers thanks ed You should be able to use the Right() function. For example: DLookup(...,"Right(Phone,4)='" & Right(Me.PhoneLookup,4) & "'") Just my 2 cents... (IMG:style_emoticons/default/2cents.gif) |
|
|
|
Mar 19 2012, 02:05 PM
Post
#6
|
|
|
UtterAccess Ruler Posts: 2,725 |
Using the Right() function, as suggested by Shrek is, of course, the way to parse out the rightmost X number of characters, but from your original post it sounds as if you're trying to retrieve more than one Record, and DLookUp() is only going to return the First Record that meets the given Criteria, not all Records. To return more than one Record you're going to have to use, once again, a Query or SQL Statement and display the results in a Form or ListBox.
Linq ;0)> |
|
|
|
Mar 19 2012, 03:56 PM
Post
#7
|
|
|
UtterAccess Veteran Posts: 379 From: Sydney, Australia |
Hi
I lost the link but here is a custom function that I found in a previous post. CODE Public Function ELookup(Expr As String, Domain As String, Optional Criteria As Variant, _ Optional OrderClause As Variant) As Variant On Error GoTo Err_ELookup 'Purpose: Faster and more flexible replacement for DLookup() 'Arguments: Same as DLookup, with additional Order By option. 'Return: Value of the Expr if found, else Null. ' Delimited list for multi-value field. 'Author: Allen Browne. allen@allenbrowne.com 'Updated: December 2006, to handle multi-value fields (Access 2007 and later.) 'Examples: ' 1. To find the last value, include DESC in the OrderClause, e.g.: ' ELookup("[Surname] & [FirstName]", "tblClient", , "ClientID DESC") ' 2. To find the lowest non-null value of a field, use the Criteria, e.g.: ' ELookup("ClientID", "tblClient", "Surname Is Not Null" , "Surname") 'Note: Requires a reference to the DAO library. Dim db As DAO.Database 'This database. Dim rs As DAO.Recordset 'To retrieve the value to find. Dim rsMVF As DAO.Recordset 'Child recordset to use for multi-value fields. Dim varResult As Variant 'Return value for function. Dim strSql As String 'SQL statement. Dim strOut As String 'Output string to build up (multi-value field.) Dim lngLen As Long 'Length of string. Const strcSep = "," 'Separator between items in multi-value list. 'Initialize to null. varResult = Null 'Build the SQL string. strSql = "SELECT TOP 1 " & Expr & " FROM " & Domain If Not IsMissing(Criteria) Then strSql = strSql & " WHERE " & Criteria End If If Not IsMissing(OrderClause) Then strSql = strSql & " ORDER BY " & OrderClause End If strSql = strSql & ";" 'Lookup the value. Set db = DBEngine(0)(0) Set rs = db.OpenRecordset(strSql, dbOpenForwardOnly) If rs.RecordCount > 0 Then 'Will be an object if multi-value field. If VarType(rs(0)) = vbObject Then Set rsMVF = rs(0).Value Do While Not rsMVF.EOF If rs(0).Type = 101 Then 'dbAttachment strOut = strOut & rsMVF!FileName & strcSep Else strOut = strOut & rsMVF![Value].Value & strcSep End If rsMVF.MoveNext Loop 'Remove trailing separator. lngLen = Len(strOut) - Len(strcSep) If lngLen > 0& Then varResult = Left(strOut, lngLen) End If Set rsMVF = Nothing Else 'Not a multi-value field: just return the value. varResult = rs(0) End If End If rs.Close 'Assign the return value. ELookup = varResult Exit_ELookup: Set rs = Nothing Set db = Nothing Exit Function Err_ELookup: MsgBox Err.Description, vbExclamation, "ELookup Error " & Err.Number Resume Exit_ELookup End Function Hope that helps. |
|
|
|
Mar 19 2012, 03:58 PM
Post
#8
|
|
|
UtterAccess Veteran Posts: 379 From: Sydney, Australia |
Oops, I just realised dbGuy already posted that.
Sorry, and here I thought I had made my very first reply to a post. |
|
|
|
Mar 19 2012, 04:32 PM
Post
#9
|
|
|
Access Wiki and Forums Moderator Posts: 48,597 From: SoCal, USA |
Hi,
Oops, I just realised dbGuy already posted that. Sorry, and here I thought I had made my very first reply to a post. No worries... It took me a while too to get used to how quickly things move along here. Cheers (IMG:style_emoticons/default/cheers.gif) |
|
|
|
Mar 20 2012, 11:05 AM
Post
#10
|
|
|
UtterAccess Guru Posts: 728 From: Arcadia, Florida |
OK so the revised line is :
varID = DLookup("ID", "PhoneLookupQuery", "[Phone]='" & Me.PhoneLookup & "'") but I would like it to find any phone number that matched the full number or a portion of it so it would find both 888-888-9900 and 888-9900 or 9900 thanks ed |
|
|
|
Mar 20 2012, 11:14 AM
Post
#11
|
|
|
Access Wiki and Forums Moderator Posts: 48,597 From: SoCal, USA |
Hi Ed,
Did you see my reply above on how to use the Right() function? Just my 2 cents... (IMG:style_emoticons/default/2cents.gif) |
|
|
|
Mar 20 2012, 11:17 AM
Post
#12
|
|
|
UtterAccess Guru Posts: 728 From: Arcadia, Florida |
Yes but I 'assumed' this would only return a match for the last four and that could result in more than one found if the entire 10 digit number was entered and it was different area code but same last four
ed |
|
|
|
Mar 20 2012, 11:34 AM
Post
#13
|
|
|
Access Wiki and Forums Moderator Posts: 48,597 From: SoCal, USA |
Hi Ed,
Yes but I 'assumed' this would only return a match for the last four and that could result in more than one found if the entire 10 digit number was entered and it was different area code but same last four ed You assumed correctly but that's what you asked for earlier. If you want to match "any" instance of the user input, you might be able to use the Like operator with a wildcard. For example: ..."[Phone] Like '*" & Me.PhoneLookup & "*'") Just my 2 cents... (IMG:style_emoticons/default/2cents.gif) |
|
|
|
Mar 20 2012, 11:53 AM
Post
#14
|
|
|
UtterAccess Ruler Posts: 2,725 |
...but I would like it to find any phone number that matched the full number or a portion of it... As I said previously: ...DLookUp() is only going to return the First Record that meets the given Criteria, not all Records. To return more than one Record you're going to have to use, once again, a Query or SQL Statement and display the results in a Form or ListBox. Linq ;0)> |
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 18th June 2013 - 09:28 PM |