473,587 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Record Set based on Query and Where Condition

I'm trying to two two things through Access to Outlook based on a
query and selections made on a form.

Firstly, i'd like to create an email based on the selections made on a
form.

Secondly i'd like to Export contact to Outlooks Contacts based on
selections.

The code i have so far for these is as follows:-

BCC Email:
Private Sub BCCEmail_Click( )
On Error GoTo Err_BCCMail_Cli ck

Dim stDocName As String
Dim MyOutlook As New Outlook.Applica tion
Dim MyMail As Outlook.MailIte m
Dim StrBCC As String
Dim rst As DAO.Recordset
Dim varItem As Variant
Dim strDescrip As String
Dim lngLen As Long
Dim strDelim As String
Dim strCategorySele ct As String
Dim strCountrySelec t As String
Dim strWhere As String

stDocName = "Q_Email"
With Me.CategorySele ct
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
strCategorySele ct = strCategorySele ct &
strCategorySele ct & strDelim & .ItemData(varIt em) & strDelim & ","
strDescrip = strDescrip & """" & .Column(1, varItem)
& """, "
End If
Next
End With

lngLen = Len(strCategory Select) - 1
If lngLen 0 Then
strCategorySele ct = "[CategoryID] IN (" & Left$
(strCategorySel ect, lngLen) & ")"
lngLen = Len(strDescrip) - 2
If lngLen 0 Then
strDescrip = "Categories : " & Left$(strDescri p, lngLen)
End If
End If

With Me.CountrySelec t
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
strCountrySelec t = strCountrySelec t & strCountrySelec t
& strDelim & .ItemData(varIt em) & strDelim & ","
strDescrip = strDescrip & """" & .Column(1, varItem)
& """, "
End If
Next
End With

lngLen = Len(strCountryS elect) - 1
If lngLen 0 Then
strCountrySelec t = "[CountryID] IN (" & Left$
(strCountrySele ct, lngLen) & ")"
lngLen = Len(strDescrip) - 2
If lngLen 0 Then
strDescrip = "Categories : " & Left$(strDescri p, lngLen)
End If
End If

If strCategorySele ct "" Then strWhere = strWhere &
strCategorySele ct & " And "
If strCountrySelec t "" Then strWhere = strWhere &
strCountrySelec t & " And "
If strWhere "" Then strWhere = Left(strWhere, Len(strWhere)
- 5)
Set rst = CurrentDb.OpenR ecordset(stDocN ame, dbOpenForwardOn ly)
With rst
Do Until .EOF
StrBCC = StrBCC & ![EmailAddress] & ";"
..MoveNext
Loop
..Close
End With
StrBCC = Left(StrBCC, Len(StrBCC) - 1)

Set MyOutlook = New Outlook.Applica tion
Set MyMail = MyOutlook.Creat eItem(olMailIte m)
MyMail.BCC = StrBCC
MyMail.Display

Set MyOutlook = Nothing
Set rst = Nothing

Exit_BCCMail_Cl ick:
Exit Sub

Err_BCCMail_Cli ck:
MsgBox Err.Description
Resume Exit_BCCMail_Cl ick

End Sub
Export Contacts:
Private Sub ExportToOutlook _Click()
On Error GoTo Err_ExportToOut look_Click

Dim stDocName As String
Dim dbs As Database
Dim rst As Recordset
Dim objOutlook As Outlook.Applica tion
Dim nms As Outlook.NameSpa ce
Dim flds As Outlook.Folders
Dim fldContacts As Object
Dim itms As Object
Dim itm As Object
Dim strTitle As String
Dim strFirstName As String
Dim strMiddleName As String
Dim strLastName As String
Dim strJobTitle As String
Dim strLastNameFirs t As String
Dim strBusinessStre et As String
Dim strBusinessStre et2 As String
Dim strBusinessCity As String
Dim strBusinessStat e As String
Dim strBusinessPost alCode As String
Dim strBusinessCoun try As String
Dim strBusinessPhon e As String
Dim strBusinessFax As String
Dim strHomePhone As String
Dim strOtherPhone As String
Dim strEMailAddress As String
Dim strEMailAddress 2 As String
Dim strWebPage As String
Dim strNotes As String
Dim strContactID As String
Dim strCRLF As String
Dim lngCount As Long
stDocName = "Q_ExportToOutl ook"

strCRLF = Chr$(13) & Chr$(10)

Set objOutlook = CreateObject("O utlook.Applicat ion")
Set nms = objOutlook.GetN amespace("MAPI" )
Set fldContacts = nms.GetDefaultF older(olFolderC ontacts)
Set itms = fldContacts.Ite ms

Set dbs = CurrentDb
Set rst = dbs![tblPerson].OpenRecordset( dbOpenTable, dbDenyRead)
lngCount = rst.RecordCount
MsgBox lngCount & " records to transfer to Outlook"

Do Until rst.EOF
With rst

strContactID = Nz(![PersonID])
strTitle = Nz(![Title])
strFirstName = Nz(![FirstName])
strLastName = Nz(![LastName])
strJobTitle = Nz(![JobTitle])
strLastNameFirs t = Nz(![LastName]) & ", " & Nz(![FirstName])
strBusinessStre et = Nz(![BusinessStreet]) & IIf(Nz(!
[BusinessStreet2]) <"", strCRLF & Nz(![BusinessStreet2]), "")
strBusinessCity = Nz(![BusinessCity])
strBusinessStat e = Nz(![BusinessState])
strBusinessPost alCode = Nz(![BusinessPostalC ode])
strBusinessCoun try = Nz(![BusinessCountry])
strBusinessPhon e = Nz(![BusinessPhone])
strBusinessFax = Nz(![BusinessFax])
strHomePhone = Nz(![HomePhone])
strHomeFax = Nz(![HomeFax])
strOtherPhone = Nz(![BusinessPhone2])
strOtherFax = Nz(![OtherFax])
strEMailAddress = Nz(![EmailAddress])
strEMailAddress 2 = Nz(![Email2Address])
strWebPage = Nz(![WebPage])
strNotes = Nz(![Notes])
End With

Set itm = itms.Add("IPM.C ontact")

With itm
.Title = strTitle
.FirstName = strFirstName
.MiddleName = strMiddleName
.LastName = strLastName
.JobTitle = strJobTitle
.BusinessAddres sStreet = strBusinessStre et
.BusinessAddres sCity = strBusinessCity
.BusinessAddres sState = strBusinessStat e
.BusinessAddres sPostalCode = strBusinessPost alCode
.BusinessAddres sCountry = strBusinessCoun try
.BusinessTeleph oneNumber = strBusinessPhon e
.BusinessFaxNum ber = strBusinessFax
.HomeTelephoneN umber = strHomePhone
.HomeFaxNumber = strHomeFax
.OtherTelephone Number = strOtherPhone
.OtherFaxNumber = strOtherFax
.Email1Address = strEMailAddress
.Email2Address = strEMailAddress 2
.WebPage = strWebPage
.Notes = strNotes
.Categories = "From Access"
.Close (olSave)
Me![txtLastContact] = strContactID & " -- " &
strLastNameFirs t

DoCmd.DoMenuIte m acFormBar, stDocName, acNormal,
acRecordsMenu, acSaveRecord, , acMenuVer70

End With
rst.MoveNext
Loop

MsgBox "All Contacts exported!"

Exit_ExportToOu tlook_Click:
Exit Sub

Err_ExportToOut look_Click:
MsgBox Err.Description
Resume Exit_ExportToOu tlook_Click

End Sub

At the moment i have the Query set up to filter this data from the
form, but it may be easiest to create a Where Condition so that i can
export the different selections to different folders?

Any help would be superb!

Nov 21 '08 #1
0 1754

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

Similar topics

2
5981
by: Chris | last post by:
I have tableA, defined as: field1 varchar2(10), field2 varchar2(10), field3 varchar2(10) I have host variables defined as: v1 pic x(10) varying v2 pic x(10) varying
8
3595
by: xixi | last post by:
hi, we are using db2 udb v8.1 on windows, i have a table contains over one million records, it has seperate own tablespace than others, with bufferpool size 250, i have created multiple views on this table , the problem is some of the views have omit infomation like this one : where ( not (OECD09='A')) and ( not ( OECD09='K')) and ( not (...
8
13203
by: John Welch | last post by:
I have a command button with the following code: DoCmd.OpenForm "frmSearchAssignments", , , "SearchAssignmentID = 1" (SearchAssignmentID is the PK, auto number) When it runs, the form opens but shows all records, rather than going to the one I want. If I look at the form in design view, it shows 'SearchAssignmentID=1' (without quotes) as the...
2
1690
by: Sreedhar Vankayala | last post by:
Hi, I have a table IV00108wc which contains several thousand records in whcih ITEMNMBR is the primary key. I have a com+ object returning recordset which has ITEMNMBR and another details related. I want to query another table based on Com+ object recordset. Select distinct WebClas1 from IV00108wc where ItemNmbr in (...).
5
15012
by: das | last post by:
hello all, this might be simple: I populate a temp table based on a condition from another table: select @condition = condition from table1 where id=1 in my stored procedure I want to do this: if @condition = 0
16
1843
by: KayCee | last post by:
Hello All - I'm new to the community. I have been asked to move the used of a database I created from a record in a table to the corresponding record of a form. After much research and trial & error - I have come the conclution that this is not possable (please let me know if I wrong.) So to achieve what has been requested I am planning to...
7
1994
by: lostdawg | last post by:
Hi, I have a question regarding placing the Where condition in this section of code Private Sub Command219_Click() DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70 If IsNull(Me!ContactID) Then MsgBox "Please select a valid record", _ vbOKOnly, "Error"
10
4453
by: BHTanna | last post by:
I have one stored procedure, which defines where condition based useing Case when...I m storing this Condition in one outpur variable of Store procedure. I would like to now use this condition in view or Query... Pls guide how to do this or is there any other way of doing the same? Also note that, my case when defines entire condition...
2
1518
by: kadeous | last post by:
I am updating records using a cron job that works perfectly fine. The issue I have is that the script is updating all records using the information from the first row matching my WHERE condition. I'm using the following to query the database for data. The row I'm searching for information from is time in the database, and mtime is a row that...
0
7920
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7849
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...
0
8215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7973
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...
0
5394
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3844
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...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
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...

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.