D
dcc15 via AccessMonster.com
I have a Form with a send message cmdbutton that I have been trying to
automate by adding some code (am new to VBA and learning). I understand some
of what I have done but not all...
When the cmdButton is clicked a "Object Required" MA Access message pops up.
I have been over the code and can't see where or which line(s) might be wrong.
Any help or suggestions would be appreciated.
CODE:
Private Sub cmdSendNotification_Click()
On Error GoTo Err_cmdSendNotification_Click
Dim stWhere As String '-- Criteria for DLookup
Dim varTo As Variant '-- Address for SendObject
Dim Text As String '-- E-mail text
Dim OpenDate As Variant '-- Rec date for e-mail text
Dim Subject As String '-- Subject line of e-mail
Dim Number As String '-- The NCMR number from form
Dim stWho As String '-- Reference to TBLUsers
Dim OpenBy As String '-- User who opened NCMR
Dim SQL As String '-- Create SQL update statement
Dim errLoop As Error
'-- Name/Group to send notification to
stWho = Me.ncmrSentTo
stWhere = TBLUsers.User = stWho
'-- Looks up email address from TblUsers
varTo = DLookup("", "TBLUsers", stWhere)
Subject = "!!New NCMR Problem!!"
Number = Me.ncmrnum
OpenDate = Me.ncmrdateopen
'-- User who opened NCMR
OpenBy = Me.ncmrby
Text = "A new NCMR has been reported. " & Chr$(13) & Chr$(13) & _
"NCMR Number: " & Number & Chr$(13) & _
"This NCMR has been opened by: " & OpenBy & Chr$(13) & _
"Open Date: " & OpenDate & Chr$(13) & Chr$(13) & Chr$(13) & _
"This is an automated message. Please do not respond to this e-
mail."
'Write the e-mail content
DoCmd.SendObject , , acFormatTXT, varTo, , , Subject, Text, -1
'Set the update statement to disable command button once e-mail is sent
SQL = "UPDATE TBLncmr SET TBLncmr.ncmrnotisent = -1 " & _
"Where TBLncmr.ncmrnum = " & Me.ncmrnum & ";"
On Error GoTo Err_Execute
CurrentDb.Execute SQL, dbFailOnError
On Error GoTo 0
'Requery checkbox to show checked after update statement has ran
'and disable send notification command button
Me.ncmrnotisent.Requery
Me.ncmrnotisent.SetFocus
Me.cmdSendNotification.Enabled = False
Exit Sub
Err_Execute:
' Notify user of any errors that result from
' executing the query.
If DBEngine.Errors.Count > 0 Then
For Each errLoop In DBEngine.Errors
MsgBox "Error number: " & errLoop.Number & vbCr & _
errLoop.Description
Next errLoop
End If
Resume Next
Exit_cmdSendNotification_Click:
Exit Sub
Err_cmdSendNotification_Click:
MsgBox Err.Description
Resume Exit_cmdSendNotification_Click
End Sub
automate by adding some code (am new to VBA and learning). I understand some
of what I have done but not all...
When the cmdButton is clicked a "Object Required" MA Access message pops up.
I have been over the code and can't see where or which line(s) might be wrong.
Any help or suggestions would be appreciated.
CODE:
Private Sub cmdSendNotification_Click()
On Error GoTo Err_cmdSendNotification_Click
Dim stWhere As String '-- Criteria for DLookup
Dim varTo As Variant '-- Address for SendObject
Dim Text As String '-- E-mail text
Dim OpenDate As Variant '-- Rec date for e-mail text
Dim Subject As String '-- Subject line of e-mail
Dim Number As String '-- The NCMR number from form
Dim stWho As String '-- Reference to TBLUsers
Dim OpenBy As String '-- User who opened NCMR
Dim SQL As String '-- Create SQL update statement
Dim errLoop As Error
'-- Name/Group to send notification to
stWho = Me.ncmrSentTo
stWhere = TBLUsers.User = stWho
'-- Looks up email address from TblUsers
varTo = DLookup("", "TBLUsers", stWhere)
Subject = "!!New NCMR Problem!!"
Number = Me.ncmrnum
OpenDate = Me.ncmrdateopen
'-- User who opened NCMR
OpenBy = Me.ncmrby
Text = "A new NCMR has been reported. " & Chr$(13) & Chr$(13) & _
"NCMR Number: " & Number & Chr$(13) & _
"This NCMR has been opened by: " & OpenBy & Chr$(13) & _
"Open Date: " & OpenDate & Chr$(13) & Chr$(13) & Chr$(13) & _
"This is an automated message. Please do not respond to this e-
mail."
'Write the e-mail content
DoCmd.SendObject , , acFormatTXT, varTo, , , Subject, Text, -1
'Set the update statement to disable command button once e-mail is sent
SQL = "UPDATE TBLncmr SET TBLncmr.ncmrnotisent = -1 " & _
"Where TBLncmr.ncmrnum = " & Me.ncmrnum & ";"
On Error GoTo Err_Execute
CurrentDb.Execute SQL, dbFailOnError
On Error GoTo 0
'Requery checkbox to show checked after update statement has ran
'and disable send notification command button
Me.ncmrnotisent.Requery
Me.ncmrnotisent.SetFocus
Me.cmdSendNotification.Enabled = False
Exit Sub
Err_Execute:
' Notify user of any errors that result from
' executing the query.
If DBEngine.Errors.Count > 0 Then
For Each errLoop In DBEngine.Errors
MsgBox "Error number: " & errLoop.Number & vbCr & _
errLoop.Description
Next errLoop
End If
Resume Next
Exit_cmdSendNotification_Click:
Exit Sub
Err_cmdSendNotification_Click:
MsgBox Err.Description
Resume Exit_cmdSendNotification_Click
End Sub