Am I doing this correctly?

Am I doing this correctly?

Post by Patrick Fi » Sun, 03 Aug 2003 13:09:36



Chances are....yes. The fields in table2 and table3, containted in
table1, should be the primary key of the other tables. Example. If you
have a table called SalesOrders and a field in that table is called
CustomerID, that field CustomerID is normally the primary key in the
Customers table.
 
 
 

Am I doing this correctly?

Post by Lauren » Tue, 20 Jan 2004 12:03:43

After the "ExitHere" below, I am closing my object vars and setting
their values to nothing. Am I doing it correctly?

Thanks for all responses.

'================= BEGIN CODE ================================
Public Sub CreateOLMessage(msgTo As String, _
msgSubj As String, _
msgBdy As String, _
ViewAfter As Boolean, _
HTMLFmt As Boolean, _
Attach As String)

''This sub creates a new email message using output from the LMS
Message manager form.

Dim objOutlook As Object ' Late bind
Dim ns As Object ' Late bind
Dim objMsg As Object
Dim strAttachments As String
Dim strAttach() As String 'Dynamic array for analyzing
attachment availability

On Error GoTo HandleErr

If IsOutlook = 0 Then
ErrMsg NoOutlookMsg
Exit Sub
End If

Set objOutlook = CreateObject("Outlook.application")
Set ns = objOutlook.GetNamespace("MAPI")
ns.Logon

Set objMsg = objOutlook.CreateItem(0) 'Const olMailItem = 0
objMsg.to = msgTo
objMsg.Subject = msgSubj

If Len(msgBdy) > 0 Then
If HTMLFmt = False Then
objMsg.body = msgBdy
Else
objMsg.HTMLBody = msgBdy
End If
End If

'=== Are there any attachments? If so, append them:
' Add attachments to MailItem object's Attachments
' collection.

EmailSent = True

With objMsg
If Len(Attach) > 0 Then
If InStr(Attach, ";") = 0 Then
If IsFile(Attach) Then
.Attachments.Add Attach
Else
ErrMsg "Attachment file: " & Attach & " not found."
End If
Else
strAttachments = Attach
If AttachmentsOK(Attach) Then
Do
.Attachments.Add Left(strAttachments,
InStr(strAttachments, ";") - 1)
strAttachments = Trim(MID(strAttachments,
InStr(strAttachments, ";") + 1))
Loop While InStr(strAttachments, ";") <> 0
If Len(strAttachments) > 0 Then .Attachments.Add
strAttachments
Else
EmailSent = False
GoTo ExitHere
End If
End If
End If
End With

If ViewAfter = True Then
objMsg.Display
Else
objMsg.Send
End If

GoTo ExitHere

ExitHere:
objMsg.Close
Set objMsg = Nothing
objOutlook.Close
Set objOutlook = Nothing
ns.logoff
Set ns = Nothing

Exit Sub

' Error handling block added by Error Handler Add-In. DO NOT EDIT this
block of code.
' Automatic error handler last updated at 11-12-2002 17:34:33
'ErrorHandler:$$D=11-12-2002 'ErrorHandler:$$T=17:34:33
HandleErr:
Select Case Err.Number
Case 287
Resume Next
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description,
vbCritical, "basLMS.CreateOLMessage"
'ErrorHandler:$$N=basLMS.CreateOLMessage
End Select
' End Error handling block.
End Sub

'================= END CODE ==================================

 
 
 

Am I doing this correctly?

Post by Wayne Morg » Tue, 20 Jan 2004 13:14:44

ou don't need to GoTo ExitHere right above the ExitHere routine because
that is where you'll go next anyway. In your error handler, if the error is
287 you Resume Next, but if the error is Case Else you give a message and
drop out the bottom of the sub. You should put in a Resume ExitHere before
the End Sub so that you go back up and clean up your variables. At the start
of your ExitHere routine you may want to use On Error Resume Next, that way
if you get to the ExitHere (due to an error that was run through the error
routine) be fore you Open something, such as objMessage, you won't receive
an error that it can't close the unopened item.

GoTo ExitHere 'Remove this

ExitHere:
On Error Resume Next
objMsg.Close
Set objMsg = Nothing
objOutlook.Close
Set objOutlook = Nothing
ns.logoff
Set ns = Nothing

Exit Sub

' Error handling block added by Error Handler Add-In. DO NOT EDIT this
block of code.
' Automatic error handler last updated at 11-12-2002 17:34:33
'ErrorHandler:$$D=11-12-2002 'ErrorHandler:$$T=17:34:33
HandleErr:
Select Case Err.Number
Case 287
Resume Next
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description,
vbCritical, "basLMS.CreateOLMessage"
'ErrorHandler:$$N=basLMS.CreateOLMessage
End Select
' End Error handling block.
Resume ExitHere
End Sub


--
Wayne Morgan
Microsoft Access MVP


<LaurenW> wrote in message
news: XXXX@XXXXX.COM ...


 
 
 

Am I doing this correctly?

Post by Alan Lan » Tue, 20 Jan 2004 13:26:47

Hi Lauren:

I just see one little problem...If you get an error, what happens? Do
your objects get closed? No. Why? Because there is no "Resume ExitHere"
at the end of your Error Trap routine, to take processing back up to where
you close all your objects. Add "Resume ExitHere" (without quotes of
course) right above the "Exit Sub" line, and it should work correctly. Good
luck.

Alan
 
 
 

Am I doing this correctly?

Post by Lauren » Tue, 20 Jan 2004 15:46:54


Thanks Wayne. I had a suspicion I was overlooking something here.


On Mon, 19 Jan 2004 04:14:44 GMT, "Wayne Morgan"
 
 
 

Am I doing this correctly?

Post by Pink Panth » Tue, 20 Jan 2004 18:58:32

Hi Lauren,

In addition to the other comments

If IsOutlook = 0 Then
ErrMsg NoOutlookMsg
Exit Sub
End If

Needs to be

If IsOutlook = 0 Then
ErrMsg NoOutlookMsg
'make sure all your code exits through one point in a procedure!
'ie the Exit_Here block
Goto Exit_Here
End If

also

ExitHere:
On Error Resume Next
'it's good habit IMO to close/release your objects in reverse order
'to that which you created them
'ie objOutlook was instantiated first so release last
objMsg.Close
Set objMsg = Nothing
ns.logoff
Set ns = Nothing
objOutlook.Close
Set objOutlook = Nothing

Exit Sub

I've never seen a problem caused by not releasing in order (or by not
cleaning up) but you can never have too many good habit's I reckon.

Regards,

Peter
 
 
 

Am I doing this correctly?

Post by Lauren » Wed, 21 Jan 2004 01:13:35


Thanks! You guys are great!


On 19 Jan 2004 20:58:32 +1100, Pink Panther < XXXX@XXXXX.COM >
 
 
 

Am I doing this correctly?

Post by dXXXfento » Wed, 21 Jan 2004 02:37:09


XXXX@XXXXX.COM (Wayne Morgan) wrote in
<UQIOb.10144$ XXXX@XXXXX.COM >:


I would never do that (On Error Resume Next) because my experience
is that if you don't set it back on, it sometimes remains off.

Instead, I'd test if the objects are Nothing in order to see if
they need to be cleaned up:

ExitHere:
If Not (objMsg Is Nothing) Then
objMsg.Close
Set objMsg = Nothing
End If
If Not (objOutlook Is Nothing) Then
objOutlook.Close
Set objOutlook = Nothing
End If
ns.logoff
Set ns = Nothing
Exit Sub

Or you could do something like Steve Jorgensen does and have a
subroutine that does this for you, and pass the object variable to
it. In that subroutine, you could have error handling appropriate
to the particular errors you might encounter in that particular
block, so that any errors would get handled appropriately without
getting you in a loop.

--
David W. Fenton http://www.yqcomputer.com/ ~dfenton
dfenton at bway dot net http://www.yqcomputer.com/ ~dfassoc