OpenArgs to Open a subform within a form? - ms-access

I have an Access database with a Form that, once the EmployeeID is double-clicked, then it opens another form that contains a subform with employee information. I obtain the EmployeeID from the original form with this code...
myID = CInt(Me.OpenArgs)
I use this string on my secondary form and the subform contained within, however, it is not picking up the EmployeeID. The main form has this code for the Double-Click event...
Private Sub EmployeeID_DblClick(cancel As Integer)
Dim myID As Variant
myID = Me.EmployeeID
DoCmd.OpenForm "subformEmployeeInfo",,,,,,myID
DoCmd.OpenForm "frm_EmployeeInformation",,,,,,myID
End Sub
When I step through the code I notice that my ID is there on the OpenForm Command but when it switches to the subform code I get an "Invalid Use of Null" error.

With a subform, you can refer to the Parent OpenArgs,:
ID= Me.Parent.OpenArgs
These are the OpenArgs of the form on which the subform resides.

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
'

Reference a Subform in a Subform

I have a report that has a Subreport. The Subreport references an ID that will populate the subreport. I keep getting an Enter Parameter Value error. If I put in the correct ID in the box and click ok the report populates correctly. So my assumption is that I have not referenced the ID correctly, but I can not figure out what I am doing incorrectly.
The report will work in the form until I insert it in to another form. I have a Navigation form called Main, a subform named ProductsList, and a subform inside of ProductsList called SupplierDS. The control name is ID. This is the reference I have:
[ID]=Forms![Main]![ProductsList].Form![SupplierDS].Form![ID]
Is this the correct reference to access the control ID?
This is the VBA code that I am using to reference the ID:
Private Sub Command524_Click()
Dim stDocName As String
stDocName = "SupplierDS"
DoCmd.OpenReport stDocName, acViewPreview, , "[ID]=Forms![Main]![ProductsList].Form![SupplierDS].Form![ID]"
End Sub
You can use string concatenation to make the statement work independent of where the form is:
DoCmd.OpenReport stDocName, acViewPreview, , "[ID] = " & Me!ID

Need to Automatically Edit Access TextBox in SubForm after SetFocus

I trying to use TabControls for subforms in an Access Form.
I have a subform embedded on a page of a TabControl. I meed to move from the last data entry field in the Main Form to the first data entry field in the Subform.
When I SetFocus to the Control in the subform, it does not allow editing without clicking on the TextBox. Is there a command for immediately enabling editing?
Private Sub ReadDate_AfterUpdate()
Dim myControl As Control
Set myControl = Forms!Data_Input!BOD_Data_Subform!textBoxToEdit
myControl.SetFocus
End Sub
Thanks in advance for any suggestions.
Two steps required to select a control on a subform from a main form
Do the following in order:
Select the subform control.
Select the appropriate control on the
subform.
Example code
Private Sub ReadDate_AfterUpdate()
Dim myControl As Control
' Select the subform control first
Forms!Data_Input!BOD_Data_Subform.SetFocus
Doevents
' Select the control on the subform.
Forms!Data_Input!BOD_Data_Subform.Form!textBoxToEdit.SetFocus
End Sub
* Edits *
I suspect answer would work if there were not TabControls in Main Form. It is a two-part process but in this case the first step is:
Forms!Data_Input!subDataTabControl.Pages(0).SetFocus
Where subDataTabControl is name of the Tab Control and 0 give page index.

How to display record inside form Access 2016 - VBA

Hope you're enjoying your Friday afternoon!
I've got a form on MS Access which searches a linked table for a barcode (which is entered by the user)
If the barcode exists in the table I'd like to show the user the record which contains the matched barcode, if not the code moves on elsewhere.
Here is what I have so far:
DoCmd.DeleteObject acQuery, "Query2"
Set qdef = CurrentDb.CreateQueryDef("Query2", s)
Me.sbfMatchedRec.SourceObject = "Query2"
I get an error when trying to change the SourceObject to Query2.
If I open Query2 it has the exact information I need in it. I can also go via the form properties and change the SourceObject to Query2 which then displays what I need, but I just need to know how to change this in VBA as above.
Thanks!
You could requery the subform with the barcode passed in from the form's textbox as the WHERE criteria in the subform's .RecordSource property.
For example, say this table (tblProducts) has your list of barcoded records:
You could then add a subform to your form that will show records from tblProducts. Also add a textbox to the form for the user to supply a barcode and a command button for the user to initiate a search:
Before hooking-up the search button with the VBA, take a look at the .RecordSource property for the subform...
...switch the query builder to SQL View:
Copy the sql as you can use this as the basis to filter the records in the subform using VBA.
Go back to the form in design view and go in to the search button's On Click event:
The SQL you've copied can then be used to create an SQL string that takes the value entered in the form's barcode textbox (I've called the text box on the form txtBarcode) as part of the WHERE clause. This sql string can then be applied to the subform's .RecordSource property and then the subform can be requeried to show the result set of the new SQL definition in the subform:
Private Sub cmdSearch_Click()
Dim strSql As String
strSql = "SELECT tblProducts.Barcode, tblProducts.ProductName" _
& " FROM tblProducts"
If _
Me.txtBarcode > "" _
Then
strSql = strSql & " WHERE tblProducts.Barcode=" & Me.txtBarcode
End If
Me.tblProducts_sub.Form.RecordSource = strSql
Me.tblProducts_sub.Form.Requery
End Sub
So if we put 1001 in the text box on the form, and click the search button, the subfom only shows that matching record:

How to keep Form_Current actions from triggering table validation errors?

I have a table T with a required text field item, and a text field userID that is meant to fill in the current user's login ID. I have a form mainForm with a subform TForm based on this table. To make sure that each record has the user's ID I have added the following event to TForm:
Sub form_current()
Me.userID = Environ("username")
End Sub
When I open mainForm, I get this error message immediately on opening:
You must enter a value in the 'T.item' field.
I think what is happening here is that the form_current() event makes Access think that I've attempted to complete entry of a new record while T.item is still blank, which violates a table constraint because that's a required field. Curiously, though, this only happens when I open mainForm; when I open TForm by itself, I don't get this error message on opening.
Should I be using a different event to fill in the user's login ID for each field? Or is there a better way to use form_current() for this?
First way.
Create a function in a module:
Public Function getUserName() As String
getUserName = Environ("Username")
End Function
Then open property window for your TForm, select "userId" field, select "Data" tab in property window, find row with "Default Value" and put next expression: =getUserName().
I don't know why but access don't see Environ() function.
Second way.
Implement OnLoad event like this:
Private Sub Form_Load()
Me.userID.DefaultValue = "=""" & Environ("username") & """"
End Sub