Get ID Of Selected Row In Sub Form - vba

I have a Parent Form with a Sub Form and I want the user to be able to select a record from the sub form, then click a button on the Parent Form, which will launch a "new" form that has full demo's pertaining to the selected record from the sub form.
How would I do this in Access 2013?

You can pass the ID as a parameter when opening the "new" form.
On your button's Click event:
Private Sub Command0_Click()
'Get the ID
Dim id_ As Long
id_ = Me.SubformName.Form!ID
'Open the new form and pass the ID to the .OpenArgs
DoCmd.OpenForm "FormName", acNormal, , , acFormPropertySettings, acWindowNormal, id_
End Sub
On the Form's Load event, check the .OpenArgs and filter the form (or whatever else you need to do) to the supplied ID.
Private Sub Form_Load()
With Me
If Not IsNull(.OpenArgs) Then
.Filter = "[ID]=" & .OpenArgs
.FilterOn = True
.Caption = "ID: " & .OpenArgs
End If
End With
End Sub

Related

How can i pass a variable from userform1 textfield dblclk event to userform2 and then dynamically pass data back to userform1 through cmdbutton click

On userform1 I have a text field that when it is doubleclicked opens a "timebox" Userform 2. The user then can adjust the time and then either cancel or apply. Apply should fill in Userform1 Textbox with the selection from Userform2.
If it was once instance no problem but i'm trying to use the Userform 2 on 28 + fields and can't reference each one separately. I need to pass the textfield.name on Userform1 to give Userform2 the address in which to send the data. But its userform2 cmdbuttom - Apply.
I looked at ByVal and ByRef which seems to work when its a Sub but not when its a cmdbutton.
Userform1 > Field
Public Sub AgStart1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim fieldname As String
fieldname = UserForm1.AgStart1.Name
TimeForm1.Show
End Sub
UserForm2
Public Sub TimeApply1_Click()
Dim timeselect
timeselect = Me.txt_Hours.Value & ":" & Me.txt_Mins & " " & Me.txt_AmPm
'I need the value above to be sent back to the field that spawned the Userform2
TimeForm1.Hide
End Sub
OR a better way like activecell for user form fields
Userform1 text field
Useform2 spawned from dblclk
Something like this should work:
UserForm1
Public Sub AgStart1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
PopTime AgStart1
End Sub
Public Sub AgStart2_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
PopTime AgStart2
End Sub
'etc etc
Sub PopTime(ctrl As Object)
Dim frm As New TimeForm1
Set frm.TheControl = ctrl 'pass the control to the second form
frm.Show
End Sub
UserForm2
Public TheControl As Object
Public Sub TimeApply1_Click()
'set the value of the passed control
TheControl.Value = Me.txt_Hours.Value & ":" & Me.txt_Mins & " " & Me.txt_AmPm
Me.Hide
End Sub

TempVars to save record ID for opening a subform

I am currently getting "Run-time error '-2146500594 (800f000e)': Method 'Form' of object'_SubForm' failed" when I run the following code:
TempVars.Add "TempFlightID", Me![FlightsPlan_SubFrm].Form!Text48
I am working on a set of navigation buttons for a set of forms some with and some without subforms in MS-Access. The goal is to have it open the needed form and have it be in the same record in the parent form as on the form that the user is currently on, and if there is a subform the same record in the subform as well. If there is not a subform it should save the ID for the record and the next time the user opens a form with a subform it should open with that ID.
I think I have the parent form navigation functional using open arguments, but can't seem to get the subform to work as intended. I am attempting to accomplish this by saving the subform ID as a TempVars as below (objects renamed for clarity):
Private Sub OpenParentForm2Btn_Click()
IDref = Me.ID
TempVars.Add "TempID", Me![SubFrm_1].Form![SubFrm_1ID]
DoCmd.OpenForm "ParentForm2", , , , , , IDref
DoCmd.Close acForm, "ParentForm1"
End Sub
Then on the second subform:
Private Sub Form_Load()
If [TempVars]("TempID") > "" Then
Me.Recordset.FindFirst "ID=" & [TempVars]("TempID")
Else
DoCmd.GoToRecord , , acLast
End If
End Sub
and for completeness here is the working code on the second parent form:
Private Sub Form_Load()
If Me.OpenArgs > "" Then
Me.Recordset.FindFirst "ID=" & Me.OpenArgs
Else
DoCmd.GoToRecord , , acLast
End If
End Sub
Any help resolving the error or directing me to a different method to accomplish this than TempVars would be appreciated.
-Cameron

Cannot Pass Values Between 2 popup forms in Access 2013

I have a ListBox in access 2013 , I need to pass values from (form1) to (form2) after double click on Lisbox in (form1). I have already try this way:
The below code when (form2) is activated , it worked normally but if forms
property popup = true
It not working , values cannot read in (form2) .
Private Sub Form_Activate()
If CurrentProject.AllForms("SearchFrm").IsLoaded = True Then
BBAIDHidenTxt.Value = Forms![SearchFrm]![SearchList].Column(0)
PrNumberHidenTxt.Value = Forms![SearchFrm]![SearchList].Column(1)
SupplierHidenTxt.Value = Forms![SearchFrm]![SearchList].Column(2)
OrderDateHidenTxt.Value = Forms![SearchFrm]![SearchList].Column(3)
TransactionTypeHidenTxt.Value = Forms![SearchFrm]![SearchList].Column(4)
TotalValueHidenTxt.Value = Forms![SearchFrm]![SearchList].Column(5)
End If
End Sub
note : i'm new in access database developing
A dialog Popup form stops all code on the calling form until the 2nd form opened is closed.
Pass the value(s) in an Openargs property to the Popup.
You can use TempVars or Pubic Variable defined in module to store the value and later access them in other form.
Best option is to use OpenArgs option as already mentioned by Minty.
Form1
Private Sub listBox_DblClick(Cancel As Integer)
Dim lstStr As String
lstStr = Me.listBox.Column(1)
DoCmd.OpenForm "SearchFrm", acNormal, , , , acDialog, lstStr
End Sub
Form2
Private Sub Form_Load()
If Not IsNull(Me.OpenArgs) Then
MsgBox Me.OpenArgs
End If
End Sub

Is it possible to raise events on a subform's form, when that subform is bound to a table?

I've got a form with a subform which is bound to a temporary table. The temporary table contains the result of a pivot, but I'd like users to be able to edit that pivot and then push the changes to the source table.
To do this, I'd like to fire events on AfterInsert, AfterUpdate and Delete so I can act on changes. As I understand it, the subform's form property refers to a temporary datasheet form when the subform is bound to a table. However, I can't get this temporary form to raise any events.
MCVE:
I have a database with a single table, Table1, a single form, Form1, and that form has a single subform control which is unbound.
I have a class, Class1, with the following code:
Private WithEvents subformForm As Access.Form
Public Sub Init(subformControl As Access.SubForm)
Set subformForm = subformControl.Form
subformForm.OnCurrent = "[Event Procedure]"
End Sub
Private Sub subformForm_Current()
MsgBox "Current!"
End Sub
The form, Form1, has the following code:
Private c1 As Class1
Private Sub Form_Load()
sub1.SourceObject = "Table.Table1"
Set c1 = New Class1
c1.Init sub1
End Sub
However, when I move about records, add records, and do whatever in that subform, the subformForm_Current event never fires.
It's because the subform object isn't a form having a code module. Thus, nowhere is the event procedure to run.
So, create a form in datasheetview using the table as source, having no code module, and use that as the subform:
Your code is ignored.
Now, adjust the form to have a code module:
Your code runs as expected.
Anyway, that's how it works for me in Access 2016.
As Gustav pointed out to me, a form needs a module to raise events.
This means you can't use the auto-created temporary datasheet form. But you can create your own form to take its place.
To work around the limitation, I created a form named frmDynDS, and set it's default view to datasheet view. Then, I opened the form in design view and added 255 text boxes to the form using the following code:
Public Sub DynDsPopulateControls()
Dim i As Long
Dim myCtl As Control
For i = 0 To 254
Set myCtl = Application.CreateControl("frmDynDS", acTextBox, acDetail)
myCtl.Name = "Text" & i
Next i
End Sub
I added a module, and added the following code to dynamically load a table into the form:
Public Sub LoadTable(TableName As String)
Dim fld As DAO.Field
Dim l As Long
Me.RecordSource = TableName
For Each fld In Me.Recordset.Fields
With Me.Controls("Text" & l)
.Properties("DatasheetCaption").Value = fld.Name
.ControlSource = fld.Name
.ColumnHidden = False
.columnWidth = -2
End With
l = l + 1
Next
For l = l To 254
Me.Controls("Text" & l).ColumnHidden = True
Next
End Sub
Then, I could adjust Class1 to the following:
Private WithEvents subformForm As Access.Form
Public Sub Init(subformControl As Access.SubForm, TableName As String)
subformControl.SourceObject = "Form.frmDynDS"
Set subformForm = subformControl.Form
subformForm.LoadTable TableName
subformForm.OnCurrent = "[Event Procedure]"
End Sub
Private Sub subformForm_Current()
MsgBox "Current!"
End Sub
And Form1 to the following:
Private c1 As Class1
Private Sub Form_Load()
Set c1 = New Class1
c1.Init sub1, "Table1"
End Sub
Using this approach, you can have a subform that can display tables created on the fly in datasheet view, and handle events for that subform.
You can have multiple subforms bound to frmDynDS displaying different tables, and handle events in different event handlers, on a single form.

SubForm with parameters from the main form

I have a form with a subform that are linked by an ID. The problem is the subform has a combo box that needs a parameter from the main form for the row source.
The form is about sales calls and they need to be able to link many proposals and contracts as they want. The combo box for the list of proposals and contracts needs to be in relation with the mills or clients of this sales call.
I know I could do a Forms!FormName!ControlName to get the mill list but does this mean I have a design problem?
Or should I do a list filled when a user click on an element in a combo box in the main form? but then I would have to handle the save and delete myself.
Thank you
If a subform requires a main form to function properly you can check for a parent. For example:
Private Sub Form_Open(Cancel As Integer)
Dim strParent As String
Dim strSubname As String
GetParent Me, strParent, strSubname
If strParent = "None" Then
MsgBox "This is not a stand-alone form.", , "Form Open Error"
Cancel = True
End If
End Sub
Function GetParent(frm, ByRef strParent, ByRef strSubname)
Dim ctl
On Error Resume Next
strParent = frm.Parent.Name
If Err.Number = 2452 Then
Err.Clear
strParent = "None"
Else
For Each ctl In frm.Parent.Controls
If ctl.ControlType = acSubform Then
If ctl.SourceObject = frm.Name Then
strSubname = ctl.Name
End If
End If
Next
End If
End Function