Passing keys from form to sub-form in Access - ms-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
'

Related

Access VBA: combine GetRows with bookmark

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.

Populate fields in main form from record in subform

In Access 2016, I have a main form, ClientInfoForm, which contains a subform, EditTransactions_subform. The subform is in datasheet view. When the user clicks a record in the subform, I want to populate fields in the main form. In the subform's Click event, I currently have:
Public Sub Form_Click
Forms![ClientInfoForm]![Amendment] = Forms![ClientInfoForm]![EditTransactions_subform].Form![Amendment]
End Sub
I am not sure if this is the right code to execute what I want. The name of the field is [Amendment] in both the subform and the main form.
Thanks in advance for your help!
Howard
This sounds back-to-front. The normal use of main form/sub-form is to navigate through your records on the main form and for each main record the sub-form will show all related child records. You sound as though you are always showing all your records from the child table in the sub-form and as you navigate through these you want the information in the main form to change.
If this is really what you want to do, and assuming these are bound forms, then you will need to make sure that there is no link between the Main and Sub-Form (on the sub-form properties clear the entries from Link Child Fields and Link Master Fields). The in the On Current event of the sub-form you could try something like this (although it's completely untested):
Dim rst As Recordset
Set rst = Me.Parent.RecordsetClone
rst.FindFirst "Amendment = '" & [Amendment] & "'" 'assumes [Amendment] is String, otherwise lose the surrounding quotes
Me.Parent.Bookmark = rst.Bookmark
rst.Close
Set rst = Nothing

Access VBA browseto pass parameters

I have a tabbed navigation. I have a bound form a user double clicks on a single user out of a long list. I want to go to an unbound form, and pre-load the form with details about that user (edit the user). I'm specifically using an unbound form because I want to show a save/cancel button. I can't find any way to pass the id of the user into the load_form call. Am I missing something? Must I use some sort of global (this seems bad to me)?
You can use OpenArgs when opening the form:-
DoCmd.OpenForm "MyForm", acNormal,,,,,Args
And whatever Args is can be referenced in "MyForm" as Me.OpenArgs
e.g. if you pass a numeric PK then you could use something like:
dim db as dao.database
set db = currentdb
dim rs as dao.recordset
set rs = db.openrecordset ("select * from MyTable where PK=" & me.openargs", dbopendynaset, dbfailonerror)
if rs.eof then
'didn't find record with PK...
else
'then populate the unbound controls on your form with the fields from the recordset
...
Note that you don't need to have an unbound form just to offer a save/cancel. if you write an event handler for the form's Before Update event, you can cancel changes or commit them as you please.

Go To Record + Specific Tab

I have created a search criteria form that allows the user to search for specific branches that our company deals with. Now - I have created a button that displays with each entry found by the search.
I want this button to open to the specific 'ID' that goes with each record, but here is my problem: this search criteria form (Tab1) updates the records information (Tab2,Tab3,Tab4) But doesnt jump to the tab after being pressed - Simply because I'm not sure how to code it so that it automatically jumps to Tab2 (the first tab of data).
Here is my code below:
Module1
Public Function viewDetails(frmID As Integer)
Dim rs As Object
DoCmd.OpenForm "Form1"
'Directs to the selected record
Set rs = Forms!Form1.RecordsetClone
rs.FindFirst "ID = " & frmID
Forms!Form1.Bookmark = rs.Bookmark
Set rs = Nothing
and
Private Sub Command46_Click()
viewDetails (Me.ID)
End Sub
Thanks :)
The .Value property of the TabControl is a read/write property that you can use to get the currently selected page or set the currently displayed page. It is the default property of the tab control so you do not need to explicitly refer to it. Note that the page index is a zero-based array so the second tab is 1 in the index:
Forms!Form1.TabCtl0 = 1 'Jump to the second tab; form must already be open

How To Refer To Continuous Subform Contol

I have an Access 2003 form with one subform, set to continuous forms, in a subform control. For one record in the main form, 1 to many records will appear in the sub form. The data displays properly.
The main form is named Widgets and the sub form is named Transactions. There are 5 textbox controls that display the data in the subform. The one in question is ReceiptDate.
What I would like to do is look at the values and determine if there was a receipt for the year 2009, and if so then change the background of that row to yellow so it stands out when the user encounters that condition. Maybe even change the date field's font to boldface..
I tried many ways of referencing the subform's controls. When I have tried Me.Transactions.ReceiptDate I have only received the first record in that subform. I'd like to be able to loop through them and see if the condition is met. I tried Me.Transactions.ReceiptDate(1) and Me.Transactions.ReceiptDate(0) and so forth.
I tried the For Each ctl In Form.Controls route as well. It worked for a few iterations and then I received a run-time error 2455 "You entered an expression that has an invalid reference to the property Form/Report".
I had the subform in "datasheet" mode but thought that was causing me not to be able to read through an array of subform controls. So I changed it to "continuous" mode. I get the same errors for either.
Is there a way to reference specific "rows" in the subform and do something based on a value found? Also, I am performing this in the On Current event as I dont know where else to put the code. The subform loads before the parent form so its possible that these controls arent even fully "there" but then I do get the first row's date when I try it in the Immediate Window.
UPDATE 12.23.2010:
With the gracious help of #Remou I am able to debug.print the ReceiptDate fields from the RecordSet. This is great because now I can evaluate the data and do certain things based on the values.. The #Remou's code helped me put this into the OnCurrent event:
Dim i As Long
Dim frm As Form
Dim rs As DAO.Recordset
' Get the form and its recordset.
Set frm = Me.Form
Set rs = frm.RecordsetClone
' Move to the first record in the recordset.
rs.MoveFirst
' Move to the first selected record.
rs.Move frm.SelTop - 1
' Enumerate the list of selected records presenting the ReceiptDate field
For i = 1 To rs.RecordCount
Debug.Print rs![ReceiptDate]
rs.MoveNext
Next i
So now that I am able to know which row in my subform has a receipt from 2009, I need to be able to highlight the entire row or rows as I come across them in that for loop. How can I reference the actual row? Datasheet view or Continuous Forms view - I have tried both.
Conditional Formatting is great but it only allows me to highlight one particular record and I'd much rather be able to do this via VBA because...... from here I will want to give the use the ability to click on any record in the subform and get the receipt details and potentially print them.
Any ideas?
In this situation, it is best to use conditional formatting.
To refer to a control on a subform, refer to the subform control by name, then the form property to get the form contained, then the control by name:
Me.MySubformControlName.Form.MyControl
See: http://www.mvps.org/access/forms/frm0031.htm
I have finally got it. The frm.SelTop = x will set the selected record and from there I can set the background or font style, etc.. Very cool. A simple test for 2009 and setting the selected record:
Dim i As Long
Dim frm As Form
Dim rs As DAO.Recordset
' Get the form and its recordset.
Set frm = Me.Form
Set rs = frm.RecordsetClone
' Move to the first record in the recordset.
rs.MoveFirst
' Move to the first selected record.
rs.Move 0
' Enumerate the list of selected records presenting
' the CompanyName field in a message box.
For i = 1 To rs.RecordCount
If Year(rs![ReceiptDate]) = 2009 Then
frm.SelTop = i '<-----------------------------
End If
rs.MoveNext
Next i
In order for me to get the email off the bottom of my continuous form, I used this much simpler code (as I avoided the RecordsetClone code)
Me.[email subform].Form.SelTop = Me.[email subform].Form.Count 'selects the last row
str = Me.[email subform].Form.Email 'capture the value of the last row
MsgBox str