Hi,
I have a button which sends email. The problem is whenever I clicked the button, and it finishes, then the database become error in terms of a textbox in every form which has Date Picker, it stopped working. I cannot fill the textbox using the date picker anymore (either a click on a particular date or click on Today button).. But the textbox still can be filled manually.
Do you think this is because of the button? Maybe I should close something down because there is still a process going on from the button? (docmd.close or set x=nothing or anything with CDO because I am new using CDO)
anyway, heres the code of the button:
I have a button which sends email. The problem is whenever I clicked the button, and it finishes, then the database become error in terms of a textbox in every form which has Date Picker, it stopped working. I cannot fill the textbox using the date picker anymore (either a click on a particular date or click on Today button).. But the textbox still can be filled manually.
Do you think this is because of the button? Maybe I should close something down because there is still a process going on from the button? (docmd.close or set x=nothing or anything with CDO because I am new using CDO)
anyway, heres the code of the button:
Code:
Private Sub EmailFollowUpButton_Click()
'this button will check every customer orders on TblCustOrder where Email1Sent is False and Email1DueDate is today or less than today.
'if the conditions are fulfilled, system will automatically send email as reminder to make a follow up call about the product.
Dim cdoConfig
Dim msgOne
Dim dbs As Database
Dim rst As DAO.Recordset
Dim notSentQry As String
Dim emailSentUpdateQry As String
'details about the order
Dim cust As String
Dim PONumber As String
Dim jobName As String
Dim dateSent As String
Dim dateToday As String
Dim unitIDRst As DAO.Recordset
Dim SN As String
Dim units As String
Dim selectUnitQry As String
Dim emailMessage As String
notSentQry = "SELECT CustOrderID, CustomerID, PONumber, JobName, DateSent, Email1DueDate FROM TblCustOrder WHERE Email1Sent=False AND Email1DueDate<=Now()"
dateToday = Format(Now(), "dd/mm/yyyy")
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(notSentQry)
If rst.EOF And rst.BOF Then
MsgBox ("No follow up should be made for today")
Exit Sub
Else
rst.MoveFirst
Do While rst.EOF = False
cust = DLookup("CustomerName", "TblCustomer", "CustomerID=" & rst("CustomerID"))
PONumber = rst("PONumber")
jobName = Nz(rst("JobName"), "")
'dateSent = rst("DateSent")
emailMessage = vbCrLf + emailMessage + cust + " PO Number: " + PONumber + ", Job Name: " + jobName + " " + vbCrLf
selectUnitQry = "SELECT UnitID, SerialNumber FROM TblCustOrderUnit WHERE OrderID=" & rst("CustOrderID")
Set unitIDRst = dbs.OpenRecordset(selectUnitQry)
unitIDRst.MoveFirst
Do While unitIDRst.EOF = False
units = DLookup("BSUnitID", "TblUnits", "UnitID=" & unitIDRst("UnitID"))
SN = unitIDRst("SerialNumber")
emailMessage = emailMessage + "+++" + units + " Serial Number: " + SN + vbCrLf
unitIDRst.MoveNext
Loop
emailSentUpdateQry = "UPDATE TblCustOrder SET Email1Sent=TRUE WHERE CustOrderID=" & rst("CustOrderID")
DoCmd.SetWarnings False
DoCmd.RunSQL emailSentUpdateQry
DoCmd.SetWarnings True
rst.MoveNext
Loop
Set cdoConfig = CreateObject("CDO.Configuration")
With cdoConfig.Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServerPort) = 25 ' (your port number) usually is 25
.Item(cdoSMTPServer) = "smtp.internet.com" ' your SMTP server goes here
'.Item(cdoSendUserName) = "My Username"
'.Item(cdoSendPassword) = "myPassword"
.Update
End With
Set msgOne = CreateObject("CDO.Message")
Set msgOne.Configuration = cdoConfig
msgOne.To = "mail@mail.com" 'recipient email address
msgOne.From = "mail2@mail.com" 'sender email address
msgOne.Subject = "Reminder for Follow Up Orders Due Today '" & dateToday & "'" 'email subject
msgOne.TextBody = "This is an email reminder to follow up these orders: " & vbCrLf & vbCrLf & emailMessage 'email body
msgOne.Send
MsgBox ("Message Sent")
End If
Set rst = Nothing
Set unitIDRst = Nothing
Set dbs = Nothing
End Sub