Load Recordset Values into Access Form - vba

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

Related

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 VBA touchup) Auto Populating after update based on another textbox

Hello I'm trying my best to piece together a code to have all text boxes on the form update to existing records.
All the textboxes are updating beside
Me.txtSftLvl = myrs!SafteyStockLvl
I am left with the error
Item Cannot Be Found In The Collection (3265)
I have double checked my naming on the table and textbox itself and they are fine...
Hoping someone would know what im doing wrong or a simpler way of accomplishing my goals.
Private Sub txtRcStock_AfterUpdate()
Dim qrystr1 As String
Dim mydb As Database
Dim myrs As DAO.Recordset
DoCmd.OpenQuery "qryPartDesc"
If Me.txtRcStock <> "" Then
Set mydb = CurrentDb
qrystr1 = "SELECT Desc FROM qryPartDesc WHERE [PartNumber] = '" & Me.txtRcStock & "'"
Set myrs = mydb.OpenRecordset(qrystr1)
If myrs.EOF = False Then
Me.txtRcCat = myrs!Desc
End If
qrystr1 = "SELECT Price FROM tblRecLog WHERE [PartNumber] = '" & Me.txtRcStock & "'"
Set myrs = mydb.OpenRecordset(qrystr1)
If myrs.EOF = False Then
Me.txtPrice = myrs!Price
If myrs.EOF = False Then
Me.txtSftLvl = myrs!SafteyStockLvl
End If
End Sub
Anything helps.

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

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?

SetFocus is getting ignored - Why?

I have 2 fields - txtTR1_Unit and cmbTR2_Unit. Together, these 2 fields represent the total UNIT.
cmbTR2_Unit has a list of unique values that when selected - txtTR1_Unit automatically gets the related value.
I've created a function called Tier1from2 - that accepts a 'string' and returns the related Tier1 value.
So when I update cmbTR2_Unit in my After_Update event, I'd like to automatically tab to the next field. - Another combo box. I figured that I shouldn't need to set any focus, because it would automatically go to the next field after updating.
txtTR1 gets updated just as expected from my Function, but then it just sits there and won't go to the next field. So I have attempted to 'SetFocus' to the next field after the update.
Still no go. What did I miss??
Private Sub cmbTR2_UNIT_AfterUpdate()
If Len(Me.cmbTR2_UNIT.Value) <> 0 Then
Me.txtTR1_UNIT.Value = Tier1From2(Me.cmbTR2_UNIT.Text)
'cmb_CostCenter.setfocus - 'this doesn't seem necessary - but it doesn't work anyway.
End If
End Sub
As a test I tried removing the function "Tier1From2(Me.cmbTR2_UNIT.text)" simply hard coding the word 'RESULT' in txtTR1_UNIT and it works without a hitch. I know I used to write a more simple function but I haven't touched VBA in awhile - How can I simplify this function:
Private Function Tier1From2(strTier2 As String) As String
Dim qdf As DAO.QueryDef
Dim db As DAO.Database
Dim strQry As String
Dim rs As Recordset
Set db = CurrentDb
Set qdf = db.QueryDefs("qUNIT_HUB")
strQry = "SELECT Tier1_Unit, Tier2_Unit " & _
" FROM LTBL_Cost_Collector " & _
" GROUP BY Tier1_Unit, Tier2_Unit " & _
" HAVING (((Tier2_Unit) = '" & strTier2 & "'));"
qdf.SQL = strQry
db.QueryDefs.Refresh
Set rs = db.OpenRecordset(strQry)
Tier1From2 = rs![Tier1_Unit]
Set db = Nothing
Set qdf = Nothing
Set Recordset = Nothing
End Function
It turns out that something in this function was causing the field and form to loose focus. db.QueryDefs.refresh perhaps? The solution was to update my Function as follows
Private Function Tier1From2(strTier2 As String) As String
Dim rs As DAO.Recordset
Dim db As DAO.Database
Dim strSQL As String
Dim strTier1 As String
Set db = CurrentDb
strSQL = "SELECT Tier1_Unit, Tier2_Unit " & _
" FROM LTBL_Cost_Collector " & _
" GROUP BY Tier1_Unit, Tier2_Unit " & _
" HAVING (((Tier2_Unit) = '" & strTier2 & "'));"
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
strTier1 = rs!Tier1_Unit
Set rs = Nothing
Set db = Nothing
Tier1From2 = strTier1
End Function
This worked without a hitch.

Data on disk is not directly updated with the 'Update' functionality on my recordset

I am working on some VBA macros. I also use ADODB.Recordset to access data in my Access database.
Now, I need to loop through records in my table and in this loop, also need to update the same table in another function.
Here is the code (cleaned for easy reading).
Dim strSql As String
Dim rstCycles As adodb.Recordset
strSql = "SELECT * FROM tblCycles WHERE DatePlanning = #" & dteCurrentDate & "#"
Set rstCycles = SelectQuery(strSql)
While Not rstCycles.EOF
if ... then
rstCycles("NoCycle1") = ...
rstCycles.Update
end if
RefreshPlanning (*)
rstCycles.MoveNext
Wend
(*) In this function I perform a select on the tblCycles table.
The problem is after the rstCycles.Update, the data is not immediately record on disk thus the call to RefreshPlanning does not read updated data. If I set a '1 second pause' after the update everything is ok. I don't want to use a kind of pause in my loop. Is there another solution?
UPDATE ---------------
RefreshPlanning is a simple function to refresh my excel sheet based on my tblCycles table.
Sub RefreshPlanning(DatePlanning As Date, CodeEquipement As String)
Dim strSql As String
Dim rstCycles As adodb.Recordset
strSql = "SELECT * FROM tblCycles WHERE DatePlanning = #" & DatePlanning & "# AND CodeEquipement = '" & CodeEquipement & "'"
Set rstCycles = SelectQuery(strSql)
While Not rstCycles.EOF
' Some code here to update my excel sheet
' ...
rstCycles.MoveNext
Wend
End Sub
DAO is many times faster than ADO with MS Access:
Dim strSql As String
Dim rstCycles As DAO.Recordset
Dim db As Database
Set db = OpenDatabase("z:\docs\test.accdb")
strSql = "SELECT * FROM tblCycles WHERE DatePlanning = #" & dteCurrentDate & "#"
Set rstCycles = db.OpenRecordset(strSql)
While Not rstCycles.EOF
if ... then
rstCycles.Edit
rstCycles("NoCycle1") = ...
rstCycles.Update
end if
''Need more information here
RefreshPlanning (*)
rstCycles.MoveNext
Wend
If dteCurrentDate is the same as today, you can say:
"SELECT * FROM tblCycles WHERE DatePlanning = Date()"