Access VBA: combine GetRows with bookmark - ms-access

I have a small dataset with a maintable and one table being dependent on the maintable. One record in the main table can have multiple related records in the subtable.
I also created a form (maintable) with a subform (subtable).
Afterwards, I created two recordsetclones based on the form and the subform respectively.
Adding the bookmark method also allows me to use data from the record that is actually active in the form. The problem is: the bookmark only applies to the mainform.
I used GetRows to create an array variable to be filled with the records from the subform. The array is filled, but with all records from the subtable. I only want those records which are currently shown on the subform.
I'm using a DAO recordset.
How can I fix this?
My code so far:
Sub FillMail()
Dim rstMail As DAO.Recordset
Dim rstIsin As DAO.Recordset
Dim db As Database
Dim isinarray As Variant
Set rstMail = Forms!Transfert.RecordsetClone
Set rstIsin = Forms!TFT_ISIN_DatasheetSub2.RecordsetClone
Set db = CurrentDb
rstMail.Bookmark = Forms!Transfert.Bookmark
rstIsin.Bookmark = Forms!TFT_ISIN_DatasheetSub2.Bookmark
isinarray = rstIsin.GetRows
MsgBox isinarray(2, 0)
End Sub

If the subform TFT_ISIN_DatasheetSub2 is in a subform control under the main form Transfert, link the Master and Child fields in the sub form control, and change:
Set rstIsin = Forms!TFT_ISIN_DatasheetSub2.RecordsetClone
to:
Set rstIsin = Forms!Transfert!subTFT_ISIN.RecordsetClone
This assumes your subform control is named subTFT_ISIN.

Related

Passing keys from form to sub-form in Access

I'm using a sub-form to assign multiple tasks at the same time. This sub-form is placed on a form that has common details applicable to all the tasks there. For instance: Customer, Product Code, Part Code etc., Both the form and the sub-form feed the data to the same table. These forms are linked with one of the keys - Line Item, which is present on both the form and the sub-form. The other two keys are Task Title - placed on the sub-form, and Stage ID placed on the main form. Line Item is configured in a way that it populates the value from another open form for both the main form and the sub-form.
But Access isn't allowing me to add any details whatsoever to the sub-form. It gives me the error "You must enter a value for 'TableName.LineItem' field.
Kindly advise.
Two Approaches:
Select Table A and hit create form on the ribbon. Then do the usual prettification like deleting id columns, setting the forms format to continuous forms, and replacing textboxes with combo- boxes where appropriate.
First Approach is put unbound controls and a button in the header. Then insert the values from the controls in the button click event
Private Sub cmbAddRecord_Click()
Dim db As Database
Set db = CurrentDb
Dim rs As DAO.Recordset
Set rs = db.OpenRecordset("TableA")
rs.AddNew
rs!A = Me.txtA
rs!B = Me.txtB
rs!C = Me.txtC
rs.Update
Me.Requery 'show changes
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
End Sub
You can also insert values while actively entering the data for instance
Private Sub A_AfterUpdate() 'A is the TextBox in the detail section that is bound to A in TableA and so forth
Me.B = B.Parent!txtB
Me.C = C.Parent!txtC 'Tip use Me.MyControlName.SetFocus to select a control
Me.Refresh 'show changes
'tip you can also put this code in hotkeys https://learn.microsoft.com/en-us/office/vba/api/access.form.keydown
End Sub
'

Selection of Records in a Subform when opening a different form via the Mainform

I have a subform as a Datasheet inside my Mainform; on my Mainform i have a Button that opens a different Form(frmtwo) as a Dialog; i would like to alter the Records currently selected in the subform via the frmtwo.
What I have so far: the Button opens the frmtwo without Problem and I have referenced the Subform via
Dim F As Form
Dim RS As Recordset
Dim length As Long
Set F = Forms![frmMain]![Subform].Form
Set RS = F.Recordset
but when I try to determine the currently selected Record of the subform via
Rs.MoveFirst
Rs.Move F.Seltop - 1
length = F.Selheight
the debugger says that length is 0.
It seems that when I click the Button to open frmtwo it deselects the Records in my Subform. Is that correct? and if so, is there a Way to "remember" the selected Records?

View Saved query data in Sub form - Access VBA

I am trying to view the data in sub-form. Below is the code which I am trying.
Dim qdfretriveVal As DAO.QueryDef
Dim rs As Recordset
Dim strQry As String
Set qdfretriveVal = CurrentDb.QueryDef("export_excel")
Set rs = qdfretriveVal(14)
rs.OpenRecordset
DoCmd.OpenQuery "export_excel", acViewNormal, acReadOnly
In Access I have saved "export_excel" query as per following.
PARAMETERS val Long;
SELECT Raw_Data_New.A, Raw_Data_New.B, Raw_Data_New.Val
FROM Raw_Data_New
WHERE (((Raw_Data_New.Val)=[val]));
I want to pass the parameter val and view the data in Subform. can any one assist me, how to achieve this output?
Probably the best way to do this is
Create a form with the query you want to display as the Record Source. Do not put any filter on here. No parameters. Nothing. Let it display all the data.
Put your new form as a subform on your main form.
Add a control like a combo box or a bound control (however you want to get the data) and link it to val (what was your parameter)
in the subform's properties go to Data > Link Master Fields > your textbox/combobox/whatever from previous bullet.
then Data > Link Child Fields > Val
Now your subform will be filtered based on your parent form but with no parameters.

How to create a temporary table and set it up as the record source for a subform?

I am trying to create a temporary table using the CreateTableDef method, and set that table as the record source of a subform in my access database when the parent form loads:
Private Sub Form_Load()
Dim db As Database
Dim tblDef As TableDef
Set db = CurrentDb
Set tblDef = db.CreateTableDef("tblMyTable")
tblDef.Fields.Append tblDef.CreateField("Field1", dbText)
tblDef.Fields.Append tblDef.CreateField("Fields", dbText)
tblDef.Fields.Append tblDef.CreateField("Field3", dbText)
db.TableDefs.Append tblDef
db.TableDefs.refresh
Me.sfrm.Form.RecordSource = "SELECT * FROM tblmyTable"
Me.sfrm.Form.Requery
End Sub
However, when the execution reaches to: Me.sfrm.Form.RecordSource = "SELECT * FROM tblIHC", it raises the run time error 2467: "the expression you entered refers to an object that is closed or does not exist."
Help is appreciated. Also, do I also need to set the source object property of the subform as well? And to what, if so.
It is much easier to simple use currentDB.Execute("CREATE TABLE tblMyTable (Field1 Text, field2 Text, field3 Text);"
But for the record source have you tried adding a ; at the end of the Select?
Do you have two subforms?
Should it read:
Me!IHCResults_subform.Form.RecordSource = "SELECT * FROM tblmyTable"
Me!IHCResults_subform.Form.Requery
And, if so, the last line is not needed. The subform will requery when changing the recordsource.
Also have in mind, that when you open the form, first the subform is opened (hidden), then closed, then the parent form is opened, and this opens the subform. So the subform must initially contain a valid recordsource.

Store records in an instance of a class to make available anywhere in project throughout the life of the program

I have a form with a combo box that I populate with records from a access db (I use linked tables). What I'd like to do is store the records in an instance of a class throughout the life of the program rather than querying the table each time the user selects the combo box. Right now I'm calling a funtion from a private sub, the function returns the DAO recordset data which is what I want but the question I have is, how do I pass the data from the function recordset to a public DAO.Recordset, assuming if this was possible it would hold the recordset data throughout the life of the running program. When I run the code and it finishes Public Rs() in the Watch output window says; Recordset(0 to -1) and No Variables, I'm not sure how to correct this.
Here's my code:
Private Sub cmdGetRecordset_Click()
Dim strDescription As String
Dim strModel As String
Dim rst As DAO.Recordset
Set rst = getdevices()
Do While Not rst.EOF
strDescripton = rst!DESC
strModel = rst!MODEL
Debug.Print strDescription & " " & strModel
rst.MoveNext
Loop
'rst.Close'
'Set rst = Nothing'
End Sub
''''''
Option Compare Database
Public Rs() As DAO.Recordset
Function getdevices() As DAO.Recordset
Dim Rs As Object
Dim CurDatabase As Object
connectDatabase
Set CurDatabase = CurrentDb
Set Rs = CurDatabase.OpenRecordset("SELECT * FROM tblCDA")
Set getdevices = Rs
closeDatabase
End Function
I don't fully understand your goal to be honest. Depending what you're trying to do there are a number of ways to tackle the issue.
1) Are you just pre-populating controls when the form loads?
Either run a series of queries in the Form_Load event each time the form is loaded and pass the results to each combobox as it's source OR create a temporary table with the values when the database is opened and query the temp table.
2) Are you trying to capture a persistent state of what the user selected when moving between forms or reports?
Use a temp table or dynamic array to capture the user selections (values of the comboboxes) and query the table or iterate through the array when referencing a value somewhere else.
3)Are you trying to dynamically change combobox values based on user selections?
Sadly I think having to requery the source data is the only option here. You could get all of the data from various tables and store it in memory using arrays and then iterate through the arrays as needed to change the combobox values but that would be a substantial memory footprint depending on the data.