Full Version: Error 424: Object Required When Moving From Private To Public Sub
UtterAccess Discussion Forums > Microsoft® Access > Access Modules
keluc1
I have been using this code to cancel an appointment in a shared outlook calender and mark that the appointment has been deleted (thanks for the code, can't remember where I found it now but it was not mine originally.)

Private Sub Cancelled_Click()
On Error GoTo ErrHandler

'add your code for outlook to be objApp
Dim objfolder As Outlook.MAPIFolder
Dim objOutlook As Outlook.Application
Set objOutlook = CreateObject("Outlook.Application")
Dim objAppt As Outlook.AppointmentItem
Set objfolder = objOutlook.GetNamespace("Mapi").GetFolderFromID("00000000D7B29A73498B82499CE2942947EC8C680100D46D50BA7A253C49A52D1C23956C0BE
A00000335002C0000")

sFilter = "[Mileage] = " & Appt_ID & ""

If Not objfolder Is Nothing Then
Set objAppt = objfolder.Items.Find(sFilter)

If objAppt Is Nothing Then
Dim answer As Integer
answer = MsgBox("Appointment not found. Do you want to mark that this appointment has been cancelled and deleted from the diary?", vbYesNo)
If answer = vbYes Then
Me!DeletedFromOutlook = True
DoCmd.RunCommand acCmdSaveRecord
Me!Cancelled = True
Else

' Set the DeletedFromOutlook flag and cancelled flag to false, save the record
Me!DeletedFromOutlook = False
DoCmd.RunCommand acCmdSaveRecord
Me!Cancelled = False
End If

Else
With objAppt
objAppt.Delete

' Set the DeletedFromOutlook flag, save the record, display a message.
Me!DeletedFromOutlook = True
Me!Date_Cancelled = Date
DoCmd.RunCommand acCmdSaveRecord
MsgBox "Appointment deleted"

End With

End If

Exit Sub

ErrHandler:
If Err.Number = -2147220991 Then
MsgBox "Open the clinic diary and try again"
' Set the DeletedFromOutlook flag and cancelled flag to false, save the record
Me!DeletedFromOutlook = False
DoCmd.RunCommand acCmdSaveRecord
Me!Cancelled = False
Else
Dim Msg As String
Msg = Err.Number & ": " & Err.Description
MsgBox Msg
End If
End If
End Sub

I now want to make this a public module so that I can call this procedure from different forms. This is what I am using;

Public Sub Cancelled_Click_Public(frmMe As Form)

On Error GoTo ErrHandler

'add your code for outlook to be objApp
Dim objfolder As Outlook.MAPIFolder
Dim objOutlook As Outlook.Application
Set objOutlook = CreateObject("Outlook.Application")
Dim objAppt As Outlook.AppointmentItem
Set objfolder = objOutlook.GetNamespace("Mapi").GetFolderFromID("00000000D7B29A73498B82499CE2942947EC8C680100D46D50BA7A253C49A52D1C23956C0BE
A00000335002C0000")

sFilter = "[Mileage] = " & Form!Appt_ID & ""

If Not objfolder Is Nothing Then
Set objAppt = objfolder.Items.Find(sFilter)

If objAppt Is Nothing Then
Dim answer As Integer
answer = MsgBox("Appointment not found. Do you want to mark that this appointment has been cancelled and deleted from the diary?", vbYesNo)
If answer = vbYes Then
Form!DeletedFromOutlook = True
DoCmd.RunCommand acCmdSaveRecord
Form!Cancelled = True
Else

' Set the DeletedFromOutlook flag and cancelled flag to false, save the record
Form!DeletedFromOutlook = False
DoCmd.RunCommand acCmdSaveRecord
Form!Cancelled = False
End If

Else
With objAppt
objAppt.Delete


' Set the DeletedFromOutlook flag, save the record, display a message.
Form!DeletedFromOutlook = True
Form!Date_Cancelled = Date
DoCmd.RunCommand acCmdSaveRecord
MsgBox "Appointment deleted"

End With

End If

Exit Sub

ErrHandler:
If Err.Number = -2147220991 Then
MsgBox "Open the clinic diary and try again"
' Set the DeletedFromOutlook flag and cancelled flag to false, save the record
Form!DeletedFromOutlook = False
DoCmd.RunCommand acCmdSaveRecord
Form!Cancelled = False
Else
Dim Msg As String
Msg = Err.Number & ": " & Err.Description
MsgBox Msg
End If
End If
End Sub

and to call this

Call Cancelled_Click_Public(Me)

I'm getting error 424: Object required. I'm lost, any help would be very appreciated!

Peter46
In your sub declaration you have used 'formMe' as the parameter used to pass the form, but you have used 'Form' in your code.
On any case I think you would need Forms!formMe as the form reference.
keluc1
Thanks!

I changed the public sub to

Public Sub Cancelled_Click_Public(frmMe As Form)

and my form references to frm. and that worked.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.