Trying to grab multiple fiedld from querydef and transfer to default email - email

I have been working with a database and received help getting the correct function to occur in that the code is working with a query and then looping through each report group (by name) and populating the fields of a report and saving the report by individual name. The last part of this which I have been struggling with. One of the fields in the recordset is Sup_Email, I’m trying to pass the report for sup with the email address in the above field to my default email client. I can’t figure how to isolate the second variable, keep it together with the right report and pass them both to email. Any help would be greatly appreciated.
Private Sub CreateReports_Click()
Dim x As String
Dim y As String
Dim z As String
Dim StrSQL
Dim StrEmail
Dim stWhereStr As String 'Where Condition'
Dim stSection As String 'Selection from drop down list
Dim stfile As String
StrSQL = "SELECT distinct[Sup] FROM ([OPDA ISSR- Courts Users by District/Cir])"
StrEmail = "SELECT [Sup_email] FROM ([OPDA ISSR- Courts Users by District/Cir])"
y = Year(date)
Dim db As DAO.Database
Dim rst As DAO.Recordset
Set db = CurrentDb
Dim qdTemp As DAO.QueryDef
Set qdTemp = db.CreateQueryDef("", StrSQL)
Set rst = qdTemp.OpenRecordset()
If rst.EOF And rst.BOF Then
MsgBox "No data available for the Ledger Process routine."
Else
rst.MoveFirst
Do While Not rst.EOF
x = rst![Sup]
z = rst![Sup_email]
stDocName = "Courts - ISSR Recertification Report"
stWhereStr = "[OPDA ISSR- Courts Users by District/Cir].[SUP]= '" & x & "'"
stfile = "P:\DFI\FIB\Access Tables\FibCustomers\ISSR Reports\Courts\" & x & " - " & y & " FedInvest InvestOne Recertification.pdf"
DoCmd.OpenReport stDocName, acPreview, , stWhereStr
DoCmd.OutputTo acOutputReport, stDocName, acFormatPDF, stfile
DoCmd.Close acReport, stDocName
rst.MoveNext
Loop
End If
rst.Close
Set rst = Nothing
End Sub
Each Sup has only one email address. The way it is setup is that a query (the OPDA ISSR- Courts Users by District/Cir query from above) pulls the first and last name of the supervisor and concatenates it into Sup, it also has their phone and email address (Sup_email), then has the employee, their accounts and access level, this information all applies to each field of the form. The direction I was sent yesterday created a query recordset (it's was continuing to loop). I tried then to add the field for email, and it's all gone awry since.

I have modified your code so that it uses the email address from the table and will create the report, then send.
Private Sub CreateReports_Click()
Dim x As String
Dim y As String
Dim StrSQL As String
Dim stWhereStr As String 'Where Condition'
Dim stSection As String 'Selection from drop down list
Dim stfile As String
Dim stDocName As String
Dim StrEmail As String
StrSQL = "SELECT DISTINCTROW [OPDA ISSR- Courts Users by District/Cir].[Sup], [OPDA ISSR- Courts Users by District/Cir].SupEmail " & _
"FROM [OPDA ISSR- Courts Users by District/Cir];"
y = Year(Date)
Dim db As DAO.Database
Dim rst As DAO.Recordset
Set db = CurrentDb
Dim qdTemp As DAO.QueryDef
Set qdTemp = db.CreateQueryDef("", StrSQL)
Set rst = qdTemp.OpenRecordset()
If rst.EOF And rst.BOF Then
MsgBox "No data available for the Ledger Process routine."
Else
Debug.Print rst.Fields.Count
rst.MoveFirst
Do While Not rst.EOF
x = rst![Sup]
StrEmail = rst![Supemail]
stDocName = "Courts - ISSR Recertification Report"
stWhereStr = "[OPDA ISSR- Courts Users by District/Cir].[SUP]= '" & x & "'"
stfile = "P:\DFI\FIB\Access Tables\FibCustomers\ISSR Reports\Courts\" & x & " - " & y & " FedInvest InvestOne Recertification.pdf"
DoCmd.OpenReport stDocName, acPreview, , stWhereStr
DoCmd.SendObject acSendReport, stDocName, acFormatPDF, StrEmail, , , "My Subject here", "your report"
DoCmd.OutputTo acOutputReport, stDocName, acFormatPDF, stfile
DoCmd.Close acReport, stDocName
rst.MoveNext
Loop
End If
rst.Close
Set rst = Nothing
End Sub

Related

send email with VBA with no user interaction with email client

I'm trying to get this code to send the attachment via email. But their will be several hundred attachments, so i don't want the user to need to interact with the email client. Any suggestions would be greatly appreciated.
Private Sub CreateReports_Click()
Dim x As String
Dim y As String
Dim StrSQL As String
Dim stWhereStr As String 'Where Condition'
Dim stSection As String 'Selection from drop down list
Dim stfile As String
Dim stDocName As String
Dim StrEmail As String
StrSQL = "SELECT DISTINCTROW [OPDA ISSR- Courts Users by District/Cir].[Sup], [OPDA ISSR- Courts Users by District/Cir].SupEmail " & _
"FROM [OPDA ISSR- Courts Users by District/Cir];"
y = Year(Date)
Dim db As DAO.Database
Dim rst As DAO.Recordset
Set db = CurrentDb
Dim qdTemp As DAO.QueryDef
Set qdTemp = db.CreateQueryDef("", StrSQL)
Set rst = qdTemp.OpenRecordset()
If rst.EOF And rst.BOF Then
MsgBox "No data available for the Ledger Process routine."
Else
Debug.Print rst.Fields.Count
rst.MoveFirst
Do While Not rst.EOF
x = rst![Sup]
StrEmail = rst![Supemail]
stDocName = "Courts - ISSR Recertification Report"
stWhereStr = "[OPDA ISSR- Courts Users by District/Cir].[SUP]= '" & x & "'"
stfile = "P:\DFI\FIB\Access Tables\FibCustomers\ISSR Reports\Courts\" & x & " - " & y & " FedInvest InvestOne Recertification.pdf"
DoCmd.OpenReport stDocName, acPreview, , stWhereStr
DoCmd.SendObject acSendReport, stDocName, acFormatPDF, StrEmail, , , "My Subject here", "your report"
DoCmd.OutputTo acOutputReport, stDocName, acFormatPDF, stfile
DoCmd.Close acReport, stDocName
rst.MoveNext
Loop
End If
rst.Close
Set rst = Nothing
End Sub
You could either use your local email client's API and open a hidden instance to create and send emails (e.g. Outlook, Lotus Notes) or you could use the CDO object.
http://www.rondebruin.nl/win/s1/cdo.htm
Even though the above example is in Excel, the code works great. The only thing you will have to make sure if you're using the CDO object is if the necessary ports are open. Also check the related threads to this question, tons of valid information if you want to go with a specific email client.
The best way to send unattended emails is to use SMTP. Bypassing the MAPI security prompts is not a good idea.
Our Total Access Emailer addin program uses SMTP so you can broadcast a large volume of emails for your users. Total Access Emailer lets you send personalized emails to everyone on your list and embed fields from your records in the message and subject. It can also attach Access reports as PDF files filtered for each recipient so that each person only sees their data. No programming is required with its Wizard interface. A VBA library is available if you want to automate it. More info here: http://www.fmsinc.com/MicrosoftAccess/Email.asp
A free trial is available: http://www.fmsinc.com/MicrosoftAccess/Email/free-trial.html

Pass an email address to the SendObject method in VBA

I would like to take the email address from my table 'GetContact_TempTbl" and send the report to that email address. This email will change according the company that recieves. I currently pull the related email address and store it in the temporary table. I currently get the Object Required error.
Many thanks in advace for the advice.
Dim db As Database
Dim rs As Recordset
Dim stRecipients As String
Dim stDocName As String
Set db = CurrentDb()
Set rs = db.OpenRecordset("GetContact_TempTbl")
Set stRecipients = rs.Fields("Contact_Email")
stDocName = "License CODs"
stRecipietns = stRecipients
DoCmd.SendObject acReport, stDocName, acFormatPDF, stRecipients, , , "Thank You for your purchase"
If your recordset holds one row for each recipient you want to email, walk the recordset to gather them instead of reading only the recipient from the first row.
Const stDocName As String = "License CODs"
Dim db As DAO.database
Dim rs As DAO.Recordset
Dim stRecipients As String
Set db = CurrentDb()
Set rs = db.OpenRecordset("GetContact_TempTbl")
With rs
Do While Not .EOF
stRecipients = stRecipients & ";" & !Contact_Email
.MoveNext
Loop
.Close
End With
If Len(stRecipients) > 0 Then
' discard leading ";"
stRecipients = Mid(stRecipients, 2)
DoCmd.SendObject acReport, stDocName, acFormatPDF, _
stRecipients, , , "Thank You for your purchase"
Else
MsgBox "No recipients to email!"
End If
Set rs = Nothing
Set db = Nothing
However, if my interpretation was incorrect, and the recordset always contains a single row with just one Contact_Email, you don't even need a recordset. You can simply retrieve the Contact_Email with DLookup.
stRecipients = Nz(DLookup("Contact_Email", "GetContact_TempTbl"), "")
You should only use set with an object, not a string:
Dim db As Database
Dim rs As Recordset
Dim stRecipients As String
Dim stDocName As String
Set db = CurrentDb()
Set rs = db.OpenRecordset("GetContact_TempTbl")
''This is not a field object, it is a string
stRecipients = rs.Fields("Contact_Email")
stDocName = "License CODs"
stRecipietns = stRecipients
DoCmd.SendObject acReport, stDocName, acFormatPDF, _
stRecipients, , , "Thank You for your purchase"
It may be possible to make this easier if you say how you create the temporary table.

Run SQL query on each item in multiselect listbox and show results in another listbox?

I'm running a "select distinct" query on fields selected in a multiselect listbox (the listbox is populated with fields from a table). The code worked fine before I made it multiselect, and now the SQL query is not functioning. For example when I select the fields "Gender" and "INTERFACE" from the field list, the results in the 2nd list box are:
SELECT DISTINCT
Gender
INTERFACE
Ideally my results would include the field name and then the values, with a line between each field results.
Here is my code:
Dim strSQL As String
Dim strCriteria As String
Dim varItem As Variant
On Error GoTo Err_Command206_Click
For Each varItem In Me.ScrubbedList.ItemsSelected
strCriteria = strCriteria & ",[" & Me!ScrubbedList.ItemData(varItem) & "]"
Next varItem
strSQL = "SELECT DISTINCT " & Mid(strCriteria, 2) & " FROM Scrubbed"
'====== Testing
Debug.Print strSQL
'=====
Me.List316.RowSource = strSQL
Exit_Command206_Click:
Exit Sub
Err_Command206_Click:
MsgBox "Please select a field"
You seem to have some things in the wrong place:
Dim strSQL As String
Dim strCriteria As String
Dim varItem As Variant
On Error GoTo Err_Command206_Click
For Each varItem In Me.ScrubbedList.ItemsSelected
strCriteria = strCriteria & ",[" & Me!ScrubbedList.ItemData(varItem) & "]"
Next varItem
strSQL = "SELECT DISTINCT " & Mid(strCriteria,2) & " FROM Scrubbed"
'====== Testing
Debug.Print strSQL
'======
Me.List316.RowSource = strSQL
Exit_Command206_Click:
Exit Sub
Err_Command206_Click:
MsgBox "Please select a field"

Loop through recordset to populate listbox with results of SQL query?

I have a multi-select listbox which I want to be the source of a sql query with results displayed in a listbox. Currently I have the results going to a recordset which is displayed beautifully when only one 1 field is selected. For instance, if the user selects Gender, no recordset datasheet opens and List20 shows the field name, underscored, with the distinct values below it--Perfect!! When I try to select more than one item things go awry. For instance 2 items selected (Gender, Interface) will result in Gender (underlined) but with 2 F's and 2 M's below it and none of the Interface field values. How do I loop through each selection and have them show in the listbox? Here is my code. Also, weirdly, List13 shows no results when I run the query, but when I take away the line:
Set Me.List13.Recordset = rs
a recordset datasheet opens up and the desired results no longer appear in List20. Please help!
Private Sub Command19_Click()
Dim strSQL As String
Dim strCriteria As String
Dim varItem As Variant
Dim dbs As Database
Set dbs = CurrentDb()
Dim qdf As QueryDef
Dim rs As Recordset
On Error GoTo Err_Command19_Click
For Each varItem In Me!List101.ItemsSelected
strCriteria = strCriteria & ",'" & Me!List101.ItemData(varItem) & "'"
Next varItem
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
strSQL = "SELECT DISTINCT " & strCriteria & " FROM Scrubbed"
strSQL = Replace(strSQL, "'", "")
Set rs = dbs.OpenRecordset(strSQL)
Do Until rs.EOF
Set Me.List20.Recordset = rs
Set Me.List13.Recordset = rs
Loop
With dbs
Set qdf = .CreateQueryDef("TmpDistinctValues", strSQL)
DoCmd.OpenQuery "TmpDistinctValues"
.QueryDefs.Delete "TmpDistinctValues"
End With
dbs.Close
qdf.Close
Exit_Command19_Click:
Exit Sub
Err_Command19_Click:
MsgBox "Please select a field"
End Sub
Surely you mean:
For Each varItem In Me!List101.ItemsSelected
strCriteria = strCriteria & "," & Me!List101.ItemData(varItem)
colcount = colcount + 1
colwidths = colwidths & ";" & "1134"
Next varItem
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
strSQL = "SELECT DISTINCT " & strCriteria & " FROM Scrubbed"
''strSQL = Replace(strSQL, "'", "")
''Set rs = dbs.OpenRecordset(strSQL)
''Do Until rs.EOF
Me.List20.RowSource = strSQL
Me.List20.ColumnCount = colcount
Me.List20.ColumnWidths = Mid(colwidths, 2)
''Set Me.List13.Recordset = rs
''Loop

How do I display a count of null values for field selected in combobox?

I've tried using DCOUNT and SQL and nothing is working. I've pasted both queries below. When I run the SQL nothing appears in the listbox. When I run the DLOOKUP I get the error message "Run-time error '2001": You canceled the previous operation. The combobox name is ScrubbedList. Table is named Scrubbed.
DCOUNT
Dim strScrubbedValue As String
strScrubbedValue = Me.ScrubbedList
Dim intCountNull As Integer
intCountNull = DCount("*", "Scrubbed", "IsNull" & strScrubbedValue)
Text267 = intCountNull
SQL
Dim strSQL As String
Dim strScrubbedValue As String
strScrubbedValue = Me.ScrubbedList
strSQL = "SELECT Count(*) As CountAll" & strScrubbedValue & " FROM Scrubbed"
strSQL = strSQL + "WHERE" & strScrubbedValue = ""
Me.List265.RowSource = strSQL
Try:
intCountNull = DCount("*", "Scrubbed", "SomeField Is Null")
So for the combo:
intCountNull = DCount("*", "Scrubbed", strScrubbedValue & " Is Null")
It is important to ensure that you have included relevant spaces when concatenating strings.
Dim strSQL As String
Dim strScrubbedValue As String
strScrubbedValue = Me.ScrubbedList
strSQL = "SELECT Count(*) As CountAll " & strScrubbedValue & " FROM Scrubbed"
strSQL = strSQL & " WHERE " & strScrubbedValue & " Is Null"
Me.List265.RowSource = strSQL
Note that Null and zero-length strings (ZLS) are not the same.
To get both, you can say:
strSQL = strSQL & " WHERE " & strScrubbedValue & " & '' = ''"
The string concatenator in VBA is &, not +. The plus sign must be used with care, because it can lead to unexpected nulls.