473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Get Recordset from Query

Good afternoon,

Can someone please tell me how I can return records from a query. I have text fields that I need to obtain, and have those necessary records sent to MS Word. I need to return the data that is in MainSectionLabel, MainLabel and SummaryTextBox.


Here is what I have so far:
Expand|Select|Wrap|Line Numbers
  1. Public Function gettbldata()
  2.  
  3. Dim strSQL As String
  4.  
  5. Set objRecordset = CreateObject("ADODB.recordset")
  6.  
  7. strSQL = "SELECT SSR2_Table.ID, SSR2_Table.DataReportID, " & _
  8.          "SSR2_Table.MainSectionID, SSR2_Table.MainSectionLabel, " & _
  9.          "SSR2_Table.MainLabel, SSR2_Table.SummaryTextBox, " & _
  10.          "SSR2_Table.[Month/Year]" & _
  11.          "FROM (SSR_DataReportName INNER JOIN SSR_DataReportLabels " & _
  12.          "  ON SSR_DataReportName.[DataReport ID] = SSR_DataReportLabels.DataReportID) " & _
  13.          "  INNER JOIN SSR2_Table " & _
  14.          "  ON SSR_DataReportLabels.DataReportID = SSR2_Table.DataReportID" & _
  15.          "WHERE (((SSR2_Table.DataReportID)=1));"
  16.  
  17.  
  18. End Function
Feb 8 '07 #1
6 5363
nico5038
3,080 Expert 2GB
The codeless approach would be to use the table (or create a query) and activate the mailmerge from MS Word.

Getting data from a recordset is done like:

Expand|Select|Wrap|Line Numbers
  1. dim rs as DAO.recordset
  2.  
  3. set rs = currentdb.openrecordset("<< your query >>")
  4.  
  5. if rs.eof and rs.bof then
  6.   ' no rows action
  7.   exit function
  8. endif
  9. rs.movefirst
  10. while not rs.eof
  11.  ' do your thing
  12.  rs.movenext
  13. wend
This does however require a libraru in Tools/References with Microsoft DAO version #.## set and the default Active X dataobjects (ADO) switched off.

Nic;o)
Feb 9 '07 #2
ADezii
8,834 Expert 8TB
Good afternoon,

Can someone please tell me how I can return records from a query. I have text fields that I need to obtain, and have those necessary records sent to MS Word. I need to return the data that is in MainSectionLabel, MainLabel and SummaryTextBox.


Here is what I have so far:
Public Function gettbldata()

Dim strSQL As String

Set objRecordset = CreateObject("ADODB.recordset")

strSQL = "SELECT SSR2_Table.ID, SSR2_Table.DataReportID, SSR2_Table.MainSectionID, " & _
"SSR2_Table.MainSectionLabel, SSR2_Table.MainLabel, SSR2_Table.SummaryTextBox, " & _
"SSR2_Table.[Month/Year]FROM (SSR_DataReportName INNER JOIN SSR_DataReportLabels ON " & _
"SSR_DataReportName.[DataReport ID] = SSR_DataReportLabels.DataReportID)" & _
"INNER JOIN SSR2_Table ON SSR_DataReportLabels.DataReportID = SSR2_Table.DataReportID" & _
"WHERE (((SSR2_Table.DataReportID)=1));"


End Function
Since Nico mentioned DAO:
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2.  
  3. Set objRecordset = CreateObject("ADODB.recordset")
  4.  
  5. strSQL = "SELECT SSR2_Table.ID, SSR2_Table.DataReportID, SSR2_Table.MainSectionID, " & _
  6. "SSR2_Table.MainSectionLabel, SSR2_Table.MainLabel, SSR2_Table.SummaryTextBox, " & _
  7. "SSR2_Table.[Month/Year]FROM (SSR_DataReportName INNER JOIN SSR_DataReportLabels ON " & _
  8. "SSR_DataReportName.[DataReport ID] = SSR_DataReportLabels.DataReportID)" & _
  9. "INNER JOIN SSR2_Table ON SSR_DataReportLabels.DataReportID = SSR2_Table.DataReportID" & _
  10. "WHERE (((SSR2_Table.DataReportID)=1));"
  11.  
  12. Dim MyDB As DAO.Database, MyRS As DAO.Recordset
  13.  
  14. Set MyDB = CurrentDB()
  15. Set MyRS = MyDB.OpenRecordset(strSQL, dbOpenDynaset)
  16.  
  17. MyRS.MoveFirst
  18.  
  19. Do While Not MyRS.EOF        'least confusing Method
  20.     'Process your 3 relevant Fields here via this Syntax
  21.     MyRS![MainSectionLevel]        <manipulate Field>
  22.     MyRS![MainLabel]                  <manipulate Field>
  23.     MyRs![SummaryTextBox]       <manipulate Field>
  24.          MyRS.MoveNext
  25. Loop
  26.  
  27. MyRS.Close
  28.  
  29. BTW: Why do you need the other non-relevant Fields in the SQL?
Feb 9 '07 #3
Thank you for your assistance, I did remove the fields that I didn't need shortly after I posted this message



Since Nico mentioned DAO:
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2.  
  3. Set objRecordset = CreateObject("ADODB.recordset")
  4.  
  5. strSQL = "SELECT SSR2_Table.ID, SSR2_Table.DataReportID, SSR2_Table.MainSectionID, " & _
  6. "SSR2_Table.MainSectionLabel, SSR2_Table.MainLabel, SSR2_Table.SummaryTextBox, " & _
  7. "SSR2_Table.[Month/Year]FROM (SSR_DataReportName INNER JOIN SSR_DataReportLabels ON " & _
  8. "SSR_DataReportName.[DataReport ID] = SSR_DataReportLabels.DataReportID)" & _
  9. "INNER JOIN SSR2_Table ON SSR_DataReportLabels.DataReportID = SSR2_Table.DataReportID" & _
  10. "WHERE (((SSR2_Table.DataReportID)=1));"
  11.  
  12. Dim MyDB As DAO.Database, MyRS As DAO.Recordset
  13.  
  14. Set MyDB = CurrentDB()
  15. Set MyRS = MyDB.OpenRecordset(strSQL, dbOpenDynaset)
  16.  
  17. MyRS.MoveFirst
  18.  
  19. Do While Not MyRS.EOF        'least confusing Method
  20.     'Process your 3 relevant Fields here via this Syntax
  21.     MyRS![MainSectionLevel]        <manipulate Field>
  22.     MyRS![MainLabel]                  <manipulate Field>
  23.     MyRs![SummaryTextBox]       <manipulate Field>
  24.          MyRS.MoveNext
  25. Loop
  26.  
  27. MyRS.Close
  28.  
  29. BTW: Why do you need the other non-relevant Fields in the SQL?
Feb 9 '07 #4
Thank you for your assistance, I did remove the fields that I didn't need shortly after I posted this message

ok I am getting an error with these statements you wrote:

Expand|Select|Wrap|Line Numbers
  1.    MyRS![MainSectionLevel]        <manipulate Field>
  2.     MyRS![MainLabel]                  <manipulate Field>
  3.     MyRs![SummaryTextBox]       <manipulate Field>
Any Idea why?
Feb 9 '07 #5
NeoPa
32,556 Expert Mod 16PB
Good afternoon,

Can someone please tell me how I can return records from a query. I have text fields that I need to obtain, and have those necessary records sent to MS Word. I need to return the data that is in MainSectionLabel, MainLabel and SummaryTextBox.


Here is what I have so far:
Expand|Select|Wrap|Line Numbers
  1. Public Function gettbldata()
  2.  
  3. Dim strSQL As String
  4.  
  5. Set objRecordset = CreateObject("ADODB.recordset")
  6.  
  7. strSQL = "SELECT SSR2_Table.ID, SSR2_Table.DataReportID, SSR2_Table.MainSectionID, " & _
  8. "SSR2_Table.MainSectionLabel, SSR2_Table.MainLabel, SSR2_Table.SummaryTextBox, " & _
  9. "SSR2_Table.[Month/Year]FROM (SSR_DataReportName INNER JOIN SSR_DataReportLabels ON " & _
  10. "SSR_DataReportName.[DataReport ID] = SSR_DataReportLabels.DataReportID)" & _
  11. "INNER JOIN SSR2_Table ON SSR_DataReportLabels.DataReportID = SSR2_Table.DataReportID" & _
  12. "WHERE (((SSR2_Table.DataReportID)=1));"
  13.  
  14.  
  15. End Function
I'm coming to this late I know, but I did notice in your original SQL that there was no white space before the WHERE clause. That would not have worked.
I quoted the original here but I changed your post to be more easily readable.
Feb 10 '07 #6
NeoPa
32,556 Expert Mod 16PB
ok I am getting an error with these statements you wrote:

Expand|Select|Wrap|Line Numbers
  1.    MyRS![MainSectionLevel]        <manipulate Field>
  2.     MyRS![MainLabel]                  <manipulate Field>
  3.     MyRs![SummaryTextBox]       <manipulate Field>
Any Idea why?
Yes.
The <manipulate Field> bit is a personal instruction to you and not actual code. You have to do what you need to do in here.
Feb 10 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: HKM | last post by:
Hello, I have a query engine that builds the SQL Query to obtain the recordSet. Following is an Exmaple Query that my QueryBuilder outputted SELECT * FROM BookInfo WHERE BookName LIKE...
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
22
by: Gerry Abbott | last post by:
Hi all, I having some confusing effects with recordsets in a recent project. I created several recordsets, each set with the same number of records, and related with an index value. I create...
6
by: lenny | last post by:
Hi, I've been trying to use a Sub or Function in VBA to connect to a database, make a query and return the recordset that results from the query. The connection to the database and the query...
3
by: Nathan Bloomfield | last post by:
Hi there, I am having difficulty with a piece of code which would work wonders for my application if only the error trapping worked properly. Basically, it works as follows: - adds records...
2
by: Lyn | last post by:
Hi, I am opening a form in Continuous mode to list the records from a recordset created in the calling form. The recordset object is declared as Public and is set into the new form's Recordset...
13
by: Jan | last post by:
Hi I have a database that I use to keep track of the sales promotions that we send to companies. I normally send a mailing based on a subset of the companies in the database (found using the...
2
by: ajspacemanspiff | last post by:
I currently have a solution that requires 2 sub queries, where each of them is convereted into a crosstab query and then I join the crosstab queries to a result. I would like to make this more...
11
by: altreed | last post by:
Hi, I am new to ASP, HTML and iis. I have my asp code working so that I can retrieve my desired record from the database. I can place the data on the screen in table form. All works fine. ...
5
by: Henrik | last post by:
The problem is (using MS Access 2003) I am unable to retrieve long strings (255 chars) from calculated fields through a recordset. The data takes the trip in three phases: 1. A custom public...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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

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