Full Version: Trouble getting code to run sequentially
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
snsd
Hi:

I have a Save command button that I created using the Access built-in wizard. The On Click event saves the current record, generates an email (via Outlook) and creates a hyperlink on the form. The save function works fine.

If I place the email code before the hyperlink code, the email is created but not the hyperlink - and vice versa. Both pieces of code seem to work fine on their own. What do I need to do to make sure that both blocks of code run and not just the first one? Do I have a problem with the syntax of my End and Exit statements?

I'm a bit of a newbie so likely have some bug in the code.

Thanks in advance for any help you might be able to give.
CODE
Private Sub cmdSaveRecord_Click()
'Saves current record
On Error GoTo Err_cmdSaveRecord_Click
    DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

'2005-09-21
''Creates hyperlink to underlying client response file/image

Dim strInputEmail As String
Dim strInputRightFax As String
'Dim strInputViewFinder As String

Const strPathEmail = "\\Sew00016.maple.fg.rbc.com\wrkgrp\CCA RESPONSE TRACKING\CART Email\"
Const strPathRightFax = "\\Sew00016.maple.fg.rbc.com\wrkgrp\CCA RESPONSE TRACKING\CART RightFax\"
Const strPathViewFinder = "\\Sew00016.maple.fg.rbc.com\wrkgrp\CCA RESPONSE TRACKING\CART ViewFinder\"

strInputEmail = strPathEmail & Me.txtIDnumber & ".msg"
strInputRightFax = strPathRightFax & Me.txtIDnumber & ".tif"
strInputViewFinder = strPathViewFinder & Me.txtIDnumber & ".pdf"

    If Me.lstDeliveryMethod = "email" Then
        Me.txtHyperlink = strInputEmail
    ElseIf Me.lstDeliveryMethod = "RightFax" Then
        Me.txtHyperlink = strInputRightFax
    ElseIf Me.lstDeliveryMethod = "ViewFinder" Then
        Me.txtHyperlink = strInputViewFinder
    End If
Exit Sub

'2005-09-28
'email to management for unsolicited instructions

Dim strEmailAddresses As String

strEmailAddresses = "xyz@abc.com"

If Me.chkUnsolicitedInstructions = -1 Then
DoCmd.SendObject , "", "", strEmailAddresses & "", "", "", "Unsolicited Response RECEIVED - " & _
"Property: " & Me.txtPropertyName & _
"  Client: " & Me.cboClientName.Column(1), _
"Response Tracking has received an 'Unsolicited Instruction' " & _
"It has been logged in CART. Processing coordinators are to action instructions " & _
"within 48 hours of receipt." & _
vbCrLf & vbCrLf & _
"CART ID: " & Me.txtIDnumber & _
vbCrLf & vbCrLf & _
"Client: " & Me.cboClientName.Column(1) & _
vbCrLf & _
"Account Number (where available): " & Me.txtAccountNumber & _
vbCrLf & vbCrLf & _
"Property: " & Me.txtPropertyName & _
vbCrLf & _
"Event Type: " & Me.cboEventType.Column(1) & _
vbCrLf & _
"CCA Number (where available): " & Me.txtCCAnumber & _
vbCrLf & vbCrLf & _
"Staff Name: " & Me.lstStaffName & _
vbCrLf & _
vbCrLf & "Please contact the above listed staff if further details are required."
Exit Sub
End If

Exit_cmdSaveRecord_Click:
    Exit Sub

Err_cmdSaveRecord_Click:
    If Err.Number <> 2501 Then
        MsgBox Err.Number & "; " & Err.Description
    End If
    Resume Exit_cmdSaveRecord_Click
End Sub
fkegley
Maybe I'm missing something but it looks like the first Exit Sub instruction is causing your problem, it is unconditional.
snsd
Thanks Frank. Hope you're on to something. How do I correct it? Do I need to place the Exit Sub before the End If statement like I did in the second block? Is that a general rule of thumb? Or am I missing the boat completely?

Anyhow, I did try that and the same challenge exists. If the unconditional statement is causing the challenge, any idea why when I put the email block of code at the top of the sub, the email code runs but not the hyperlink code?

Thanks again for your help.

Dave
fkegley
Only if you want the Exit Sub command to be executed at that point should it be unconditional. If its execution depends on a condition being true, then you would need it inside an If...End If structure.

If you always want both chunks of code executed then I would think you don't need Exit Sub where you have it. I would make a comment out of it.
snsd
Jack:

Bingo! Thanks for the explanation as well. I pretty much fumble through this stuff and it's nice to actually understand what's going on for a change.

Dave

thanks.gif
fkegley
You're welcome, Bill.
snsd
Sorry about that John.

Humblest apologies.

Larry laugh.gif
fkegley
Larry, it's OK. <LOL>
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.