Avatar billede t-johansen Juniormester
20. marts 2006 - 10:23 Der er 15 kommentarer og
1 løsning

Kan ikke bruge søgekritere på den forespørsel

Jeg har følgende kode som virker fint til at sende emails med ud fra en forespørsel, men lige så snart jeg bruger et søgekritere i forspørgslen (like "*" & [indtast søgning]) så fungerer koden ikke længere.
Hvordan kan dette lykkes?

Jeg  har fået koden fra Jephens eksempel og modificeret den lidt.
http://www.jephens.com/howtoemail.asp


Modul kode:



Option Compare Database
Option Explicit

Public Function SendEMail()

Dim db As DAO.Database
Dim MailList As DAO.Recordset
Dim MyOutlook As Outlook.Application
Dim MyMail As Outlook.MailItem
Dim Subjectline As String
Dim BodyFile As String
Dim fso As FileSystemObject
Dim MyBody As TextStream
Dim MyBodyText As String

Set fso = New FileSystemObject

 

  ' Now, we open Outlook for our own device..
    Set MyOutlook = New Outlook.Application


' Set up the database and query connections

    Set db = CurrentDb()

    Set MailList = db.OpenRecordset("MyEmailAddresses")

' now, this is the meat and potatoes.
' this is where we loop through our list of addresses,
' adding them to e-mails and sending them.

    Do Until MailList.EOF

        ' This creates the e-mail
       
        Set MyMail = MyOutlook.CreateItem(olMailItem)
           
            ' This addresses it
            MyMail.To = MailList("email")
           
            'This gives it a subject
            MyMail.Subject = Subjectline$
           
            'This gives it the body
            MyMail.Body = MyBodyText


           

            ' To briefly describe:
            ' "c:\myfile.txt" = the file you want to attach
            '
            ' olByVaue = how to pass the file.  olByValue attaches it, olByReference creates a shortcut.
            '      the shortcut only works if the file is available locally (via mapped or local drive)
            '
            ' 1 = the position in the outlook message where to attachment goes.  This is ignored by most
            '      other mailers, so you might want to ignore it too.  Using 1 puts the attachment
            '      first in line.
            '
            ' "My Displayname" = If you don't want the attachment's icon string to be "c:\myfile.txt" you
            '      can use this property to change it to something useful, i.e. "4th Qtr Report"



            'This sends it!
            'MyMail.Send'

            'Some people have asked how to see the e-mail
            'instead of automaticially sending it.
            'Uncomment the next line
            'And comment the "MyMail.Send" line above this.

            MyMail.Display


       
    'And on to the next one...
    MailList.MoveNext

Loop

'Cleanup after ourselves

Set MyMail = Nothing


'Uncomment the next line if you want Outlook to shut down when its done.
'Otherwise, it will stay running.

'MyOutlook.Quit
Set MyOutlook = Nothing

MailList.Close
Set MailList = Nothing
db.Close
Set db = Nothing

End Function
Avatar billede fennec Nybegynder
20. marts 2006 - 10:26 #1
Prøv med % i stedet for *.
Avatar billede t-johansen Juniormester
20. marts 2006 - 10:40 #2
så kan jeg slet ikke få nogle resultater frem.
Men den siger stadig fejl.

idenne linje:
Set MailList = db.OpenRecordset("MyEmailAddresses")



Prøvede med dette i forespørgelsen "MyEmailAddresses":
Like "%" & [indtast] & "%"

og med dette:
Like "%" & [indtast]
Avatar billede terry Ekspert
20. marts 2006 - 10:56 #3
have you tried

Like "*" & [indtast] & "*"
Avatar billede t-johansen Juniormester
20. marts 2006 - 10:59 #4
Yes that was the standard search criteria, that didnt work with my module code. my module code only works without any search criteria, and I only need to send to the one within the criteria.
Avatar billede mugs Novice
20. marts 2006 - 10:59 #5
Like "*" & [indtast kundenavn] & "*"
Avatar billede terry Ekspert
20. marts 2006 - 11:01 #6
You should concentrate on getting the query (MyEmailAddresses) to work. If that doesnt work then you dsont have any record to eamil to.
Avatar billede terry Ekspert
20. marts 2006 - 11:05 #7
can we see your query (SQL)?
Avatar billede t-johansen Juniormester
20. marts 2006 - 11:05 #8
The query(MyEmailAddresses) works also with (Like "*" & [indtast] & "*") but when I use it with my code it fails in this line :

Set MailList = db.OpenRecordset("MyEmailAddresses")

But if i dont have a search criteria in the query then my code works fine.
Avatar billede t-johansen Juniormester
20. marts 2006 - 11:08 #9
here is the query:

SELECT Emails.EMailAddress, Emails.sms
FROM Emails
WHERE (((Emails.EMailAddress) Like "*" & [indstast] & "*"));
Avatar billede t-johansen Juniormester
20. marts 2006 - 11:10 #10
By the way I know there is a small error in the code:

' This addresses it
            MyMail.To = MailList("email")

should be:

' This addresses it
            MyMail.To = MailList("EMailAddress")

but that doesnt change the fault
Avatar billede t-johansen Juniormester
20. marts 2006 - 11:17 #11
Terry, jeg er villig til at sende dig databasen hvis det hjælper.

men så skal jeg bruge en email
Avatar billede terry Ekspert
20. marts 2006 - 11:22 #12
Try

adding an input box to your code


Set MailList = db.OpenRecordset("SELECT * FROM MyEmailAddresses WHERE Emails.EMailAddress Like '*" & inputbox("indtast")
& "*'")
Avatar billede terry Ekspert
20. marts 2006 - 11:23 #13
Oops, not quite right.

Set MailList = db.OpenRecordset("SELECT * FROM MyEmailAddresses WHERE Emails.EMailAddress Like '*" & inputbox("indtast") & "*'")
Avatar billede t-johansen Juniormester
20. marts 2006 - 11:28 #14
That works, thx

How do I give you the points?

you commentet it, should I just choose accept or do you have to give me a formal answer?
Avatar billede terry Ekspert
20. marts 2006 - 11:34 #15
:o)
Avatar billede t-johansen Juniormester
20. marts 2006 - 11:38 #16
so how about my other question ;-)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Dyk ned i databasernes verden på et af vores praksisnære Access-kurser

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester