473,794 Members | 2,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Basic DAO.Recordset question

For some reason this does not seem to be working...
Am I missing something basic?

Dim rst As DAO.Recordset
Dim db As DAO.Database
Set db = CurrentDb
Set rst = db.OpenRecordse t("qryEmailS" ) ' <= can I use query here?

' qryEmailS contains only one column - "strS"
' I want to loop through each item in the rst
' must I use rst here? how about Array?

Do Until rst.EOF
Debug.Print rst!strS
rst.MoveNext
Loop
Nov 12 '05
14 4046
the missing parameter is Forms!frmMain!f rm0.Form!Entity _ID
if you were to close the form 'frmMain' and you were to run the query,
you'd get an input box prompting for 'entity id'

so you have 2 choices...
1) make sure frmMain is open with a valid value in entity_id

2) don't open the form and use qrydef to set the parameter
Dim db As Database
Dim rs As Recordset
Dim qdf As QueryDef
dim lngEntityId as long ' the entity ID that you want to search
for

Set db = CurrentDb
Set qdf = db.QueryDefs("q ryEmailS")
qdf.Parameters( 0) = lngEntityId
Set rs = qdf.OpenRecords et(dbOpenDynase t)
Debug.Print rs!strS

if you want to search for all entity id's, make a new query that
doesn't
have a WHERE clause
"deko" <dj****@hotmail .com> wrote in message news:<ha******* **********@news svr25.news.prod igy.com>...
It looks good to me. What is it doing (or not doing)? Are you getting an
error? Do you have a reference to DAO set?


The error I'm getting is:

Error Number 3061: Too few parameters. Expected 1.

This is qryEmailS:

SELECT tblEntity.First Name as strS FROM tblEntity
WHERE ((tblEntity.Ent ity_ID)=Forms!f rmMain!frm0.For m!Entity_ID)
AND tblEntity.First Name Is Not Null
UNION SELECT tblEntity.LastN ame FROM tblEntity
WHERE ((tblEntity.Ent ity_ID)=Forms!f rmMain!frm0.For m!Entity_ID)
AND tblEntity.LastN ame Is Not Null
UNION SELECT tblEntity.Compa ny FROM tblEntity
WHERE ((tblEntity.Ent ity_ID)=Forms!f rmMain!frm0.For m!Entity_ID)
AND tblEntity.Compa ny Is Not Null
UNION SELECT tblEmail.EmailA ddress FROM tblEmail
WHERE ((tblEmail.Enti ty_ID)=Forms!fr mMain!frm0.Form !Entity_ID)
AND tblEmail.EmailA ddress Is Not Null;

What I'm trying to do is pretty simple - I need to check if any of the
results of qryEmailS are in a particular string:

Dim rst As DAO.Recordset
Dim db As DAO.Database
Set db = CurrentDb
Set rst = db.OpenRecordse t("qryEmailS" )

For Each olmi In olfsm.Items
Do Until rst.EOF
If (InStr(olmi.To, rst!strS)) > 0 Then
'do something here
End If
rst.MoveNext
Loop
Next

I need some way to iterate through each item in the query, and I'm thinking
a recordset is the way to go, unless there is some way to get the query
results into an array. I'm wondering if I'm getting that runtime error
because the query is a union query...

Thanks!

Nov 12 '05 #11
le*********@nat pro.com (Roger) wrote in
<8c************ **************@ posting.google. com>:
the missing parameter is Forms!frmMain!f rm0.Form!Entity _ID
if you were to close the form 'frmMain' and you were to run the
query, you'd get an input box prompting for 'entity id'
It's only a missing parameter because DAO does not use the Access
Expression Service to resolve references to Access objects.
Remember, though a form is created by Access and stored in a Jet
database, it is not something that is structurally known or
understood by Jet. DAO is an interface to Jet that Access can use.
When you mix in Access objects in a DAO context, it is simply
unknown.
so you have 2 choices...
1) make sure frmMain is open with a valid value in entity_id
That won't matter one iota -- DAO won't know what it is whether the
form is open or not.
2) don't open the form and use qrydef to set the parameter
Dim db As Database
Dim rs As Recordset
Dim qdf As QueryDef
dim lngEntityId as long ' the entity ID that you want to
search
for

Set db = CurrentDb
Set qdf = db.QueryDefs("q ryEmailS")
qdf.Parameters( 0) = lngEntityId
Set rs = qdf.OpenRecords et(dbOpenDynase t)
Debug.Print rs!strS
This is one way to accomplish, yes (though you'd want to have
cleanup code for the querydef, recordset and database variables).
if you want to search for all entity id's, make a new query
that
doesn't
have a WHERE clause


There are two approaches to this other than setting the parameter
of the querydef. One would be to leave the query as it stands and
instead define the SQL for your recordset inline:

This is qryEmailS:

Dim lngEntityID As Long
Dim strSQL As String
Dim db As DAO.Database
Dim rst As DAO.Recordset

lngEntityID=For ms!frmMain!frm0 .Form!Entity_ID
strSQL = "SELECT tblEntity.First Name as strS FROM tblEntity"
strSQL = strSQL & " WHERE tblEntity.Entit y_ID=" & lngEntityID
strSQL = strSQL & " AND tblEntity.First Name Is Not Null"
strSQL = strSQL & " UNION SELECT tblEntity.LastN ame FROM
tblEntity" strSQL = strSQL & " WHERE tblEntity.Entit y_ID=" &
lngEntityID" strSQL = strSQL & " AND tblEntity.LastN ame Is Not
Null" strSQL = strSQL & " UNION SELECT tblEntity.Compa ny FROM
tblEntity" strSQL = strSQL & " WHERE tblEntity.Entit y_ID=" &
lngEntityID" strSQL = strSQL & " AND tblEntity.Compa ny Is Not
Null" strSQL = strSQL & " UNION SELECT tblEmail.EmailA ddress"
strSQL = strSQL & " FROM tblEmail"
strSQL = strSQL & " WHERE tblEmail.Entity _ID=" & lngEntityID
strSQL = strSQL & " AND tblEmail.EmailA ddress Is Not Null;"

Set db = CurrentDB()
Set rst = db.OpenRecordse t(strSQL)

Then do your thing.

Parenthetically , I'm not entirely sure I understand why you'd want
to do this kind of thing. Fundamentally, whenever you do a UNION of
several fields from a single table, it's a pointer that there may
be something wrong with your structure. In your case, do you need a
result set that has FirstName, LastName, Company and EMailAddress
all in one column, or do you just want the name, company and email
of the entity? If the latter, then you don't need a UNION with a
loop at all, just a join between tblEntity and tblEMail (an inner
join, because a query for constructing email addresses isn't much
use if it includes people who have none!) and then concatenate the
name, company and email from the result.

So, your query could be something like this:

SELECT tblEntity.First Name, tblEntity.LastN ame,
tblEntity.Compa ny, tblEmail.EmailA ddress
FROM tblEntity INNER JOIN tblEmail
ON tblEntity.Entit yID = tblEmail.Entity ID;

If you really want to eliminate Null records (not sure why you'd
have any records where all four would be Null), you can add this
WHERE clause:

WHERE (((tblEntity.Fi rstName) Is Not Null)
OR ((tblEntity.Las tName) Is Not Null)
OR ((tblEntity.Com pany) Is Not Null)
OR ((tblEmail.Emai lAddress) Is Not Null))

The last is only necessary if you're afraid that tblEmail could
have a record with a blank email address. Actually, in that
instance, you'd want this, instead:

WHERE (((tblEntity.Fi rstName) Is Not Null)
OR ((tblEntity.Las tName) Is Not Null)
OR ((tblEntity.Com pany) Is Not Null))
AND ((tblEmail.Emai lAddress) Is Not Null)

The concatenation of the results could be done either with IF/ELSE
blocks or you could do it with a loop through the Fields collection
of the recordset. I'd not recommend the latter, as you need to
treat different fields in different ways, assuming you're going for
a result like:

FirstName LastName <em***@address. com>

Also, you'd want to skip the company name if there's a name, unless
you want:

FirstName LastName, Company <em***@address. com>

And if you want that, you'd have two of your four fields treated
differently, so that a loop would make no sense.

Now, assuming that you actually do need the UNION to get all the
data into a single column (though I'm not convinced that you do),
the original SQL will get you what you want. However, it means you
now have to maintain both the Query and the SQL string in your
code. So, you might want to fix up the Query to be usable in DAO.
One option is to set the parameters. However, you have to remember
to do that each time you use it. Another option is, as Dan Morgan
suggested, to use a function in your saved Query that returns the
value you need. DAO can handle these user-defined functions (this
is one of the things that makes DAO rather unsafe, and this has
been patched in Jet 4 SP8). Rather than having a specific function,
I would suggest a generic one. So, we would take Dan's code:

Function GetValue
GetValue = Forms!frmMain!f rm0.Form!Entity _ID
End Function

First off, it's a very bad idea to not explicitly declare the
return type of a function, so it really should be:

Function GetValue As Variant
GetValue = Forms!frmMain!f rm0.Form!Entity _ID
End Function

Now, this is actually 100% equivalent, as undeclared function
return types will implicitly be variants, but it's much better
code, as you can tell at a glance what the code returns (you also
get the return type in Intellisense), but also because you are not
depending on implicit behavior that might change when run under
different variants of VB/A.

Now, to make it generic, you need to pass it the form and control:

Function GetValue (ByVal strForm As String, _
ByVal strControl As String) As Variant
If IsLoaded(strFor m) Then
GetValue = Forms(strForm). Controls(strCon trol)
End If
End Function

You'd probably want an error handler in case the control doesn't
exist.

In your query, however, this wouldn't work, because you're
referring to a control on a subform. I've never actually
encountered this kind of issue, so my code for this doesn't account
for it. The simplest way I can think of is to add an optional
argument for the subform:

Function GetValue (ByVal strForm As String, _
ByVal strControl As String, _
Optional strSubForm as String) As Variant
If IsLoaded(strFor m) Then
If Len(strSubForm) = 0 Then
GetValue = Forms(strForm). Controls(strCon trol)
Else
GetValue = _
Forms(strForm). Controls(strSub Form).Form.Cont rols(strControl )
End If
End If
End Function

I wasn't entirely sure this kind of thing works, but, yes, it does
work. It's kind of messy and probably not preferable to setting the
parameters, but it gives you an idea of something you can do to get
around this kind of problem.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 12 '05 #12
Hello, and thanks for all the replys.

This seems to be working:

Set qdf = db.QueryDefs("q ryEmailAddresse s")
For Each prm In qdf.Parameters
prm.Value = Eval(prm.Name)
Next prm
Set rst = qdf.OpenRecords et(dbOpenSnapsh ot) 'all I need is a read only rst,
so I figure dbOpenSnalshot is quicker

Here is query EmailAddresses:

SELECT tblEmail.EmailA ddress
FROM tblEmail
WHERE ((tblEmail.Enti ty_ID)=Forms!fr mMain!frm0.Form !Entity_ID) And
tblEmail.EmailA ddress Is Not Null;
That Union query I was using before was a mess, to be sure. Nevertheless,
the problem was not with the query as some have suggested. It seems the
problem is simply this: you cannot build a DAO Recordset from a query with
parameters referencing an Access form. I've discovered this by trial and
error, but a more detailed explanation is at
http://www.mvps.org/access/queries/qry0013.htm - in short, it has something
to do with the Access Expression Service not resolving references when using
VBA to open a recordset.

Becuase the query parameter in question is on an open form, I can use the
method above to supply the required parameter value.

I have not explored the option of using a Function for returning the value I
need, but David Fenton's post does a great job of explaining how to do
that - thanks, David.
Nov 12 '05 #13
On Thu, 08 Jan 2004 21:24:17 GMT, "deko" <dj****@hotmail .com> wrote:
For some reason this does not seem to be working...
Am I missing something basic?

Dim rst As DAO.Recordset
Dim db As DAO.Database
Set db = CurrentDb
Set rst = db.OpenRecordse t("qryEmailS" ) ' <= can I use query here?

This needs to include the record type parameter, such as:

Set rst = db.OpenRecordse t("qryEmailS" , dbOpenDynaset)
Furthermore, in this statement, I believe the string ""qryEmailS "
needs to be a valid SQL statement.
Nov 12 '05 #14
<WillWeGetOurFr eedomBack> wrote in message
news:n2******** *************** *********@4ax.c om...
On Thu, 08 Jan 2004 21:24:17 GMT, "deko" <dj****@hotmail .com> wrote:
For some reason this does not seem to be working...
Am I missing something basic?

Dim rst As DAO.Recordset
Dim db As DAO.Database
Set db = CurrentDb
Set rst = db.OpenRecordse t("qryEmailS" ) ' <= can I use query here?

This needs to include the record type parameter, such as:

Set rst = db.OpenRecordse t("qryEmailS" , dbOpenDynaset)
Furthermore, in this statement, I believe the string ""qryEmailS "
needs to be a valid SQL statement.


It can be a SQL statement or the name of a query or table.
--
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com
Nov 12 '05 #15

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

Similar topics

4
3094
by: Tom | last post by:
I want to open a recordset object on an .asp page. When I open the recordset I would like to use a stored procedure that expects a parameter to be passed for the stored procedure. I will then use the recordset to loop thru the recordset, update values from the recordset and then update the database by passing parmeters to another stored procedure. I would like to use the recordset object but can it be used to pass a parameter to a stored...
2
1460
by: JP SIngh | last post by:
Hi All I have threee tables like the one below and i would like to ask if it is possible to display the data from all three tables using a single recordset in ASP. TABLE - tblList id - Number ListName - Text
4
1861
by: Volumstein | last post by:
Hello, I am spoiled coming from programming in word and excel which offer the "record macro" tool that eliminated most basic syntax confusions that I had. unfortunately msaccess doesn't offer that option, and my luck in the groups has yielded little results for manipulating tables via vb code in a form. Could anyone illustrate a few examples for me? 1.) How would I Modify a specific cell in a table called "Mytable?"
1
1881
by: SAKTHIVEL | last post by:
Hi, Im developing applications using Visual Basic and MS-Access 2003. Union query solves my specific problems. But I unable to call this query from visual basic form like other queries. The query follows: select PSCLNo,Fyearid,CompId, "CLNO" as TrType from VwCLothsrNo UNION select PSCLNo,Fyearid,CompId, "PSNO" as TrType from VwPSsrNo; Visual Basic Form Statement:
10
6696
by: ZaphodBBB | last post by:
Hi again Can I create a recordset (which I find a lot easier to manipulate) from an SQL statement? (instead of a querydef). I have a subform that has reviews of job (service checks) on it. Each job on the subform (datasheet view) has the date it was done, serial number, customer name, department, customer_id(invisible textbox), job number. I want to create a recordset that I can use for the basis of reports. Say I click on a job in...
2
2626
by: trezraven | last post by:
I am creating a template in Word 2007 using Visual Basic Editor. I am pulling information from an Access database. I know this is probably a stupid question, but for the life of me I can't figure out how I get my data to start on a new page and keep the formatting from the first page once a new record is encountered. I have attached a copy of my code. Any help will be greatly appreciated!!! Public blnCancelled As Boolean Private Sub...
7
2283
by: tdr | last post by:
I need to compare table 1 to table 2 and if the row/recordset in table 1 is different from table 2, write the entire row/recordset from table 1 to table 3. I can read an entire row/recordset from table 1 into a recordset using the following strSQL_in = "select * from table1" ( and i can refer to the columns as needed)
6
2565
by: Vince | last post by:
Hello all, I am using Visual Basic to open a saved query and then save information in the query to an array for later use. The problem is that the same query shows different results when opened directly vs. when opened by Visual Basic. It is as if Visual Basic is not letting the query fully evaluate before processing records. The query is a subtotal query that contains several criteria set up as "where" in the group-by box. Most of...
4
2718
by: =?Utf-8?B?R1ROMTcwNzc3?= | last post by:
Hi Guys, thanks for your help yesterday, I've got one more question, then I think I'm done for now,... Is it possible to insert recordset data in a javascript, for instance I have a javascript code that calculates the total price, depending on number of units, currently what the code does is set the price like so - if qty 1 then £99+VAT if qty equall to or greater than 2 and equall to or less than 9 then price =
0
9671
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10433
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10161
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9035
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2919
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.