473,783 Members | 2,287 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 #1
14 4044
"deko" <dj****@hotmail .com> wrote in message
news:5U******** ********@newssv r27.news.prodig y.com...
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


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?
--
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com
Nov 12 '05 #2
> 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 #3
TC
Your code is fine. If you are getting error 3061 (Too few parameters.
Expected 1), the query requires a parameter (which it apparanetly doesn't),
or it has a syntax error. Open the form to which the query refers, enter the
exact same values that you used before, then run the query manually (not
from code) & see what happens. I bet it blows up!

HTH,
TC
"deko" <dj****@hotmail .com> wrote in message
news:5U******** ********@newssv r27.news.prodig y.com...
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 #4
"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.

First things first - you have to get the recordset to open. To do
that you have to deal with the above error. Then you can loop through
the recordset to your heart's content.

So...
<SNIP>
With Access queries that contain paramters that are all references to
Access controls on open forms, you can simulate the expression service
that Access provides when the queries are run through the user
interface, as follows:

Set db = CurrentDb
Set qdf = db.QueryDefs("M yQuery")

For Each prm In qdf.Parameters
prm.Value = Eval(prm.Name)
Next prm

Set rs = qdf.OpenRecords et(dbOpenDynase t)
' or qdf.Execute dbFailOnError

</SNIP>
the whole article is here:
http://www.mvps.org/access/queries/qry0013.htm
Nov 12 '05 #5
> the whole article is here:
http://www.mvps.org/access/queries/qry0013.htm


I looked at that site - that is definitely my problem. What I've done in
the mean time is use a MakeTable query to dump the query results into a
table, then define the recordset based on the table. I'm not sure how much
of a performance hit I'm taking, but it seems to be working. See complete
code below.

What this sub does is loop through every message in the Outlook Sent Items
folder and populates a table with DateSent, Subject and Recipient of every
message that was sent to each contact in the mdb (there's a button on the
mdb's Contacts form to show the results - ad hoc in a popup form -
individually for each contact. It would take too long to do every contact
at once).

The joker in the pack is the limitation of the Outlook Object Model: there
is no way to always get a fully qualified email address out of the MailItem
property. It depends on how the contact is saved in the Outlook Contacts
folder - if the "DisplayAs" field contains the email address, you can
usually get the email address out of the "MailItem.T o" property for all the
messages sent to that contact; but if you've just replied to a message,
sometimes "MailItem.T o" has only the contact's name, e.g. 'John Smith' -
this means my code would not find that message - because I am looking for
the contact's email address.

Public Sub SentMessages()
On Error GoTo HandleErr
Dim rst, rste As DAO.Recordset
Dim db As DAO.Database
Dim olns As Outlook.Namespa ce
Dim ola As New Outlook.Applica tion
Dim olfsm As Outlook.MAPIFol der
Dim olmi As Outlook.MailIte m
Dim strEmail, j, i As String
DoCmd.Hourglass True
DoCmd.SetWarnin gs False
DoCmd.OpenQuery "qryEmailSentDe lete"
DoCmd.OpenQuery "qryEmailEntity " 'qdf here...
DoCmd.SetWarnin gs True
Set db = CurrentDb
Set olns = ola.GetNamespac e("MAPI")
Set olfsm = olns.GetDefault Folder(olFolder SentMail)
Set rst = db.OpenRecordse t("tblEmailSent ")
Set rste = db.OpenRecordse t("tblEmailEnti ty") 'use qdf here to avoid
table creation?
Do Until rste.EOF
If InStr(1, rste!s, "#", vbTextCompare) Then 'ignore comments next
to email address
j = InStr(1, rste!s, "#", vbTextCompare)
i = "1"
Else
j = Nz(Len(rste!s), 0)
i = "0"
End If
strEmail = Left(rste!s, j - i)
'Debug.Print "strEmail = " & strEmail
For Each olmi In olfsm.Items
DoEvents
'Debug.Print "unmatched olmt.to = " & olmi.To
If (InStr(olmi.To, strEmail)) > 0 Then
rst.AddNew
rst!Sent = (CDate(olmi.Sen tOn))
rst!Subject = olmi.Subject
rst!Recipient = olmi.To
'Debug.Print "matched olmi.To = " & olmi.To
rst.Update
End If
Next
rste.MoveNext
Loop
Exit_here:
DoCmd.Hourglass False
rst.Close
rste.Close
Set olns = Nothing
Set olfsm = Nothing
Set rst = Nothing
Set rste = Nothing
Set db = Nothing
Exit Sub
HandleErr:
Select Case Err.Number
Case Else
modHandler.LogE rr ("modOutlook(Se ntMessages)")
Resume Exit_here
End Select
End Sub
Nov 12 '05 #6
The problem is the Forms!frmMain!f rm0.Form!Entity _ID. Write a function
to get the value of Forms!frmMain!f rm0.Form!Entity _ID such as:

function GetValue

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

end function

change your query to reference the function rather than
Forms!frmMain!f rm0.Form!Entity _ID.

I think it has something to do with DAO not knowing about Access
forms?
"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 #7
> The problem is the Forms!frmMain!f rm0.Form!Entity _ID. Write a function
to get the value of Forms!frmMain!f rm0.Form!Entity _ID such as:

function GetValue

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

end function


hmmm... that sounds interesting. but how do I pass that to a query? I've
discovered that any recordset operation that uses a query that has a
reference to a form fails - the work around I've been using is to save the
form value I'm interested in (value3) to a table (tblC), then use a nested
query:

SELECT tblA.value1 FROM tblA WHERE tblA.value2 IN (Select value3 FROM tblC)

This mean the added overhead of writing the value to a table everytime I
need a recordset. If I could find a better way, that would be great...

I've been looking at this site:

http://www.mvps.org/access/queries/qry0013.htm

but have not figured out how to use QueryDefs yet...
Nov 12 '05 #8
TC
Not true. Queries can certianly refer to form variables (although
personally, I think that ids a terrible idea from a software design
perspective).

deko, have you tried what I suggested?

TC
"Dan Morgan" <us****@yahoo.c om> wrote in message
news:fe******** *************** ***@posting.goo gle.com...
The problem is the Forms!frmMain!f rm0.Form!Entity _ID. Write a function
to get the value of Forms!frmMain!f rm0.Form!Entity _ID such as:

function GetValue

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

end function

change your query to reference the function rather than
Forms!frmMain!f rm0.Form!Entity _ID.

I think it has something to do with DAO not knowing about Access
forms?
"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 #9
In the query builder criteria use the function as the criteria rather
than the form control. You will then be able to use the query as a
recordset in DAO.

With regard to another posting. Yes form controls can be used in
queries, but DAO gets cranky when you open a recordset based on the
query.

"deko" <dj****@hotmail .com> wrote in message news:<iH******* **********@news svr25.news.prod igy.com>...
The problem is the Forms!frmMain!f rm0.Form!Entity _ID. Write a function
to get the value of Forms!frmMain!f rm0.Form!Entity _ID such as:

function GetValue

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

end function


hmmm... that sounds interesting. but how do I pass that to a query? I've
discovered that any recordset operation that uses a query that has a
reference to a form fails - the work around I've been using is to save the
form value I'm interested in (value3) to a table (tblC), then use a nested
query:

SELECT tblA.value1 FROM tblA WHERE tblA.value2 IN (Select value3 FROM tblC)

This mean the added overhead of writing the value to a table everytime I
need a recordset. If I could find a better way, that would be great...

I've been looking at this site:

http://www.mvps.org/access/queries/qry0013.htm

but have not figured out how to use QueryDefs yet...

Nov 12 '05 #10

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

Similar topics

4
3092
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
1880
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
2282
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
2563
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
2716
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
10313
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...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10081
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
9946
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
8968
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...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2875
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.