faster alternative to series of DLookups

A

AccessMan

I am executing in VBA a series of individual DLookup statements against the
same record in order to get access to several fields within that record.
These statements seem to take a long time to run, and this approach certainly
seems less than optimal.

If I designate the table (it's really a query) as a DAO.Recordset, is there
a way to query this recordset once and get access to all of the field values
that I want? I don't have to use DAO.Recordset, but I have a tiny bit of
experience under my belt recently with it.
 
L

Linq Adams via AccessMonster.com

You don't say exactly what you're doing, but the general code would be

Dim rs As DAO.Recordset
Dim varField1 as String
Dim varField2 as String
Dim varField3 as String

Set db = CurrentDb
Set rs = db.OpenRecordset("YourQuery")

Do While Not rs.EOF

varField1 = rs!Field1
varField2 = rs!Field2
varField3 = rs!Field3

rs.MoveNext
Loop

rs.Close
Set rs = Nothing

This assigns three fields to three variables, but you could assign the field
data to controls in your form. You could also include conditional If...Then
around the assignment lines, to pull values from a particual record in
YourQuery.
 
L

Linq Adams via AccessMonster.com

You said you were comfortable with using DAO recordsets, but be sure

Microsof DAO 3.6 Object Library

is set in references (Tools - References)
 
A

AccessMan

This is helpful, but I still need to position rs at a particular record on
the basis of field criteria that I have. Say I have field criteria values of
Criteria1, Criteria2, and Criteria3 for rs fields Field1, Field2, and Field3.
I need access to the field values Field4 and Field5 when the criteria are
met.

I read ahead about your comment about DAO. I'm OK using ADO, but I just
can't find decent enough resources to get a sufficient critical mass of
knowledge. Any recommendations - books or sites???

Thanks!
 
A

AccessMan

I suppose I could use the do loop to find the record I'm looking for, but I
thought there would be a more direct way to do it programmatically.
 
D

Douglas J. Steele

You still haven't really explained what you're trying to do, but what about
something like:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim varField4 As Variant
Dim varField5 As Variant


strSQL = "SELECT Field4, Field5 " & _
"FROM MyTable " & _
"WHERE Field1 = " & Criteria1 & _
" AND FIeld2 = '" & Criteria2 & "' " & _
"AND Field3 = " & Format(Criteria3, "\#yyyy\-mm\-dd\#")

Set db = CurrentDb()
Set rs = db.OpenRecords(strSQL)

If rs.EOF = True Then
' No matching record found
Else
varField4 = rs!Field4
varField5 = rs!Field5
End If

Here, I've assume Field1 is numeric, Field2 is text and Field3 is a date
(just to show you the different syntax requirements)
 
A

AccessMan

Doug - you finished the puzzle. The Set rs=db.OpenRecords (strSQL) statement
is what I was looking for.

Thanks again!
 
D

david

For faster speed, your code would be like this:

dim fld1 as dao.field
dim fld2 as dao.field
dim fld3 as dao.field

set fld1 = rs!Field1
set fld2 = rs!Field2
set fld3 = rs!Field3
Do While Not rs.EOF
varField1 = fld1
varField2 = fd2
varField3 = fd3
rs.MoveNext
Loop


(david)
 
D

Douglas J. Steele

What's the actual SQL statement you're using?

What are the data types of the fields?
 
A

AccessMan

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT * FROM [qry Query] WHERE " & _
"[X] = '" & Me.MyX & "' AND " & _
"[Y] = '" & Me.MyY & "' AND " & _
"[Z] = '" & Me.MyZ & "'"
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL)

All fields are string.
 
D

Douglas J. Steele

Okay, try:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT * FROM [qry Query] WHERE " & _
"[X] = '" & Me.MyX & "' AND " & _
"[Y] = '" & Me.MyY & "' AND " & _
"[Z] = '" & Me.MyZ & "'"
Debug.Print strSQL
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL)

Now, when the code runs (and fails), go the Immediate Window (Ctrl-G) and
look at what's printed there for the SQL statement.

Oh, and are you sure they're all text fields? Did you perhaps use the Lookup
Field misfeature, so that while text appears in the field, the field is
actually storing a numeric value?

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


AccessMan said:
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT * FROM [qry Query] WHERE " & _
"[X] = '" & Me.MyX & "' AND " & _
"[Y] = '" & Me.MyY & "' AND " & _
"[Z] = '" & Me.MyZ & "'"
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL)

All fields are string.

Douglas J. Steele said:
What's the actual SQL statement you're using?

What are the data types of the fields?
 
A

AccessMan

The displayed sql looked fine. Here it is:

SELECT * FROM [qry Query] WHERE [X] = '9958228' AND [Y] = '23301' AND [Z] =
'S0001'

X and Y are formatted as text in their base table, but I am directing the
sql to a query.

I'm not using Lookup. The error is attributed to the Set rs =
db.OpenRecordset(strSQL) statement.

Do I need to add a second parameter to the OpenRecordset call like
dbOpenSnapshot?

Douglas J. Steele said:
Okay, try:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT * FROM [qry Query] WHERE " & _
"[X] = '" & Me.MyX & "' AND " & _
"[Y] = '" & Me.MyY & "' AND " & _
"[Z] = '" & Me.MyZ & "'"
Debug.Print strSQL
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL)

Now, when the code runs (and fails), go the Immediate Window (Ctrl-G) and
look at what's printed there for the SQL statement.

Oh, and are you sure they're all text fields? Did you perhaps use the Lookup
Field misfeature, so that while text appears in the field, the field is
actually storing a numeric value?

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


AccessMan said:
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT * FROM [qry Query] WHERE " & _
"[X] = '" & Me.MyX & "' AND " & _
"[Y] = '" & Me.MyY & "' AND " & _
"[Z] = '" & Me.MyZ & "'"
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL)

All fields are string.

Douglas J. Steele said:
What's the actual SQL statement you're using?

What are the data types of the fields?

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


I got a error, "Too few parameters. Expected 2.".


:

You still haven't really explained what you're trying to do, but what
about
something like:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim varField4 As Variant
Dim varField5 As Variant


strSQL = "SELECT Field4, Field5 " & _
"FROM MyTable " & _
"WHERE Field1 = " & Criteria1 & _
" AND FIeld2 = '" & Criteria2 & "' " & _
"AND Field3 = " & Format(Criteria3, "\#yyyy\-mm\-dd\#")

Set db = CurrentDb()
Set rs = db.OpenRecords(strSQL)

If rs.EOF = True Then
' No matching record found
Else
varField4 = rs!Field4
varField5 = rs!Field5
End If

Here, I've assume Field1 is numeric, Field2 is text and Field3 is a
date
(just to show you the different syntax requirements)

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


I suppose I could use the do loop to find the record I'm looking for,
but I
thought there would be a more direct way to do it programmatically.

:

This is helpful, but I still need to position rs at a particular
record
on
the basis of field criteria that I have. Say I have field criteria
values of
Criteria1, Criteria2, and Criteria3 for rs fields Field1, Field2,
and
Field3.
I need access to the field values Field4 and Field5 when the
criteria
are
met.

I read ahead about your comment about DAO. I'm OK using ADO, but I
just
can't find decent enough resources to get a sufficient critical
mass
of
knowledge. Any recommendations - books or sites???

Thanks!


:

You don't say exactly what you're doing, but the general code
would
be

Dim rs As DAO.Recordset
Dim varField1 as String
Dim varField2 as String
Dim varField3 as String

Set db = CurrentDb
Set rs = db.OpenRecordset("YourQuery")

Do While Not rs.EOF

varField1 = rs!Field1
varField2 = rs!Field2
varField3 = rs!Field3

rs.MoveNext
Loop

rs.Close
Set rs = Nothing

This assigns three fields to three variables, but you could
assign
the
field
data to controls in your form. You could also include conditional
If...Then
around the assignment lines, to pull values from a particual
record
in
YourQuery.
 
A

AccessMan

As a test I created a table from [qry Query] (using make table) and directed
my sql to it. This worked just fine. Unfortunately, I can't employ this
solution and need to use the query. Can I force these fields to be
explicitly text somehow in the query?

AccessMan said:
The displayed sql looked fine. Here it is:

SELECT * FROM [qry Query] WHERE [X] = '9958228' AND [Y] = '23301' AND [Z] =
'S0001'

X and Y are formatted as text in their base table, but I am directing the
sql to a query.

I'm not using Lookup. The error is attributed to the Set rs =
db.OpenRecordset(strSQL) statement.

Do I need to add a second parameter to the OpenRecordset call like
dbOpenSnapshot?

Douglas J. Steele said:
Okay, try:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT * FROM [qry Query] WHERE " & _
"[X] = '" & Me.MyX & "' AND " & _
"[Y] = '" & Me.MyY & "' AND " & _
"[Z] = '" & Me.MyZ & "'"
Debug.Print strSQL
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL)

Now, when the code runs (and fails), go the Immediate Window (Ctrl-G) and
look at what's printed there for the SQL statement.

Oh, and are you sure they're all text fields? Did you perhaps use the Lookup
Field misfeature, so that while text appears in the field, the field is
actually storing a numeric value?

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


AccessMan said:
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT * FROM [qry Query] WHERE " & _
"[X] = '" & Me.MyX & "' AND " & _
"[Y] = '" & Me.MyY & "' AND " & _
"[Z] = '" & Me.MyZ & "'"
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL)

All fields are string.

:

What's the actual SQL statement you're using?

What are the data types of the fields?

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


I got a error, "Too few parameters. Expected 2.".


:

You still haven't really explained what you're trying to do, but what
about
something like:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim varField4 As Variant
Dim varField5 As Variant


strSQL = "SELECT Field4, Field5 " & _
"FROM MyTable " & _
"WHERE Field1 = " & Criteria1 & _
" AND FIeld2 = '" & Criteria2 & "' " & _
"AND Field3 = " & Format(Criteria3, "\#yyyy\-mm\-dd\#")

Set db = CurrentDb()
Set rs = db.OpenRecords(strSQL)

If rs.EOF = True Then
' No matching record found
Else
varField4 = rs!Field4
varField5 = rs!Field5
End If

Here, I've assume Field1 is numeric, Field2 is text and Field3 is a
date
(just to show you the different syntax requirements)

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


I suppose I could use the do loop to find the record I'm looking for,
but I
thought there would be a more direct way to do it programmatically.

:

This is helpful, but I still need to position rs at a particular
record
on
the basis of field criteria that I have. Say I have field criteria
values of
Criteria1, Criteria2, and Criteria3 for rs fields Field1, Field2,
and
Field3.
I need access to the field values Field4 and Field5 when the
criteria
are
met.

I read ahead about your comment about DAO. I'm OK using ADO, but I
just
can't find decent enough resources to get a sufficient critical
mass
of
knowledge. Any recommendations - books or sites???

Thanks!


:

You don't say exactly what you're doing, but the general code
would
be

Dim rs As DAO.Recordset
Dim varField1 as String
Dim varField2 as String
Dim varField3 as String

Set db = CurrentDb
Set rs = db.OpenRecordset("YourQuery")

Do While Not rs.EOF

varField1 = rs!Field1
varField2 = rs!Field2
varField3 = rs!Field3

rs.MoveNext
Loop

rs.Close
Set rs = Nothing

This assigns three fields to three variables, but you could
assign
the
field
data to controls in your form. You could also include conditional
If...Then
around the assignment lines, to pull values from a particual
record
in
YourQuery.
 
D

Douglas J. Steele

What do you mean by "directing the sql to a query"?

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


AccessMan said:
As a test I created a table from [qry Query] (using make table) and
directed
my sql to it. This worked just fine. Unfortunately, I can't employ this
solution and need to use the query. Can I force these fields to be
explicitly text somehow in the query?

AccessMan said:
The displayed sql looked fine. Here it is:

SELECT * FROM [qry Query] WHERE [X] = '9958228' AND [Y] = '23301' AND [Z]
=
'S0001'

X and Y are formatted as text in their base table, but I am directing the
sql to a query.

I'm not using Lookup. The error is attributed to the Set rs =
db.OpenRecordset(strSQL) statement.

Do I need to add a second parameter to the OpenRecordset call like
dbOpenSnapshot?

Douglas J. Steele said:
Okay, try:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT * FROM [qry Query] WHERE " & _
"[X] = '" & Me.MyX & "' AND " & _
"[Y] = '" & Me.MyY & "' AND " & _
"[Z] = '" & Me.MyZ & "'"
Debug.Print strSQL
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL)

Now, when the code runs (and fails), go the Immediate Window (Ctrl-G)
and
look at what's printed there for the SQL statement.

Oh, and are you sure they're all text fields? Did you perhaps use the
Lookup
Field misfeature, so that while text appears in the field, the field is
actually storing a numeric value?

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT * FROM [qry Query] WHERE " & _
"[X] = '" & Me.MyX & "' AND " & _
"[Y] = '" & Me.MyY & "' AND " & _
"[Z] = '" & Me.MyZ & "'"
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL)

All fields are string.

:

What's the actual SQL statement you're using?

What are the data types of the fields?

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


I got a error, "Too few parameters. Expected 2.".


:

You still haven't really explained what you're trying to do, but
what
about
something like:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim varField4 As Variant
Dim varField5 As Variant


strSQL = "SELECT Field4, Field5 " & _
"FROM MyTable " & _
"WHERE Field1 = " & Criteria1 & _
" AND FIeld2 = '" & Criteria2 & "' " & _
"AND Field3 = " & Format(Criteria3, "\#yyyy\-mm\-dd\#")

Set db = CurrentDb()
Set rs = db.OpenRecords(strSQL)

If rs.EOF = True Then
' No matching record found
Else
varField4 = rs!Field4
varField5 = rs!Field5
End If

Here, I've assume Field1 is numeric, Field2 is text and Field3 is
a
date
(just to show you the different syntax requirements)

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


message
I suppose I could use the do loop to find the record I'm looking
for,
but I
thought there would be a more direct way to do it
programmatically.

:

This is helpful, but I still need to position rs at a
particular
record
on
the basis of field criteria that I have. Say I have field
criteria
values of
Criteria1, Criteria2, and Criteria3 for rs fields Field1,
Field2,
and
Field3.
I need access to the field values Field4 and Field5 when the
criteria
are
met.

I read ahead about your comment about DAO. I'm OK using ADO,
but I
just
can't find decent enough resources to get a sufficient
critical
mass
of
knowledge. Any recommendations - books or sites???

Thanks!


:

You don't say exactly what you're doing, but the general
code
would
be

Dim rs As DAO.Recordset
Dim varField1 as String
Dim varField2 as String
Dim varField3 as String

Set db = CurrentDb
Set rs = db.OpenRecordset("YourQuery")

Do While Not rs.EOF

varField1 = rs!Field1
varField2 = rs!Field2
varField3 = rs!Field3

rs.MoveNext
Loop

rs.Close
Set rs = Nothing

This assigns three fields to three variables, but you could
assign
the
field
data to controls in your form. You could also include
conditional
If...Then
around the assignment lines, to pull values from a particual
record
in
YourQuery.
 
A

AccessMan

I mean that the object of the FROM in strSQL is a query ([qry Query]) and not
a table.

I set the X, Y, and Z fields in [qry Query] to be plain text, and that did
not work.

To get beyond the issue, I have resorted to using a temporary table that is
cleared and then populated by an append version of [qry Query] when needed,
so I am operational now, although I'd still like to know how to use a query
as the source of the recordset.

[qry Query] is not updateable. Could that be the problem?


Douglas J. Steele said:
What do you mean by "directing the sql to a query"?

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


AccessMan said:
As a test I created a table from [qry Query] (using make table) and
directed
my sql to it. This worked just fine. Unfortunately, I can't employ this
solution and need to use the query. Can I force these fields to be
explicitly text somehow in the query?

AccessMan said:
The displayed sql looked fine. Here it is:

SELECT * FROM [qry Query] WHERE [X] = '9958228' AND [Y] = '23301' AND [Z]
=
'S0001'

X and Y are formatted as text in their base table, but I am directing the
sql to a query.

I'm not using Lookup. The error is attributed to the Set rs =
db.OpenRecordset(strSQL) statement.

Do I need to add a second parameter to the OpenRecordset call like
dbOpenSnapshot?

:

Okay, try:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT * FROM [qry Query] WHERE " & _
"[X] = '" & Me.MyX & "' AND " & _
"[Y] = '" & Me.MyY & "' AND " & _
"[Z] = '" & Me.MyZ & "'"
Debug.Print strSQL
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL)

Now, when the code runs (and fails), go the Immediate Window (Ctrl-G)
and
look at what's printed there for the SQL statement.

Oh, and are you sure they're all text fields? Did you perhaps use the
Lookup
Field misfeature, so that while text appears in the field, the field is
actually storing a numeric value?

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT * FROM [qry Query] WHERE " & _
"[X] = '" & Me.MyX & "' AND " & _
"[Y] = '" & Me.MyY & "' AND " & _
"[Z] = '" & Me.MyZ & "'"
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL)

All fields are string.

:

What's the actual SQL statement you're using?

What are the data types of the fields?

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


I got a error, "Too few parameters. Expected 2.".


:

You still haven't really explained what you're trying to do, but
what
about
something like:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim varField4 As Variant
Dim varField5 As Variant


strSQL = "SELECT Field4, Field5 " & _
"FROM MyTable " & _
"WHERE Field1 = " & Criteria1 & _
" AND FIeld2 = '" & Criteria2 & "' " & _
"AND Field3 = " & Format(Criteria3, "\#yyyy\-mm\-dd\#")

Set db = CurrentDb()
Set rs = db.OpenRecords(strSQL)

If rs.EOF = True Then
' No matching record found
Else
varField4 = rs!Field4
varField5 = rs!Field5
End If

Here, I've assume Field1 is numeric, Field2 is text and Field3 is
a
date
(just to show you the different syntax requirements)

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


message
I suppose I could use the do loop to find the record I'm looking
for,
but I
thought there would be a more direct way to do it
programmatically.

:

This is helpful, but I still need to position rs at a
particular
record
on
the basis of field criteria that I have. Say I have field
criteria
values of
Criteria1, Criteria2, and Criteria3 for rs fields Field1,
Field2,
and
Field3.
I need access to the field values Field4 and Field5 when the
criteria
are
met.

I read ahead about your comment about DAO. I'm OK using ADO,
but I
just
can't find decent enough resources to get a sufficient
critical
mass
of
knowledge. Any recommendations - books or sites???

Thanks!


:

You don't say exactly what you're doing, but the general
code
would
be

Dim rs As DAO.Recordset
Dim varField1 as String
Dim varField2 as String
Dim varField3 as String

Set db = CurrentDb
Set rs = db.OpenRecordset("YourQuery")

Do While Not rs.EOF

varField1 = rs!Field1
varField2 = rs!Field2
varField3 = rs!Field3

rs.MoveNext
Loop

rs.Close
Set rs = Nothing

This assigns three fields to three variables, but you could
assign
the
field
data to controls in your form. You could also include
conditional
If...Then
around the assignment lines, to pull values from a particual
record
in
YourQuery.
 
D

Douglas J. Steele

There's no need for the query to be updatable.

How did you "set the X, Y, and Z fields in [qry Query] to be plain text"?

Have you tried just using

SELECT * FROM [qry Query] WHERE [X] = 9958228 AND [Y] = 23301 AND [Z] =
'S0001'

(i.e.: removing the quotes around the values for X and Y)?

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


AccessMan said:
I mean that the object of the FROM in strSQL is a query ([qry Query]) and
not
a table.

I set the X, Y, and Z fields in [qry Query] to be plain text, and that did
not work.

To get beyond the issue, I have resorted to using a temporary table that
is
cleared and then populated by an append version of [qry Query] when
needed,
so I am operational now, although I'd still like to know how to use a
query
as the source of the recordset.

[qry Query] is not updateable. Could that be the problem?


Douglas J. Steele said:
What do you mean by "directing the sql to a query"?

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


AccessMan said:
As a test I created a table from [qry Query] (using make table) and
directed
my sql to it. This worked just fine. Unfortunately, I can't employ
this
solution and need to use the query. Can I force these fields to be
explicitly text somehow in the query?

:

The displayed sql looked fine. Here it is:

SELECT * FROM [qry Query] WHERE [X] = '9958228' AND [Y] = '23301' AND
[Z]
=
'S0001'

X and Y are formatted as text in their base table, but I am directing
the
sql to a query.

I'm not using Lookup. The error is attributed to the Set rs =
db.OpenRecordset(strSQL) statement.

Do I need to add a second parameter to the OpenRecordset call like
dbOpenSnapshot?

:

Okay, try:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT * FROM [qry Query] WHERE " & _
"[X] = '" & Me.MyX & "' AND " & _
"[Y] = '" & Me.MyY & "' AND " & _
"[Z] = '" & Me.MyZ & "'"
Debug.Print strSQL
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL)

Now, when the code runs (and fails), go the Immediate Window
(Ctrl-G)
and
look at what's printed there for the SQL statement.

Oh, and are you sure they're all text fields? Did you perhaps use
the
Lookup
Field misfeature, so that while text appears in the field, the field
is
actually storing a numeric value?

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT * FROM [qry Query] WHERE " & _
"[X] = '" & Me.MyX & "' AND " & _
"[Y] = '" & Me.MyY & "' AND " & _
"[Z] = '" & Me.MyZ & "'"
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL)

All fields are string.

:

What's the actual SQL statement you're using?

What are the data types of the fields?

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


message
I got a error, "Too few parameters. Expected 2.".


:

You still haven't really explained what you're trying to do,
but
what
about
something like:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim varField4 As Variant
Dim varField5 As Variant


strSQL = "SELECT Field4, Field5 " & _
"FROM MyTable " & _
"WHERE Field1 = " & Criteria1 & _
" AND FIeld2 = '" & Criteria2 & "' " & _
"AND Field3 = " & Format(Criteria3, "\#yyyy\-mm\-dd\#")

Set db = CurrentDb()
Set rs = db.OpenRecords(strSQL)

If rs.EOF = True Then
' No matching record found
Else
varField4 = rs!Field4
varField5 = rs!Field5
End If

Here, I've assume Field1 is numeric, Field2 is text and Field3
is
a
date
(just to show you the different syntax requirements)

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


message
I suppose I could use the do loop to find the record I'm
looking
for,
but I
thought there would be a more direct way to do it
programmatically.

:

This is helpful, but I still need to position rs at a
particular
record
on
the basis of field criteria that I have. Say I have field
criteria
values of
Criteria1, Criteria2, and Criteria3 for rs fields Field1,
Field2,
and
Field3.
I need access to the field values Field4 and Field5 when
the
criteria
are
met.

I read ahead about your comment about DAO. I'm OK using
ADO,
but I
just
can't find decent enough resources to get a sufficient
critical
mass
of
knowledge. Any recommendations - books or sites???

Thanks!


:

You don't say exactly what you're doing, but the general
code
would
be

Dim rs As DAO.Recordset
Dim varField1 as String
Dim varField2 as String
Dim varField3 as String

Set db = CurrentDb
Set rs = db.OpenRecordset("YourQuery")

Do While Not rs.EOF

varField1 = rs!Field1
varField2 = rs!Field2
varField3 = rs!Field3

rs.MoveNext
Loop

rs.Close
Set rs = Nothing

This assigns three fields to three variables, but you
could
assign
the
field
data to controls in your form. You could also include
conditional
If...Then
around the assignment lines, to pull values from a
particual
record
in
YourQuery.
 
A

AccessMan

In desperation I set the X, Y, and Z fields to plain text by opening the
query in design view and setting the Text Format property to Plain Text.
Curiously, the Format property combo box has no selections to choose from.

I tried hard-coding the criteria, both using the single quotes (for text)
and not using them (for number), and got the same error each time.

Now here's an interesting clue. I created a new query on the temporary
table that was created from the output of my original query, and I specified
this new query in my strSQL assignment statement. THIS WORKED! This new
query has the same data as the original query (and it is updateable).


Douglas J. Steele said:
There's no need for the query to be updatable.

How did you "set the X, Y, and Z fields in [qry Query] to be plain text"?

Have you tried just using

SELECT * FROM [qry Query] WHERE [X] = 9958228 AND [Y] = 23301 AND [Z] =
'S0001'

(i.e.: removing the quotes around the values for X and Y)?

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


AccessMan said:
I mean that the object of the FROM in strSQL is a query ([qry Query]) and
not
a table.

I set the X, Y, and Z fields in [qry Query] to be plain text, and that did
not work.

To get beyond the issue, I have resorted to using a temporary table that
is
cleared and then populated by an append version of [qry Query] when
needed,
so I am operational now, although I'd still like to know how to use a
query
as the source of the recordset.

[qry Query] is not updateable. Could that be the problem?


Douglas J. Steele said:
What do you mean by "directing the sql to a query"?

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


As a test I created a table from [qry Query] (using make table) and
directed
my sql to it. This worked just fine. Unfortunately, I can't employ
this
solution and need to use the query. Can I force these fields to be
explicitly text somehow in the query?

:

The displayed sql looked fine. Here it is:

SELECT * FROM [qry Query] WHERE [X] = '9958228' AND [Y] = '23301' AND
[Z]
=
'S0001'

X and Y are formatted as text in their base table, but I am directing
the
sql to a query.

I'm not using Lookup. The error is attributed to the Set rs =
db.OpenRecordset(strSQL) statement.

Do I need to add a second parameter to the OpenRecordset call like
dbOpenSnapshot?

:

Okay, try:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT * FROM [qry Query] WHERE " & _
"[X] = '" & Me.MyX & "' AND " & _
"[Y] = '" & Me.MyY & "' AND " & _
"[Z] = '" & Me.MyZ & "'"
Debug.Print strSQL
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL)

Now, when the code runs (and fails), go the Immediate Window
(Ctrl-G)
and
look at what's printed there for the SQL statement.

Oh, and are you sure they're all text fields? Did you perhaps use
the
Lookup
Field misfeature, so that while text appears in the field, the field
is
actually storing a numeric value?

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT * FROM [qry Query] WHERE " & _
"[X] = '" & Me.MyX & "' AND " & _
"[Y] = '" & Me.MyY & "' AND " & _
"[Z] = '" & Me.MyZ & "'"
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL)

All fields are string.

:

What's the actual SQL statement you're using?

What are the data types of the fields?

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


message
I got a error, "Too few parameters. Expected 2.".


:

You still haven't really explained what you're trying to do,
but
what
about
something like:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim varField4 As Variant
Dim varField5 As Variant


strSQL = "SELECT Field4, Field5 " & _
"FROM MyTable " & _
"WHERE Field1 = " & Criteria1 & _
" AND FIeld2 = '" & Criteria2 & "' " & _
"AND Field3 = " & Format(Criteria3, "\#yyyy\-mm\-dd\#")

Set db = CurrentDb()
Set rs = db.OpenRecords(strSQL)

If rs.EOF = True Then
' No matching record found
Else
varField4 = rs!Field4
varField5 = rs!Field5
End If

Here, I've assume Field1 is numeric, Field2 is text and Field3
is
a
date
(just to show you the different syntax requirements)

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


message
I suppose I could use the do loop to find the record I'm
looking
for,
but I
thought there would be a more direct way to do it
programmatically.

:

This is helpful, but I still need to position rs at a
particular
record
on
the basis of field criteria that I have. Say I have field
criteria
values of
Criteria1, Criteria2, and Criteria3 for rs fields Field1,
Field2,
and
Field3.
I need access to the field values Field4 and Field5 when
the
criteria
are
met.

I read ahead about your comment about DAO. I'm OK using
ADO,
but I
just
can't find decent enough resources to get a sufficient
critical
mass
of
knowledge. Any recommendations - books or sites???

Thanks!


:

You don't say exactly what you're doing, but the general
code
would
be

Dim rs As DAO.Recordset
Dim varField1 as String
Dim varField2 as String
Dim varField3 as String

Set db = CurrentDb
Set rs = db.OpenRecordset("YourQuery")

Do While Not rs.EOF

varField1 = rs!Field1
varField2 = rs!Field2
varField3 = rs!Field3

rs.MoveNext
Loop

rs.Close
Set rs = Nothing

This assigns three fields to three variables, but you
could
assign
the
field
data to controls in your form. You could also include
conditional
If...Then
around the assignment lines, to pull values from a
particual
record
in
YourQuery.
 
D

Douglas J. Steele

Changing the Format does absolutely nothing to the value: all it does is
change how the value is displayed.

Both Linq (who appears to have bowed out of this thread) and I have
repeatedly pointed out to you that you haven't explained what you're trying
to do. If you bothered to tell us, the answer could be much simpler...

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


AccessMan said:
In desperation I set the X, Y, and Z fields to plain text by opening the
query in design view and setting the Text Format property to Plain Text.
Curiously, the Format property combo box has no selections to choose from.

I tried hard-coding the criteria, both using the single quotes (for text)
and not using them (for number), and got the same error each time.

Now here's an interesting clue. I created a new query on the temporary
table that was created from the output of my original query, and I
specified
this new query in my strSQL assignment statement. THIS WORKED! This new
query has the same data as the original query (and it is updateable).


Douglas J. Steele said:
There's no need for the query to be updatable.

How did you "set the X, Y, and Z fields in [qry Query] to be plain text"?

Have you tried just using

SELECT * FROM [qry Query] WHERE [X] = 9958228 AND [Y] = 23301 AND [Z] =
'S0001'

(i.e.: removing the quotes around the values for X and Y)?
 
A

AccessMan

I don't know what else to provide that would be helpful. Rather than cloud
the issue with additional irrelevant details of my database, I've tried to
provide what I consider to be the essential facts which I will repeat here.

I have a form with a button control that I want to look up several field
values from a query object using criteria taken from user-entered values in
three unbound combo boxes on the form. The looked-up values are to be placed
in other unbound text boxes on the form. I achieved this functionality using
individual DLookup statements, but this proved to be too slow in execution.
So it occurred to me that there had to be a better way, one that involved
finding the correct row in the query once (instead of n separate times) and
that then allowed access to the individual fields on that row. Your
suggestion of using a DAO.Recordset object was just what I was looking for.
For some reason, the OpenRecordSet statement doesn't like my particular query
being the the sql FROM argument. It does work if I use a table created from
my query (via make table), or if I use a query on that table.

Like I mentioned, I'm up and running with an acceptable workaround (use of
the temp table that I need to clear and repopulate from time to time), so
your advice has already been very helpful and is highly appreciated. I don't
know what else I could pass on that would be helpful. Certainly sending you
my problematic query is impractical since I'd have to include many other
queries and tables that it draws on.

Unless I've mentioned something new here that is a clue, I'm OK considering
this closed and I thank you again for your guidance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top