473,394 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Can someone help me with a sophisticated DLookUp?

MLH
The following SQL returns PRECISELY what I want:

SELECT TOP 1 tblCorrespondence.CorrespID,
tblOutboundTypes.OTypDescription, tblCorrespondence.OutDate,
tblCorrespondence.VehicleJobID, tblCorrespondence.OutType
FROM tblOutboundTypes INNER JOIN tblCorrespondence ON
tblOutboundTypes.OutType = tblCorrespondence.OutType
WHERE (((tblCorrespondence.OutDate) Is Not Null) AND
((tblCorrespondence.VehicleJobID)=3))
ORDER BY tblCorrespondence.CorrespID DESC;

For a given VehicleJobID value of 3, I wanted to find the last
record in correspondence table and its corresponding OTypDescription
in tblOutboundTypes.

I'd love to be able to do this on-the-fly, so to speak, with a DLookUp
statement. I'm wondering how to structure it. Hope some of you have
suggestions.
Jul 15 '06 #1
8 4550
Any reason you can't join your table to this query?

Jul 15 '06 #2
MLH
On 14 Jul 2006 18:38:07 -0700, pi********@hotmail.com wrote:
>Any reason you can't join your table to this query?
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hmmm??? What I'd like to do, actually, is not have
this query as a saved object at all. I have a procedure
in which I'd like to utilize a DLookUp to extract a
specific record (like this SQL does) and find a particular
field value in it. Not sure that can be done, but I thought
I'd ask.
Jul 15 '06 #3

MLH wrote:
On 14 Jul 2006 18:38:07 -0700, pi********@hotmail.com wrote:
Any reason you can't join your table to this query?
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hmmm??? What I'd like to do, actually, is not have
this query as a saved object at all. I have a procedure
in which I'd like to utilize a DLookUp to extract a
specific record (like this SQL does) and find a particular
field value in it. Not sure that can be done, but I thought
I'd ask.
I guess try both ways and use GetTickCount and run the test like 10K
times to see which is faster. (see the performance measurement section
in Vogel's "VB Object & Component Handbook" Hard to argue with
empirical evidence.

Jul 15 '06 #4
MLH wrote:
The following SQL returns PRECISELY what I want:

SELECT TOP 1 tblCorrespondence.CorrespID,
tblOutboundTypes.OTypDescription, tblCorrespondence.OutDate,
tblCorrespondence.VehicleJobID, tblCorrespondence.OutType
FROM tblOutboundTypes INNER JOIN tblCorrespondence ON
tblOutboundTypes.OutType = tblCorrespondence.OutType
WHERE (((tblCorrespondence.OutDate) Is Not Null) AND
((tblCorrespondence.VehicleJobID)=3))
ORDER BY tblCorrespondence.CorrespID DESC;

For a given VehicleJobID value of 3, I wanted to find the last
record in correspondence table and its corresponding OTypDescription
in tblOutboundTypes.

I'd love to be able to do this on-the-fly, so to speak, with a DLookUp
statement. I'm wondering how to structure it. Hope some of you have
suggestions.
I may be missing something, so excuse me if my advice is incorrect.
Remove the
tblCorrespondence.VehicleJobID=3
in the where statement.

Save the query to QUery1

Then Me.OTypDescription =
Dlookup("OTypDescription","Query1","tblCorresponde nce.VehicleJobID=3")
Jul 15 '06 #5
MLH wrote:
The following SQL returns PRECISELY what I want:

SELECT TOP 1 tblCorrespondence.CorrespID,
tblOutboundTypes.OTypDescription, tblCorrespondence.OutDate,
tblCorrespondence.VehicleJobID, tblCorrespondence.OutType
FROM tblOutboundTypes INNER JOIN tblCorrespondence ON
tblOutboundTypes.OutType = tblCorrespondence.OutType
WHERE (((tblCorrespondence.OutDate) Is Not Null) AND
((tblCorrespondence.VehicleJobID)=3))
ORDER BY tblCorrespondence.CorrespID DESC;

For a given VehicleJobID value of 3, I wanted to find the last
record in correspondence table and its corresponding OTypDescription
in tblOutboundTypes.

I'd love to be able to do this on-the-fly, so to speak, with a DLookUp
statement. I'm wondering how to structure it. Hope some of you have
suggestions.
All Dlookup does is translate your request to SQL, process it and
return the answer.
You already have your SQL. So why translate it to a DLookup so that the
DLookup can translate it back again and use it?

Assuming your string is pointed to by a variable called sql then you
should be able to get your value directly as
CurrentDB.Openrecordset(sql).Collect(1) ... which returns the value of
the second field (fields are numbered zero, one, etc)

Trying to simplity your SQL I get something (probably with syntax
errors) like:

Public Function GetOTypDescription(ByVal vVehicleJobId As Long) As
String
Dim sql As String
sql = "SELECT TOP 1 tot.OTypDescription"
sql = sql & vbNewLine & "FROM tblOutboundTypes tot"
sql = sql & vbNewLine & "INNER JOIN tblCorrespondence tc ON"
sql = sql & vbNewLine & "tot.OutType = tc.OutType"
sql = sql & vbNewLine & "WHERE tc.OutDate Is Not Null AND"
sql = sql & vbNewLine & "tc.VehicleJobID=" & vVehicleJobId
sql = sql & vbNewLine & "ORDER BY tc.CorrespID DESC;"
GetOTypDescription = CurrentDb.OpenRecordset(sql).Collect(0)
End Function

If you create a public function like that (or a private function at the
end of your module) then all you need to do to use
GetOTypDescription(7)
quite simply.

Jul 15 '06 #6
MLH
Speed is not an issue. It isn't repetitive. Running
the query seems instantaneous to me. I can barely
get my finger off the left mouse button after clicking
the BANG button and launching the query before
its results are staring me in the face.

What I would like to know is if DLookUp can be
used to extract the same data. Do you know how?
Jul 15 '06 #7
MLH wrote:
What I would like to know is if DLookUp can be
used to extract the same data. Do you know how?
Not, AFAIK, with joined table. DlookUp deals with one table.

Make it a function, it's easy as pie (air code) - you're using A97, right?

You want to retrieve the OTypDescription value. Just use this very
simple function with the job id as an argument.

Function fRetrievePreciiselyWhatMlhNeeds(lngVehJobId as long) as String

'assumes a long is returned. If there is no result from the
'SQL, then a "" (ZLS) is returned
'
'lngVehJobId is the vehicle job number ID fed from calling proc

dim strSql as string
dim dbs as DAO.Database
dim rst as DAO.recordset

On error goto Err_Proc

'Simplified SQL follows - assumes you have unique field names and
'won't need the overly verbose SQL the excellent Access query
'design grid returns by default. LOOK AT WHAT I've WRITTEN
'CAREFULLY and how lngVehJobId is introduced

strsql = "SELECT TOP 1 OTypDescription " & _
"FROM tblOutboundTypes INNER JOIN tblCorrespondence ON " & _
"tblOutboundTypes.OutType = tblCorrespondence.OutType " & _
"WHERE OutDate Is Not Null AND " & _
"VehicleJobID = " & lngVehJobId & _
"ORDER BY CorrespID DESC"

Set dbs = Access.currentdb

'Open SQL in snapshot mode - this is much faster when
'there are lots of records involved

set rst = dbs.openrecordset(strSql, dbopensnapshot)

with rst

if .eof then

fRetrievePreciiselyWhatMlhNeeds = ""

else

.movefirst 'note that top 1 values can return more than 1 record

fRetrievePreciiselyWhatMlhNeeds = .fields!OTypDescription

end if

End with

Exit_Proc:

On Error Resume Next
rst.close
Set rst = nothing
dbs.close
set dbs = nothing
on error goto 0

exit function

Err_Proc:

Select Case err.number

case else

msgbox "Error " & err.number & " " & _
err.description,vbcritical, _
"Error in fRetrievePreciiselyWhatMlhNeeds", _
Err.HelpFile, Err.HelpContext

Resume exit_proc

End select

end function
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "Whatcha doin?" - Ditto "TIM-MAY!!" - Me
Jul 15 '06 #8
MLH
Thx, Tim.
AAMOF, that's what I did.
Was curious about another way.
Lyle Fairfield made a suggestion
that I'm gonna try. Looks promising.
Jul 16 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: KLAU | last post by:
I have a field that retrieves information from an expression in a query. I have used a DLookup function to get the calculated field from the query. However, the relationship is 1-to-many so one...
6
by: JLM | last post by:
What am I missing here? I have a form where I enter a "Class Code". This value corresponds to what sits in table "class code descriptions" along with the "title" of each "class code." Key...
5
by: Kalvin Schroder | last post by:
I am fairly new to Access, and am trying to put together an invoice form. The main form in called InvoiceDetailFm. Source is the table InvoiceDetail and has invoice number, saleman, and CustID as...
3
by: DFS | last post by:
FYI, Using DLookup("ResultsField","Pass-thru query") consumes 2 SEQUENCE numbers each time it's called. Anyone know why?
4
by: MLH | last post by:
I have tried using DLookUp in this manner... If DLookUp("","tblClients","='2021234567'") Then MsgBox "Found it!" End If I am wondering if that is a misuse of the DLookUp command? Type...
2
by: ctyrrell | last post by:
I have read with interest the many discussions of the 3048 Error: Cannot open any more databases. I understand how the number of open Table ID's causes the problem. My question has to do with the...
8
by: Christine Henderson | last post by:
I have a problem using the above function in the following simplified circumstance: In the lookup table called "Klms Travelled" I have 3 fields, eg: Receiver Name Receiver Suburb ...
11
by: MLH | last post by:
DLookup("", "tblPreliminaryVINs", "=Forms!frmVINODO!SerialNum") is giving me a Type Mismatch error. That's confusing to me and I don't know how to circumvent it. The field in...
9
by: | last post by:
In my database I have a 'control table' in which basic info is stored about the application, for instance the application's path and the name of the company that is using it. In all of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.