Clearing textboxes after sending email - ms-access

Im working on a form that sends an email on the click of a command button, but after the email is made the text boxes the user must fill in do not automatically clear out. I have provided the code I have below. Is there some other kind of clearing command I am missing maybe?
Thanks in advance :)
On Error GoTo errhandle
Me.Filter = "CurrentDate= #" & Format(Me!CurrentDate, "yyyy\-mm\-dd") & "# and DiscoverTime= '" & Me!DiscoverTime & "' and TailNumber= '" & Me!TailNumber & "' and FleetID= '" & Me!FleetID & "'"
Me.FilterOn = True
DoCmd.SendObject acSendForm, "frmETIC", acFormatPDF, "email", "", "", "Recovery Report", "Attached is the submitted Recovery Report"
exiterr:
Exit Sub
errhandle:
If Err.Number <> 2501 Then
MsgBox ("Email cancelled!")
End If
Resume exiterr
Me.CurrentDate = Null
Me.DiscoverTime = Null
Me.TailNumber = Null
Me.FleetID = Null

You are exiting the Sub before clearing. Try the following.
On Error GoTo errhandle
Me.Filter = "CurrentDate= #" & Format(Me!CurrentDate, "yyyy\-mm\-dd") & "# and DiscoverTime= '" & Me!DiscoverTime & "' and TailNumber= '" & Me!TailNumber & "' and FleetID= '" & Me!FleetID & "'"
Me.FilterOn = True
DoCmd.SendObject acSendForm, "frmETIC", acFormatPDF, "email", "", "", "Recovery Report", "Attached is the submitted Recovery Report"
Me.CurrentDate = vbNullString
Me.DiscoverTime = vbNullString
Me.TailNumber = vbNullString
Me.FleetID = vbNullString
exiterr:
Exit Sub
errhandle:
If Err.Number <> 2501 Then
MsgBox ("Email cancelled!")
End If
Resume exiterr

Related

Email from Access 2010 with Outlook

The code below creates an email. It only works on the first record of the db. Also, the code puts all of the fields in the body. I would like it to only put the fields that have "Request from Finance" in the field.
Private Sub cmdEMail_Click()
On Error GoTo cmdEMail_Click_Error
Dim OutApp As Object
Dim strEMail As String
Dim OutMail As Object
Dim strbody As String
strEMail = Me.EMail
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Please add the following time codes to Oracle for Lilly Project 1005894. Thank you!" & vbCrLf _
& "" & vbCrLf & "INSTRUCTIONS:" & vbCrLf _
& "" & vbCrLf & "Make sure the Task Description starts with EU. This is automatically added by entering EU in the Contract field on the form." & vbCrLf _
& "" & vbCrLf & "If you wish to keep track of your time code requests, CC: yourself on the e-mail and considering entering a compound name or other identifier in the subject line. Alternatively, save a copy of the spreadsheet with your time codes to your desktop." & vbCrLf _
& "" & vbCrLf & "WRITING TASK NUMBER NAME =" & [Forms]![frm_Regulatory]![WriterTaskNumberName] & vbCrLf _
& "" & vbCrLf & "ADD DRAFT TASK NUMBER NAME =" & [Forms]![frm_Regulatory]![AddDraftTaskNumberName] & vbCrLf _
& "" & vbCrLf & "EDIT TASK NUMBER NAME =" & [Forms]![frm_Regulatory]![EditTaskNumberName] & vbCrLf _
& "" & vbCrLf & "QUALITY REVIEW TASK NUMBER NAME =" & [Forms]![frm_Regulatory]![DataIntegrityQRTaskNumber] & vbCrLf _
& "" & vbCrLf & "Task Description =" & [Forms]![frm_Regulatory]![Text186] & vbCrLf
On Error Resume Next
If Me.ActiveWritingCode = "Request from Finance" Then
With OutMail
.To = strEMail
.CC = ""
.BCC = ""
.Subject = "Lilly EU 1005894 Time Code Request"
.Body = strbody & vbNewLine & .Body
.Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End If
On Error GoTo 0
Exit Sub
cmdEMail_Click_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdEMail_Click of Sub Form_frm_Regulatory"
End Sub
Here is a generic script to loop through records in a table.
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM Contacts")
'Check to see if the recordset actually contains rows
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst 'Unnecessary in this case, but still a good habit
Do Until rs.EOF = True
'Perform an edit
'rs.Edit
'rs!VendorYN = True
'rs("VendorYN") = True 'The other way to refer to a field
'rs.Update
'Save contact name into a variable
'sContactName = rs!FirstName & " " & rs!LastName
'Move to the next record. Don't ever forget to do this.
rs.MoveNext
Loop
Else
MsgBox "There are no records in the recordset."
End If
MsgBox "Finished looping through records."
rs.Close 'Close the recordset
Set rs = Nothing 'Clean up
Here is another example which is quite good.
https://msdn.microsoft.com/en-us/library/bb243789(v=office.12).aspx

How to solve vba runtime error 3420

I have the following the detect duplicate product name when user enters a new record.
Private Sub ProdName_BeforeUpdate(Cancel As Integer)
Dim Product As String
Dim stLinkCriteria As String
Dim rsc As DAO.Recordset
Set rsc = Me.RecordsetClone
Product = Me.ProdName.value
stLinkCriteria = "[ProdName]=" & "'" & Product & "'"
If DCount("ProdName", "ProdProduct", stLinkCriteria) > 0 Then
Me.Undo
MsgBox "Warning duplicate entry " _
& Product & " has already been entered." _
& vbCr & vbCr & "You will now be taken to the record.", vbInformation _
, "Duplicate Information"
'Go to record of original product name
rsc.FindFirst stLinkCriteria
Me.Bookmark = rsc.Bookmark
End If
Set rsc = Nothing
End Sub
The code checks and finds duplicate but after it displays the following error and doesn't go to the orginal record:
Run-time error '3420'
Object invalid or no longer set
Please can someone help me get it right?
Try to avoid the undo (or move it to the end) and do cancel the update:
If DCount("ProdName", "ProdProduct", stLinkCriteria) > 0 Then
Cancel = True
MsgBox "Warning duplicate entry " _
& Product & " has already been entered." _
& vbCr & vbCr & "You will now be taken to the record.", vbInformation _
, "Duplicate Information"
'Go to record of original product name
rsc.FindFirst stLinkCriteria
Me.Bookmark = rsc.Bookmark
End If
And you may even skip the DCount and must use the Text property:
Product = Me!ProdName.Text
stLinkCriteria = "[ProdName]=" & "'" & Product & "'"
rsc.FindFirst stLinkCriteria
Cancel = Not rsc.NoMatch
If Cancel = True Then
MsgBox "Warning duplicate entry " _
& Product & " has already been entered." _
& vbCr & vbCr & "You will now be taken to the record.", vbInformation _
, "Duplicate Information"
'Go to record of original product name
Me.Bookmark = rsc.Bookmark
End If
The Run-time Error 3420, Object Invalid or No Longer Set occurs when the form status is dirty, you can simply set the form dirty to false before .findFirst call.
If Me.Dirty Then
Me.Dirty = False
End If
Me.Recordset.FindFirst stLinkCriteria

Runtime error 2465: Can't find the field "|1" referenced

I inherited this Access Database for a client and was tasked with updating some things in it. Unfortunately I don't know much about VBA/Access. I keep receiving the error Runtime error 2465: Can't find the field "|1" referenced on the DoCmd.SendObject line. I've done a lot of searching and it's such a cryptic error I'm getting nowhere. Can someone explain to me what is wrong here? I'm assuming it's a syntax issue somewhere or I'm using variables wrong or something.
Private Sub cbCompleted_AfterUpdate()
If cbCompleted = -1 Then
tbCompleted = Date
tbCompleted.Locked = True
Doctor_Name.Locked = True
Department.Locked = True
Start_Date.Locked = True
Specialty.Locked = True
Doctor_.Locked = True
Taxonomy_.Locked = True
DepartmentCombo.Locked = True
UPIN_.Locked = True
SpecialtyCombo.Locked = True
Dim sd As String
Dim dn As String
sd = "" & [Start Date]
dn = "" & [Doctor Name]
DoCmd.SendObject acSendNoObject, , , "Tim,Keith,Yvonne,Sandy,susan#domain.org,Vicki#domain.org", "Tom,Barbara,Rachael,Penny,Troy,bernasue#domain.org", , "Doctor " & dn & " " & "Start Date:" & " " & sd, dn & " " & "is scheduled to start" & sd & vbNewLine & "NPI# :" & [NPI#] & vbNewLine & "Specialty: " & [Speciality#] & vbNewLine & "Department/Practice: " & [Department#] & vbNewLine & "Provider# for HR/Acctg: " & [Doctor#], True
Else
'[snip] Unlock all fields locked above
End If
End Sub
Thanks much

Access VBA passing parameters vbs file

i am creating a .vbs file that should open access, and inside access a form call "Issue Details", but passing a parameter, meaning that if i have 10 issues in my "Issues" table a vbs file is created for each one and when clicked should open the right record(would be one ID for each record in the table). It is so far opening access and it is opening the form(Issue Details) but it is blank. What am i missing? Help, getting crazy here ... Check code below. The weird thing here is that if i double click it again it will refresh and open the right record without opening anymore windows.. How can i fix that? I dont want to do it twice :)
Public Sub sendMRBmail(mrbid)
DoCmd.OpenForm "Issue Details", , , "[ID] = " & mrbid
End Sub
Private Sub Create_Click()
On Error GoTo Err_Command48_Click
Dim snid As Integer
snid = Me.ID
Dim filename As String
filename = "S:\Quality Control\vbs\QC" & snid & ".vbs"
Dim proc As String
proc = Chr(34) & "sendMRBmail" & Chr(34)
Dim strList As String
strList = "On Error Resume Next" & vbNewLine
strList = strList & "dim accessApp" & vbNewLine
strList = strList & "set accessApp = createObject(" & Chr(34) & "Access.Application" & Chr (34)")" & vbNewLine
strList = strList & "accessApp.OpenCurrentDataBase(" & Chr(34) & "S:\Quality Control\Quality DB\Quality Database.accdb" & Chr(34) & ")" & vbNewLine
strList = strList & "accessApp.Run " & proc & "," & Chr(34) & snid & Chr(34) & vbNewLine
strList = strList & "set accessApp = nothing" & vbNewLine
Open filename For Output As #1
Print #1, strList
Close #1
Err_Command48_Click:
If Err.Number <> 0 Then
MsgBox "Email Error #: " & Err.Number & ", " & "Description: " & Err.Description
Exit Sub
End If
End Sub
This is what is inside a created vbs file
On Error Resume Next
dim accessApp
set accessApp = GetObject("S:\Quality Control\Quality DB\Quality Database.accdb")
accessApp.Run"sendMRBmail","231"
set accessApp = nothing
Thanks to all that made inputs, i already found the answer. I added acFormEdit at the end of my DoCmd and it worked, check below:
DoCmd.OpenForm "Issue Details", , , "[ID] = " & mrbid, acFormEdit

How do I display form properties in a message box?

I have the following code, which should loop through all the forms in the project, and give a messagebox with the settings for each form. I know the loop is correct, because I use the loop elsewhere and I just copied it. Why is the messagebox blank?
For Each frm In CurrentProject.AllForms
DoCmd.OpenForm frm.Name, acDesign
mess = "Form: " & frm.Name & vbCrLf & " Allow Addition: " & CStr(frm.AllowAdditions) & vbCrLf & "Allow Deletions: " & CStr(frm.AllowDeletions) & vbCrLf & " Allow Edit: " & CStr(frm.AllowEdits)
MsgBox (mess)
DoCmd.Close acForm, frm.Name, acSaveYes
Next frm
Set frm = Nothing
With Remou's tips, I got the following to work:
For Each frm In CurrentProject.AllForms
DoCmd.OpenForm frm.Name, acDesign
Set frm = Forms(frm.Name)
mess = "Form: " & frm.Name & vbCrLf & " Allow Addition: " & CStr(frm.AllowAdditions) & vbCrLf & "Allow Deletions: " & CStr(frm.AllowDeletions) & vbCrLf & " Allow Edit: " & CStr(frm.AllowEdits)
MsgBox (mess)
DoCmd.Close acForm, frm.Name, acSaveNo
Next frm
Set frm = Nothing
You cannot access form properties without opening the form. You should only use Set with objects, not with strings.
See Access 2010: Which form control fires a macro?
For Each f In CurrentProject.AllForms
DoCmd.OpenForm f.Name, acDesign
Set frm = Forms(f.Name)
mess = "Form: " & frm.Name & vbCrLf
mess = mess & " Allow Addition: " & CStr(frm.AllowAdditions) & vbCrLf
mess = mess & "Allow Deletions: " & CStr(frm.AllowDeletions) & vbCrLf
mess = mess & " Allow Edit: " & CStr(frm.AllowEdits)
MsgBox mess
DoCmd.Close acForm, f.Name, acSaveNo
Next