If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Help: reading data from table
Hi
This must be another easy one for you guys: I am trying to read all email addresses in a table into a string variable using "getrows". I have searched the Internet for a few days and couldn't get it work for me. the following is the code I come up with. It doesn't work. It stops at OpenRecordset saying type missmatch. Please help...... ad ============================= Sub test() Dim db As Database Dim Lrs As Recordset Dim LSQL As String Dim email As Variant Dim c As Integer Dim strEmail As String 'Open connection to current Access database Set db = CurrentDb() 'Create SQL statement to retrieve value from GST table LSQL = "SELECT Contacts.[E-Mail Address] FROM MailingList INNER JOIN Contacts ON MailingList.ContactID = Contacts.[Contact ID];" Set Lrs = db.OpenRecordset(LSQL) email = Lrs.GetRows For c = 1 To 2 strEmail = strEmail & email(c) & "; " Next c ' Display the results. MsgBox strEmail Lrs.Close Set Lrs = Nothing End Sub |
#2
|
|||
|
|||
You don't need the GetRows Method. All you need to do is
to traverse the Recordset and use concatenation to append to the strEmail as you traverse the Recordset using a loop. Check Access Help on the MoveNext Method of the Recordset. HTH Van T. Dinh MVP (Access) -----Original Message----- Hi This must be another easy one for you guys: I am trying to read all email addresses in a table into a string variable using "getrows". I have searched the Internet for a few days and couldn't get it work for me. the following is the code I come up with. It doesn't work. It stops at OpenRecordset saying type missmatch. Please help...... ad ============================= Sub test() Dim db As Database Dim Lrs As Recordset Dim LSQL As String Dim email As Variant Dim c As Integer Dim strEmail As String 'Open connection to current Access database Set db = CurrentDb() 'Create SQL statement to retrieve value from GST table LSQL = "SELECT Contacts.[E-Mail Address] FROM MailingList INNER JOIN Contacts ON MailingList.ContactID = Contacts.[Contact ID];" Set Lrs = db.OpenRecordset(LSQL) email = Lrs.GetRows For c = 1 To 2 strEmail = strEmail & email(c) & "; " Next c ' Display the results. MsgBox strEmail Lrs.Close Set Lrs = Nothing End Sub . |
#3
|
|||
|
|||
"Van T. Dinh" wrote in message ... You don't need the GetRows Method. All you need to do is to traverse the Recordset and use concatenation to append to the strEmail as you traverse the Recordset using a loop. Check Access Help on the MoveNext Method of the Recordset. Thank you very much Van! It works now. However I had to dim Lrs as object, not Recordset. If I dim Lrs as Recordset, it gives me type mismatching error message at OpenRecordset. Why is this? code that WORKs: =============== Sub test() Dim db As Database Dim Lrs As Object '------ Dim LSQL As String Dim strEmail As String 'Open connection to current Access database Set db = CurrentDb() 'Create SQL statement to retrieve value from table LSQL = "SELECT Contacts.[E-Mail Address] FROM MailingList INNER JOIN Contacts ON MailingList.ContactID = Contacts.[Contact ID];" Set Lrs = db.OpenRecordset(LSQL) While Not Lrs.EOF strEmail = strEmail + Lrs![E-Mail Address] + "; " Lrs.MoveNext Wend ' Display the results. MsgBox strEmail Lrs.Close Set Lrs = Nothing End Sub |
#4
|
|||
|
|||
Thank you very much Van! It works now. However I had to dim Lrs as
object, not Recordset. If I dim Lrs as Recordset, it gives me type mismatching error message at OpenRecordset. Why is this? I have figured it out: set the appriate references, forgot which one, after trying different stuff, it's working now. Thanks again. |
#5
|
|||
|
|||
You probably have both ADO & DAO Library in the References
and the ADO has higher priority than the DAO Library. When you dim. the Recordset without qualifier, it is defaulted to ADO Recordset. However, you code needs DAO Recordset and ADO Recordset / DAO Recordset are not compatible. Try the Dim statement: Dim Lrs As DAO.Recordset HTH Van T. Dinh MVP (Access) -----Original Message----- Thank you very much Van! It works now. However I had to dim Lrs as object, not Recordset. If I dim Lrs as Recordset, it gives me type mismatching error message at OpenRecordset. Why is this? code that WORKs: =============== Sub test() Dim db As Database Dim Lrs As Object '------ Dim LSQL As String Dim strEmail As String 'Open connection to current Access database Set db = CurrentDb() 'Create SQL statement to retrieve value from table LSQL = "SELECT Contacts.[E-Mail Address] FROM MailingList INNER JOIN Contacts ON MailingList.ContactID = Contacts.[Contact ID];" Set Lrs = db.OpenRecordset(LSQL) While Not Lrs.EOF strEmail = strEmail + Lrs![E-Mail Address] + "; " Lrs.MoveNext Wend ' Display the results. MsgBox strEmail Lrs.Close Set Lrs = Nothing End Sub . |
#6
|
|||
|
|||
Thanks again
|
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
SQL view of messed up action queries | Kendra | Running & Setting Up Queries | 2 | August 31st, 2004 09:53 PM |
Merge data from one table into another table | xpnovice | General Discussion | 2 | June 8th, 2004 01:58 PM |
Data Table | Max | Worksheet Functions | 1 | April 12th, 2004 04:12 AM |
can I hide a series in a stacked bar chart but keep the series in the data table | Stephen Bullen | Charts and Charting | 0 | December 5th, 2003 10:53 AM |