asked on
Private Sub cmdNewRecord_Click()
Dim apptdat As Date
Dim apptStrTim As Date
Dim apptEndTim As Date
Dim outobj As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim outappt As Outlook.AppointmentItem
Set outobj = CreateObject("outlook.application")
Set outappt = outobj.CreateItem(olAppointmentItem)
Dim colCal As Outlook.Items
Dim colMyAppts As Outlook.Items
Dim intApptsKilled As Integer
Dim strFind As String
Set objapp = CreateObject("Outlook.Application")
Set objNS = objapp.GetNamespace("MAPI")
Set colCal = objNS.GetDefaultFolder(olFolderCalendar).Items
colCal.Sort "[Start]"
colCal.IncludeRecurrences = False
Forms!frmAppointments.FilterOn = False
' Save record first to be sure required fields are filled.
Me.ApptLength = DateDiff("n", ApptStartTime, ApptEndTime)
DoCmd.RunCommand acCmdSaveRecord
apptdat = Format(Forms![frmAppointments]![ApptDate], "dd mmm yyyy")
apptStrTim = Format(Forms![frmAppointments]![ApptStartTime], "hh:mm AM/PM")
apptEndTim = Format(Forms![frmAppointments]![ApptEndTime], "hh:mm AM/PM")
' Check to see if Appt is already in Outlook and, if so, modify instead of adding new.
If Me!AddedToOutlook = True Then
apptdat = Forms![frmAppointments]![oldApptDate]
'get appointments spanning 24hrs in date range of current day
strFind = "[Start] > " & _
Quote(apptdat & " 12:00 AM") & " AND [End] < " & _
Quote(apptdat & " 11:59 PM")
intApptsKilled = 0
Set colMyAppts = colCal.Restrict(strFind)
For Each outappt In colMyAppts
If InStr(outappt.Subject, Forms![frmAppointments]![Appt]) Then
Dim db As Database
Dim appts_RST As Recordset
Set db = CurrentDb
Set appts_RST = db.OpenRecordset("tblAppointments", dbOpenDynaset)
appts_RST.MoveFirst
Do While (Not appts_RST.EOF)
If (appts_RST![AppointmentID] = Forms![frmAppointments]![AppointmentID]) Then
Exit Do
End If
appts_RST.MoveNext
Loop
With outappt
.Start = Me.ApptDate
.Duration = Me.ApptLength
.Subject = Me.Appt
If Not IsNull(Me.ApptNotes) Then .Body = Me.ApptNotes
If Not IsNull(Me.ApptLocation) Then .Location = Me.ApptLocation
.ReminderSet = Me.ApptReminder
If (Me.ApptReminder = True) Then .ReminderMinutesBeforeStart = Me.ReminderMinutes
.Save
End With
intApptsKilled = intApptsKilled + 1
End If
Next
' Release the Outlook object variable.
Set objapp = Nothing
' Set the AddedToOutlook flag, save the record, move to new record, and display a message.
DoCmd.GoToRecord , , acNewRec
MsgBox "Appointment Modified!"
Exit Sub
Else
' Add a new appointment.
With outappt
.Start = Me!ApptDate & " " & Me!ApptStartTime
.Duration = Me!ApptLength
.Subject = Me!Appt
If Not IsNull(Me!ApptNotes) Then .Body = Me!ApptNotes
If Not IsNull(Me!ApptLocation) Then .Location = Me!ApptLocation
If Me!ApptReminder Then
.ReminderMinutesBeforeStart = Me!ReminderMinutes
.ReminderSet = True
End If
.Save
'get appointments spanning 24hrs in date range of current day
strFind = "[Start] > " & _
Quote(apptdat & " 12:00 AM") & " AND [End] < " & _
Quote(apptdat & " 11:59 PM")
'Looks up appointment just logged to retrieve its unique EntryID to store in Access table entry
intApptsKilled = 0
Set colMyAppts = colCal.Restrict(strFind)
For Each outappt In colMyAppts
If InStr(outappt.Subject, Forms![frmAppointments]![Appt]) Then
'MsgBox (outappt.EntryID)
Me!EntryID = (outappt.EntryID)
intApptsKilled = intApptsKilled + 1
End If
Next
End With
End If
' Release the Outlook object variable.
Set objapp = Nothing
' Set the AddedToOutlook flag, save the record, move to new record, and display a message.
Me!AddedToOutlook = True
DoCmd.GoToRecord , , acNewRec
MsgBox "Appointment Added!"
Exit Sub
AddAppt_Err:
MsgBox "Error " & Err.Number & vbCrLf & Err.Description
Exit Sub
End Sub
ASKER
ASKER
Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.
TRUSTED BY
(either that of move this entire system to Access)
Access can do things like this but it is not easy.
And note that MS can change the way Access interacts with outlook without notice (thus "breaking" all your code