473,625 Members | 2,632 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Return field values from a query using a Recordset in VBA

I am trying to extract the values from a query using VBA.
Specifically, I want to concatenate all the values of the "rosEmail"
field from query "qselRosterEmai lList" into one string (strEmails). I
can get one record's result by using the DLookup fuction, of course,
but I want to get every record's value. To do this, I believe I need
to build a recordset, but I do not know how.

I've read about doing it using DAO in Access 97, but I am using Access
2002. Also, I do not want to register any extra libraries, since I'll
be sharing the Access database with others and do not want those users
to have to register libraries as well.

The final result should be something like:
strEmails = "pe*****@email. com, pe*****@email.c om, pe*****@email.c om,
pe*****@email.c om"

Thanks for your help,
David

Nov 13 '05 #1
19 59950
Unfortunately, this code does not work. I get an error saying
"User-defined type not defined" and it doesn't go farther than the
first line of code. Although I'm not sure, I think this is because
it's DAO and not ADO. You wrote "Oh, so convert it to ADO. Or use late
binding" - can you elaborate on how to do this? I don't know how to
return the Recordset in ADO.

Also, where do I define the query that feeds this data? (strQuery =
qselRosterEmail List or something like that)

Thanks

Nov 13 '05 #2

bdt513 wrote:
I am trying to extract the values from a query using VBA.
Specifically, I want to concatenate all the values of the "rosEmail"
field from query "qselRosterEmai lList" into one string (strEmails). I can get one record's result by using the DLookup fuction, of course,
but I want to get every record's value. To do this, I believe I need
to build a recordset, but I do not know how.

I've read about doing it using DAO in Access 97, but I am using Access 2002. Also, I do not want to register any extra libraries, since I'll be sharing the Access database with others and do not want those users to have to register libraries as well.

The final result should be something like:
strEmails = "pe*****@email. com, pe*****@email.c om, pe*****@email.c om,
pe*****@email.c om"

Thanks for your help,
David


Umm... where's the code?
You basically declare a string variable and then inside looping through
the records, you append each value to the string.

This is crap, but it works... assumes you get your info from a query...
Option Compare Database

Public Function AddressList(ByV al strQuery As String) As String
Dim db As DAO.Database
Dim qd As DAO.QueryDef
Dim rs As DAO.Recordset
Dim strList As String

Set db = DBEngine(0)(0)
Set qd = db.QueryDefs(st rQuery)
Set rs = qd.OpenRecordse t

Do Until rs.EOF
strList = strList & ", " & rs.Fields(2).Va lue
rs.MoveNext
Loop

AddressList = Right$(strList, Len(strList) - 2)

rs.Close
Set rs = Nothing
Set qd = Nothing
Set db = Nothing
End Function

query:
SELECT tblPeople.First Name, tblPeople.LastN ame, Left$([Firstname],1) &
[LastName] & "@mail.net" AS Expr1
FROM tblPeople;
Oh, so convert it to ADO. Or use late binding.

Nov 13 '05 #3
Here is the Recordset code I have now, and this produces an error at
the rs.Open line. "rosEmail" is the field I want and
"qselRosterEmai lList" is the query from which I want to read.

Private Sub cmdGenerateEmai lList_Click()

Dim temp, list As String
Dim rs As ADODB.Recordset

Set rs = New ADODB.Recordset
rs.Open "qselRosterEmai lList"

Do While Not rs.EOF
temp = rs!rosEmail
list = temp & ", "
rs.MoveNext
Loop

rs.Close

End Sub

Nov 13 '05 #4
I think rs.Open "qselRosterEmai lList" needs a connection.
I often use the connection of the current project

rs.Open "qselRosterEmai lList", CurrentProject. Connection

And you might want to add further arguments after the conenction about the
way the recordset must be opened.

Kees de Boer

bdt513 wrote:
Here is the Recordset code I have now, and this produces an error at
the rs.Open line. "rosEmail" is the field I want and
"qselRosterEmai lList" is the query from which I want to read.

Private Sub cmdGenerateEmai lList_Click()

Dim temp, list As String
Dim rs As ADODB.Recordset

Set rs = New ADODB.Recordset
rs.Open "qselRosterEmai lList"

Do While Not rs.EOF
temp = rs!rosEmail
list = temp & ", "
rs.MoveNext
Loop

rs.Close

End Sub

Nov 13 '05 #5
C.P.J. de Boer wrote:

Did you work in The Hague in 1986?
--
Bas Cost Budde, Holland
http://www.heuveltop.nl/BasCB/msac_index.html
I prefer human mail above automated so in my address
replace the queue with a tea
Nov 13 '05 #6
I've modified the code, but I'm now getting a different error. For
some reason it does not like using a query as the Recordset on the
rs.Open command. When I use a table, I'm fine, but I need to pull this
data from a query. What do I need to do in order to allow me to use a
query to feed this Recordset? (I receive the error message, "No value
given for one or more parameters")

Here's the code as it stands now...

Sub EmailList()

Dim rs As ADODB.Recordset
Dim strEmails As String

Set rs = New ADODB.Recordset

rs.Open "Select * FROM qselRosterEmail List;", CurrentProject. Connection

While Not rs.EOF
strEmails = strEmails & ", " & rs.Fields(0).Va lue
rs.MoveNext
Wend
rs.Close
Set rs = Nothing

End Sub

Nov 13 '05 #7
> rs.Open "Select * FROM qselRosterEmail List", CurrentProject. Connection

As counterintuitiv e as it sounds, you cannot actually use
CurrentProject. Connection in this manner; you must decalre and use a
Connection object (and no, I don't know why):

Dim cnxn As ADODB.Connectio n
Dim rs As ADODB.Recordset
Set cnxn = CurrentProject. Connection
Set rs = New ADODB.Recordset

rs.Open "Select * FROM qselRosterEmail List", cnxn

Your code should run as above, however, I advise you to get in the habit of
specifying the parameters after the Connection, otherwise, Access uses the
defaults for the first two parameters, then figures the final one out on its
own (which slows down your code).

Filling in the default values, you have opened up this type of Recordset
/cursor:

rs.Open "Select * FROM qselRosterEmail List", cnxn, adOpenForwardOn ly,
adLockReadOnly

This is fine in your situation, but you will need other types of cursors
(Keyset, LockOptimistic)

Also, you should specify the final parameter, in order to speed up the code:
adCmdText for queries, adCmdTableDirec t for tables, or adCmdStoredProc for
Stored Procedures

rs.Open "Select * FROM qselRosterEmail List", cnxn, adOpenForwardOn ly,
adLockReadOnly, adCmdText
Darryl Kerkeslager
"bdt513" <BD****@gmail.c om> wrote:
I've modified the code, but I'm now getting a different error. For
some reason it does not like using a query as the Recordset on the
rs.Open command. When I use a table, I'm fine, but I need to pull this
data from a query. What do I need to do in order to allow me to use a
query to feed this Recordset? (I receive the error message, "No value
given for one or more parameters")

Here's the code as it stands now...

Sub EmailList()

Dim rs As ADODB.Recordset
Dim strEmails As String

Set rs = New ADODB.Recordset

rs.Open "Select * FROM qselRosterEmail List;", CurrentProject. Connection

While Not rs.EOF
strEmails = strEmails & ", " & rs.Fields(0).Va lue
rs.MoveNext
Wend
rs.Close
Set rs = Nothing

End Sub

Nov 13 '05 #8
That was a very helpful post, and I appreciate the advice. Still,
however, the code does not work. I receive the same error on the
rs.Open line:
"No value given for one or more parameters"

When I run the same code but use a table instead of a query, the code
works. Short of using a make table, how can I use the query
(qselRosterEmai lList) to populate the Recordset? [The query does work
okay on its own. It opens properly and returns the right information]

Here's the code that's in my module:
-----------------------------
Sub EmailList()

Dim cnxn As ADODB.Connectio n
Dim rs As ADODB.Recordset
Set cnxn = CurrentProject. Connection
Set rs = New ADODB.Recordset

rs.Open "Select * FROM qselRosterEmail List", cnxn, adOpenForwardOn ly,
adLockOptimisti c, adCmdText

[...other stuff...]

End Sub
-----------------------------

When I use the follow line, which looks to a table instead of a query,
I have no problem:
rs.Open "Select * FROM tblRoster", cnxn, adOpenForwardOn ly,
adLockOptimisti c, adCmdText

Thanks again for you help,
David

Nov 13 '05 #9
Unfortunately, this code does not work. I get an error saying
"User-defined type not defined" and it doesn't go farther than the
first line of code. Although I'm not sure, I think this is because
it's DAO and not ADO. You wrote "Oh, so convert it to ADO. Or use late
binding" - can you elaborate on how to do this? I don't know how to
return the Recordset in ADO.

Also, where do I define the query that feeds this data? (strQuery =
qselRosterEmail List or something like that)

Thanks

Nov 13 '05 #10

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

Similar topics

1
682
by: bdt513 | last post by:
I am trying to extract the values from a query using VBA. Specifically, I want to concatenate all the values of the "rosEmail" field from query "qselRosterEmailList" into one string (strEmails). I can get one record's result by using the DLookup fuction, of course, but I want to get every record's value. To do this, I believe I need to build a recordset, but I do not know how. I've read about doing it using DAO in Access 97, but I am...
4
15445
by: Kannan s | last post by:
Dear Sir, Sub: help requred to build an Update Query using if or iif I am having two tables with the following fields I wish to create a single update query in MS Access Table1: code, qty_x, qty_y, qty_z, qty_o ( code:ch, qty_x,qty_y, qty_z, qty_o : n) Table2: code, qty, xyz ( code:ch, qty: n, xyz=ch) Relationship created between the two tables on code field
5
2336
by: sklett | last post by:
I'm not real experienced with asp.net so this may be obvious. I've got a situation where some of my client side javascript is causing my posted form's controls to lose their values. In other words, I have a server control TextBox, I enter some text, my client side scripts do some stuff and in the Page_Load method after submitting the form the value for my TextBox is empty. If the javascipt doesn't run, the values are fine. I've looked...
2
3713
by: midlothian | last post by:
Hello Trying to update a memo field in an update query using the Replace function, and am getting a type conversion error. Here is my parameter: CStr(Replace(CStr(),"$",Chr$(13) & Chr$(10))) This fails. However, this also fails: CStr()
6
3034
markmcgookin
by: markmcgookin | last post by:
Hi Folks, I am running a simple query using VB (This isnt a VB Question, dont worry!) on SQL Server Compact. I have the query below being created, and then added to if a flower location doesn't equal nothing (if it is nothing, its returning all the values) now, when this query runs and I select a flower location, i.e. L3 I want it to return all values where the flower location is equal to L3 and those where it is equal to L5. statement...
9
76059
ADezii
by: ADezii | last post by:
There are basically 4 Methods that you can use to access the values of Fields in Recordsets. For the purposes of this demonstration, I'll be using a Table called tblEmployees, a Field named , and a DAO Recordset although these Methods are equally applicable to ADO Recordsets. The following code will print all the Last Name () values in the Employees (tblEmployees) Table using all 4 of these Methods. Dim MyDB As DAO.Database, MyRS As...
1
2069
by: vbarookie | last post by:
I was able to consolidate about 100 csv files into one master table which I then imported into access (2003). The problem is that the csv files contained different layouts. Currently the data is scrambled meaningless junk. I need to take the consolidated table and put the field values in order according the the field names by design. I need a query or code that will search across 50 or so colums, look for the correct data if it exists...
1
3488
by: afromanam | last post by:
Hello, Good morning, I have a question, hope someone can help me. I have a table with say, 5 columns, each named A,B,C,D,E The table was imported from Excel, so picture please this:
2
2275
by: tomric | last post by:
I have a question on passing data to the criteria of a parameter of a parameter query using VBA. I have a query set up that has ten different fields in it. One is field named “status”, this field contains numbers from 0 to 9 depending on the type of defect a particular part was rejected for. I need to filter the data from the query based on a number or numbers in the status field. On the form that calls this query is a series of 7 check...
2
1731
by: Ken Jones | last post by:
Table URL_3 consist of the following 2 columns of information being Record No and URL No URL 1 http:/publishing/45/100006_f.SAL_Local.html 2 http:/publishing/45/100006_f.SAL_Area.html I need a substring query that will return the values of Local and Area from column URL of Table URL_3 into an additional column of information. I was looking for the solution, shown below, that tried to delimit the desired...
0
8182
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8635
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
8352
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
8494
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...
1
6115
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
4085
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2614
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
2
1496
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.