Results 1 to 6 of 6

Thread: Field reference in VB 6.0 code for Access 2000

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    3

    Field reference in VB 6.0 code for Access 2000

    Hi everyone,

    I was wondering if you might be able to shed some light on what is probably a simple problem:

    I have read carefully the post titled:

    Access and Fields of Recordsets using DAO3.6 [Resolved]

    I am experiencing a similar problem, though I have checked and checked again for typos to no avail. I have also tried to enter a Stop statement as suggested in the above thread but before the Stop happens the error occurs, so I can't step in and take a look.

    My only incling is that perhaps the code is looking for Distributors.Email in the wrong place (it is a field called "Email" in a table called "Distributors" - though I did hope this was explicit).

    If anyone can spot my mistake I would be delighted to hear what it is, otherwise if the Stop statement could be explained further to help me uncover the problem that would be much appreciated!

    Many thanks in advance!

    Abiart


    Code:
    Private Sub EmailDistributorsEnquiryDetails_Click()
    Dim rsEmail As DAO.Recordset
    Dim strEmail As String
    Set rsEmail = CurrentDb.OpenRecordset("qryDistributorsEnquiryDetails")
    
    Do While Not rsEmail.EOF
    strEmail = rsEmail.Fields("Distributors.Email").Value
    Stop
    DoCmd.SendObject acSendQuery, acqryDistributorsEnquiryDetails, acFormatXLS, Distributors.Email, [[email protected]], , "Company Name Sales Leads", "Dear [DistributorEmails.FirstName] Attached is your monthly update of sales leads. We would appreciate if you could go through these records and let us know the status of the enquiries. Thank you for your kind assistance in this matter. Kind regards Company Name Sales Team", 1
    
    rsEmail.MoveNext
    
    Loop
    Set rsEmail = Nothing
    
    End Sub

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Field reference in VB 6.0 code for Access 2000

    Well, you may have to do a .MoveFirst first. But I think you're on the right track for the error. The field name in the recordset isn't the table & field name, it's just the field name - Email, in this case, unless you used AS in the query to call it something else.
    You're also going to run into problems in your .SendQuery statement, but see if the above helps first.
    Tengo mas preguntas que contestas

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    3

    SendObject - To argument lookup / Re: Field reference in VB 6.0 code for Access 2000

    Dear Savelinus,

    Thanks for your suggestions. I haven't yet put in a .MoveFirst, but I found changing ("Distributors.Email") to ("Email") has solved the first problem.

    You're right that my .SendQuery statement was problematic, however I changed the object name from acqryDistributorsEnquiryDetails to "qryDistributorsEnquiryDetails", and I am now getting to the stage where the compose email form appears !

    I think my final problem is what syntax to use when I want the To arguement to lookup a field in another table. This is the confusing part: I have the query I want to send: a list of enquiries which includes a combo field which looks up a Distributor ID in table Distributors. For each distributor I want to filter the query to show only the enquiries with that distributor's ID in the record and then cross-reference that distributor ID in the Distributors table to find the Email to send the filtered query to.

    I think I have the right code for this (and I've made a dummy where all the email addresses in the distributors table are mine, so I can test it), but it's just the syntax to get the To arguement to lookup a field that I need.

    I have tried combinations of different parentheses/inverted commas etc but suspect I'll need something a little more complex?

    To clarify, here is the updated code:

    Code:
    Private Sub EmailDistributorsEnquiryDetails_Click()
    Dim rsEmail As DAO.Recordset
    Dim strEmail As String
    Set rsEmail = CurrentDb.OpenRecordset("qryDistributorsEnquiryDetails")
    
    Do While Not rsEmail.EOF
    strEmail = rsEmail.Fields("Email").Value
    DoCmd.SendObject acSendQuery, "qryDistributorsEnquiryDetails", acFormatXLS, ("Email"), "[email protected]", , "Company Name Sales Leads", "Dear [DistributorEmails.FirstName] Attached is your monthly update of sales leads. We would appreciate if you could go through these records and let us know the status of the enquiries. Thank you for your kind assistance in this matter. Kind regards Company Name Sales Team", 1
    
    rsEmail.MoveNext
    
    Loop
    Set rsEmail = Nothing
    
    End Sub
    I can't attach a a screensave of the Send Mail form that appears, but note the text "Email" appears in the To field and [DistributorsEmails.FirstName] appears in the message body instead of Joe/Fred etc. Also the attached Excel file is not filtered.

    Many thanks again!

    Abby
    Last edited by abiart; Jun 14th, 2006 at 05:44 AM.

  4. #4
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Field reference in VB 6.0 code for Access 2000

    Do you mean where you have "[email protected]"? Replace that with strEmail, that's why you got that value. You don't have to use the variable, you could use just rsEmail.Fields("Email").Value. I use variables, though.
    "Dear [DistributorEmails.FirstName]..." is also incorrect, for a similar reason to your first problem. Additionally, since it's in quotes, that wording is exactly what you'll get.
    Tengo mas preguntas que contestas

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    3

    Unhappy Automation or SendObject? / Re: Field reference in VB 6.0 code for Access 2000

    I have just read the following article:

    Using Automation in Microsoft Office Access 2003 to Work with Microsoft Office Outlook 2003

    Maybe the automation method will give me the flexibility I need? Is it even possible to lookup field data for To arguments with SendObject commands?

  6. #6
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Field reference in VB 6.0 code for Access 2000

    Automation is fine. I have Access & Outlook 2K, not 2003, but here's an example of sending an email, using various variables (haven't dimmed or set them all in this example, but this is the idea):
    VB Code:
    1. 'send Outlook email
    2. Private Sub MailNoCommentQuestions(strSubj As String, strBody As String)
    3. On Error GoTo errMailing
    4.       Dim objOutlook As New Outlook.Application
    5.       Dim objMail As MailItem
    6.      
    7.       Set objMail = objOutlook.CreateItem(olMailItem)
    8.       With objMail
    9.             .To = strTo
    10.             .CC = strCC
    11.             .Subject = strSubj
    12.             .Body = strBody
    13.             .Display
    14.       End With
    15.      
    16. Cleanup:
    17.       If Not objOutlook = Nothing Then
    18.             Set objMail = Nothing
    19.             Set objOutlook = Nothing
    20.       End If
    21.      
    22. errMailing:
    23.       MsgBox "Email not sent!" & vbCrLf & vbCrLf & Err.Number & vbCrLf & Err.Description, vbCritical, "Email Not Sent"
    24.       Resume Cleanup
    25. End Sub
    There are other properties for the email, like attachments, etc. The code above doesn't actually send this email; it displays it for you to send. Use .Send to send right away. Also, you'll need to set a reference to Outlook in the code IDE window to use Outlook automation.
    The SendObject method doesn't look up any values; you have to supply them. That's what you have your recordset for.
    For your "Distributors.Firstname" section, try:
    VB Code:
    1. strBody = "Dear " & rsEmail!FirstName & ", " & vbcrlf & "Attached..."
    assuming your query for rsEmail also returns the first name. The rsEmail!FirstName notation is just an alternate method to rsEmail.Fields("FirstName").Value.
    Last edited by salvelinus; Jun 14th, 2006 at 07:43 AM.
    Tengo mas preguntas que contestas

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width