Access 2013: Email GROUPED sections of report to GROUPED BY recipients - sql

I need to email sections of a report out of MS Access 2013 based on whom the report is GROUPED BY.
Best Case: On Click of a single button, each "UW" would receive only the rows that are assigned to them.
Second Best Case: Each grouped section has a button. On Click of each button, the "UW" that the section is grouped by would receive only the rows that are assigned to them.
This VBA w/ SQL script from Access 2013 VBA: Query recipient email address allows me to create an Outlook email addressed to a UW, but is not necessarily the UW which the report is grouped by.
Dim varSubject As Variant
Dim varBody As Variant
Dim email as string
Dim sqlSTR as string
Dim r As DAO.Recordset
Dim varInitials as string
Dim dbs as DAO.database
set dbs = currentDB()
varInitials = "P.UW"
sqlSTR = "SELECT E.Email FROM Employee E INNER JOIN ProposalTracking P ON
E.Initials = P.UW WHERE " & someField & " = & " & """" & someVariable & """"
Set r = dbs.OpenRecordset(Name:=sqlSTR , Type:=dbOpenSnapshot)
varName = r![Email]
r.close
varSubject = "Today's Proposals"
varBody = "Please see the proposals which are assigned to you."
DoCmd.SendObject , , , varName, , , varSubject, varBody, True, False
How can I email multiple UWs by whom the report is grouped in as few clicks as possible?

Related

Load Recordset Values into Access Form

rookie here - I have a form ("6_SubmissionGrid") containing a listbox ("lst_SelectSubm") from which I am creating a recordset ("rs6"). I wish to launch a second form ("6a_ContractQuote") and populate textboxes from the recordset, but am having trouble getting the values to display. I've not set any parameters or events on the second form (except to clear rs6 upon close) and have the following code in the form containing the listbox. The recordset is intended to only hold one unique record.
Dim Db As Database
Dim rs6 As Recordset
Dim SlctSubm As String
Dim strSQL As String
Dim PrincNo As String
Dim PrincName As String
Dim txt_PrincNo As String
Dim txt_PrincName As String
Public Sub btn_LaunchContrQuote_Click()
Set Db = CurrentDb
Set rs6 = Db.OpenRecordset("SELECT [5_SUBMISSION].[5_SubmNo], [5_SUBMISSION].[5_SubmStatus], [5_SUBMISSION].[5_DateRecd], [5_SUBMISSION].[3_PrincNo], [3_PRINCIPAL].[3_PrincName], [5_SUBMISSION].[2_AgcyNo], [2_AGENCY].[2_AgcyName], [5_SUBMISSION].[1_ProducerNo], [1_PRODUCER].[1_Last Name], [1_PRODUCER].[1_First Name], [5_SUBMISSION].[4_ObligeeNo], [4_OBLIGEE].[4_ObligName], [5_SUBMISSION].[5_ProjectDescription], [5_SUBMISSION].[5_ProjectCity], [5_SUBMISSION].[5_ProjectState], [5_SUBMISSION].[5_Underwriter], [5_SUBMISSION].[4_AddtlObligee1], [5_SUBMISSION].[4_AddtlObligee2], [5_SUBMISSION].[4_AddtlObligee3], [5_SUBMISSION].[4_AddtlObligee4] " & _
"FROM (((5_SUBMISSION INNER JOIN 2_AGENCY ON [5_SUBMISSION].[2_AgcyNo] = [2_AGENCY].[2_AgcyNo]) INNER JOIN 3_PRINCIPAL ON [5_SUBMISSION].[3_PrincNo] = [3_PRINCIPAL].[3_PrincNo]) INNER JOIN 4_OBLIGEE ON [5_SUBMISSION].[4_ObligeeNo] = [4_OBLIGEE].[4_ObligeeNo]) INNER JOIN 1_PRODUCER ON ([5_SUBMISSION].[1_ProducerNo] = [1_PRODUCER].[1_ProducerNo]) AND ([2_AGENCY].[2_AgcyNo] = [1_PRODUCER].[2_AgcyNo]) " & _
"GROUP BY [5_SUBMISSION].[5_SubmNo], [5_SUBMISSION].[5_SubmStatus], [5_SUBMISSION].[5_DateRecd], [5_SUBMISSION].[3_PrincNo], [3_PRINCIPAL].[3_PrincName], [5_SUBMISSION].[2_AgcyNo], [2_AGENCY].[2_AgcyName], [5_SUBMISSION].[1_ProducerNo], [1_PRODUCER].[1_Last Name], [1_PRODUCER].[1_First Name], [5_SUBMISSION].[4_ObligeeNo], [4_OBLIGEE].[4_ObligName], [5_SUBMISSION].[5_ProjectDescription], [5_SUBMISSION].[5_ProjectCity], [5_SUBMISSION].[5_ProjectState], [5_SUBMISSION].[5_Underwriter], [5_SUBMISSION].[4_AddtlObligee1], [5_SUBMISSION].[4_AddtlObligee2], [5_SUBMISSION].[4_AddtlObligee3], [5_SUBMISSION].[4_AddtlObligee4] " & _
"HAVING ((([5_SUBMISSION].[5_SubmNo])= '" & Me.lst_SelectSubm & "'));")
rs6.MoveLast
PrincNo = rs6.Fields(3).Value
PrincName = rs6.Fields(4).Value
DoCmd.OpenForm "6a_ContractQuote", acNormal, , [txt_PrincNo] = PrincNo And [txt_PrincName] = PrincName
End Sub
Appreciate any recommendations. I've tried various configurations on the Where Condition of the OpenForm command, but no luck. Thank you!
You could address the form after having opened it:
Dim ContractQoute As Form
'...
PrincNo = rs6.Fields(3).Value
PrincName = rs6.Fields(4).Value
DoCmd.OpenForm "6a_ContractQuote", acNormal
Set ContractQoute = Forms("6a_ContractQuote")
ContractQoute![txt_PrincNo].Value = PrincNo
ContractQoute![txt_PrincName].Value = PrincName
Set ContractQoute = Nothing

Open a report and attach the DAO Recordset to it

I have a form that is for entering inventory transactions. I added a button to it that when the operator press it, a report opens and show the assigned material to a projects. I get this error when I press the button and the report doesn't open.
enter image description here
I added a code to open event of my report that make a dao recordset and I want to attach the recordset to my report. The code for opening the report is as follow:
Private Sub Report_Open(Cancel As Integer)
'Create the necessary recordset and connections
Dim dbs As DAO.Database
Dim rsInventoryControl As DAO.Recordset
Dim rsInventoryAssigned As DAO.Recordset
Dim rsFiltered As DAO.Recordset
Dim strSQLInventory As String
Dim strSQLAssigned As String
Dim strAssignableAmount As String
Dim lngStockID As Long
Dim lngInventoryID As Long
Dim strInventoryNumber As String
'get the data from form
lngStockID = Forms("frmInventoryPermission")!StockID
lngInventoryID = Forms("frmInventoryPermission")!frmInventoryPermissionDetailSubform.Form!cboInventoryID
'set the connection and recordsets
Set dbs = CurrentDb
'Create and run rsInventoryAssigned recordset
strSQLAssigned = "SELECT tblInventoryPermission.Assigned,Sum(tblInventoryPermissionDetail.AssignedQty) AS SumOfAssignedQty, tblInventory.InventoryID, tblStocks.StockID " & _
"FROM (tblInventoryPermission INNER JOIN tblStocks ON tblInventoryPermission.StockID = tblStocks.StockID) INNER JOIN (tblInventoryPermissionDetail INNER JOIN tblInventory ON tblInventoryPermissionDetail.InventoryID = tblInventory.InventoryID) ON tblInventoryPermission.TransferPermissionID = tblInventoryPermissionDetail.InventoryPermissionID " & _
"GROUP BY tblInventoryPermission.Assigned, tblInventory.InventoryID, tblStocks.StockID " & _
"HAVING (((tblInventoryPermission.Assigned)=False));"
Set rsInventoryAssigned = dbs.OpenRecordset(strSQLAssigned)
'find the record based on the information in form
'Control that Stock and Inventory Id is specified
rsInventoryAssigned.Filter = "[InventoryID]='" & lngInventoryID & "' AND [StockID]= '" & lngStockID & "'"
'set the recordsource of the report to filtered recordsource
rsFiltered = rsInventoryAssigned.OpenRecordset
Me.RecordSource = rsFiltered
End Sub
Avoid any VBA code with DAO recordsets and simply save your parameterized query using form controls and then assign it permanently in recordsource of report. Being parameterized, query will adjust each time the report is opened.
Below SQL uses the PARAMETERS clause (allowable in Access' SQL dialect) and uses parameters in WHERE clause. Additionally query uses table aliases to avoid repeatedly writing out the long table names.
SQL (save as Access query object and then manually assign query to RecordSource of report)
PARAMETERS Forms!frmInventoryPermission!frmInventoryPermissionDetailSubform.Form!cboInventoryID TEXT(255),
Forms!frmInventoryPermission!StockID TEXT(255);
SELECT p.Assigned,
i.InventoryID,
s.StockID,
SUM(pd.AssignedQty) AS SumOfAssignedQty
FROM (tblInventoryPermission p
INNER JOIN tblStocks s
ON p.StockID = s.StockID)
INNER JOIN (tblInventoryPermissionDetail pd
INNER JOIN tblInventory i
ON pd.InventoryID = i.InventoryID)
ON p.TransferPermissionID = pd.InventoryPermissionID
WHERE p.Assigned = False
AND i.[InventoryID] = Forms!frmInventoryPermission!frmInventoryPermissionDetailSubform.Form!cboInventoryID
AND s.[StockID]= Forms!frmInventoryPermission!StockID
GROUP BY p.Assigned,
i.InventoryID,
s.StockID;
VBA (place behind form button and delete entire Report_Open event)
DoCmd.OpenReport "myreport", acViewPreview

Access - Display Count of Records in Textbox

I have an Access Application. It opens on a search form and the user selects the criteria, hits "Search" and it displays list of records that meets their criteria. The user then clicks on a record, and it displays the information about that record. All of this works great, but I am trying to add a "Count" feature, that will display a textbox showing the number of times that user is in the database.
But my textbox only ever displays "1".
In my textbox: I have the following:
=Count([Requester_UserName])
Because a Requester can have many entries in the database, I want to show a count of how many entries they have on this form, although the form only displays information about the one specific record, so this specific user might have 15 entries. How do I get this form to show "15" in that textbox?
Any help is appreciated.
Don't count (no pun intended) on the .Count method. It's unreliable for various scenarios. Use a SQL statement instead. Here's a multipurpose method:
'------------------------------------------------------------------------------
'Purpose : Retrieve number of records
'
'Prereq. : ADO 2.x
'Parameter: cn - Initialized ADODB.Connection object
' sTable - Table name
' sField - DB column name used for COUNT. This is ideally a integer column
' sFilter - SQL WHERE clause
'Returns : # of records
'Note : -
'------------------------------------------------------------------------------
Public Function SQLCount(ByVal cn As ADODB.Connection, ByVal sTable As String, Optional ByVal sField As String = "*", _
Optional ByVal sFilter As String = vbNullString) As Long
Dim rs As ADODB.RecordSet
Dim lRecCount As Long
Dim sSQL As String
sSQL = "SELECT COUNT([" & sField & _
"]) AS RecCount FROM [" & sTable & "]"
If Len(sFilter) Then
sSQL = sSQL & " WHERE " & sFilter
End If
sSQL = sSQL & ";"
Set rs = New ADODB.RecordSet
rs.Open sSQL, cn
If Not (rs.BOF And rs.EOF) Then
lRecCount = rs.Fields("RecCount").Value
End If
rs.Close
Set rs = Nothing
SQLCount = lRecCount
End Function
'==============================================================================
Assuming the ADODB table name is MyTable, ID is a integer column in that table and you want to know how many records with 'Miami' are stored in the field named City, you call it like
Debug.Print SQLCount(cn, "MyTable", "ID", "City = 'Miami")

VBA Select Query into String for use with automatic population of web page form

I am trying to:
Run a select query on an Access DB using VBA (this part is simple), populate a string with the results of the query (one field, but n results) with each result separated by a carriage return.
it's going to be a list of order numbers e.g:
1234567
2345678
3456789
9876543
etc
I then open a web site, wait for it to load, then 'paste' in the string results. Then press a search button (I actually have this part working).
What I can't figure out is how to get the results of the SQL query into a string.
Right now I have them in a Variant and I can't use that with my web site paste and it won't convert into a string using Cstr.
Dim IE As Object
Dim OrderNos As DAO.Recordset
Dim dbs As DAO.Database
Dim strSQL As String
Dim OrderArray As Variant
Dim OrderString As String
'set up the SQL query in easy to swallow lumps.
strSQL = "SELECT [Short Term Storage].[Order no]"
strSQL = strSQL & "FROM [Short Term Storage]"
strSQL = strSQL & "WHERE (([Short Term Storage].[Sales Rep Name] Is Null And [Short Term Storage].[Status]<>'SHIPPED'));"
'set the variable to the results from SQL query
Set dbs = CurrentDb
Set OrderNos = dbs.OpenRecordset(strSQL)
OrderArray = OrderNos.GetRows()
I think I either need to use a For Each loop or a Do Until EOF loop.
How can I get each row of the array into a string, then add a vbcrlf, then the next row of the array until EOF?
I think you need something like:
Dim rs As DAO.Recordset
Set rs = dbs.OpenRecordset(strSQL)
Do Until rs.EOF = True
OrderString = OrderString & rs!FieldName & vbCrLf
rs.MoveNext
Loop

MS Access, ComboBox value into SQL statement

The function of my form is to allow a user to assign another user a task, this works fine. The user selects who they want to assign a task to via combo box which is linked to an access DB, I am now trying to implement a feature were by I can get the value of that selected user from the combobox (by matching the ID in the combox to the ID in the DB and then finding the email from that row, so I can then insert it into an outlook email message that pops up on when the user clicks "Assign" (the outlook code works fine it is just not pulling a value from the combo box, so on click it brings outlook new email up, but the "To" field is empty, I have also tried to print the variable I am assigning to that field to ensure it isnt an issue with the outlook code and it stills returns no value).
Here is the section of code that wont work....
Dim OutApp As Object
Dim OutMail As Object
Dim EmailSubject As String
Dim EmailSendTo As String
Dim MailBody As String
Dim db As DAO.Database, rs As DAO.Recordset
Dim s As String
Dim sqlStatement As String
Combo2.SetFocus
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT tblUsers.Email FROM tblUsers WHERE tblUsers.UserID = 'Combo2.Text ")
Combo2 is the name of the combobox
May be so?
Set rs = db.OpenRecordset("SELECT tblUsers.Email FROM tblUsers WHERE tblUsers.UserID = " & Combo2.SelectedItem.ToString())
Try this one:
If your UserID is Text try this one:
Set rs = db.OpenRecordset("SELECT tblUsers.Email FROM tblUsers WHERE tblUsers.UserID = '" & Combo2.Text & "'")
However, if your UserID is numeric try this one:
Set rs = db.OpenRecordset("SELECT tblUsers.Email FROM tblUsers WHERE tblUsers.UserID = " & Combo2.Text)
I don't completely understand how Access comes into play, so if this is wrong I apologize. This is how I would do it in Access:
Dim OutApp As Object
Dim OutMail As Object
Dim EmailSubject As String
Dim EmailSendTo As String
Dim MailBody As String
Dim db As DAO.Database, rs As DAO.Recordset
Dim EmailTo as String
Dim s As String
Dim sqlStatement As String
Combo2.SetFocus
Set db = CurrentDb
EmailTo = DLookup ("Email", "tblUsers", "tblUsers.UserID = '" & Combo2.Text & "'")
If your UserID is an INT, you would change that last line to:
EmailTo = DLookup ("Email", "tblUsers", "tblUsers.UserID = " & Combo2.Text & "")
By the way, you said Edper's solution "didn't pick up anything". You know that "rs" isn't going to have an actual value in that scenario, right? rs("Email") would, however, so if you added:
EmailSendTo = rs("Email")
after his SQL string, that MIGHT give you what you needed.