VBA - Filter subform on unbound field in form - vba

I am passing a value from a lookup form to another form. The value shows in an unbound field on the form (works great). I also want the subform to be filtered by what is in this unbound field. So, for example, I pick 2016 on my lookup form. It brings up another form, enters the value 2016 into an unbound field called yearvar. I cannot get it to filter what's in the subform. Here's my code that's not working:
yearvar.Value = Me.OpenArgs 'Enters year from lookup form into field call yearvar
Forms!frmEthEntCatYr.frmEthEntCatSubCatYr.Form.Filter = "[EthYear]= " & Chr(34) & Forms!frmEthEntCatYr.yearvar & Chr(34)
Form name is frmEthEntCatYr. Subform name is frmEthEntCatSubCatYr. Unbound field on form is called Yearvar. Field on subform that I want to filter is called EthYear.
What am I doing wrong? Thanks in advance!

Problem solved and it was an easy fix (thanks to someone helping me with my dah moment). I had originally tried to link the main form and subform using the Subform field linker wizard, but the unbound field was not showing (yearvar) in the Master Fields dropdowns in the Link wizard. Someone suggested just putting it in the Property Sheet - in Link Master fields, I typed yearvar and in Link Child Fields, I typed Ethyear. Works perfectly now. Lesson of the day for me: sometimes the wizards do not do what we want them to do, and you have to do it the manual way. And I didn't even have to write code to do this!

Related

Open a form to a record selected in a subform

I want to open a form to the record selected in the subform of a different form. I've tried following what I’ve seen in other posts about this, but I still can't get it to work. I think I'm just missing some very basic detail but this is my first Access database and I can't figure out what it is. Thanks in advance for your assistance.
Details:
F_Detail - (This is a Single Form containing details on a project.)
F_List - (This is a Single Form containing a subform and a button.)
subF_List - (This is the subform contained in F_List. This subform is in Datasheet view)
Project_ID - (This is the primary key contained in subF_List and in F_Detail. It is the common criteria between the two. It is Short Text type.)
subF_List displays row after row of projects. F_Detail displays details about a single project at a time. From F_List I want to select a row in subF_List and then click the button to open F_Detail, and F_Detail will be displaying details of the project whose row was selected in subF_List when I pressed the button.
What I Have for the Button:
On Click > Event Procedure
Private Sub ProjectDetailButton_Click()
DoCmd.OpenForm "F_Detail",,,"Project_ID = " & Me.Project_ID
End Sub
Upon clicking the button, I get an error saying, "Compile error: Method or data member not found" and it highlights the .Project_ID at the end of my code.
I don't know what I'm doing wrong and would appreciate any help you can lend. Please let me know if I've left out any needed detail.
Use apostrophe delimiters for text field parameters. Use # for date/time, nothing for number.
"Project_ID = '" & Me.Project_ID & "'"
Me is alias for form or report the code is behind. To reference a field in its RecordSource:
Me!Project_ID
Code on main form referencing field on subform must reference through subform container control. I always name container different from object it holds, such as ctrProjects:
Me.ctrProjects!Project_ID
I name controls different from fields they are bound to, such as tbxProject:
Me.ctrProjects.Form.tbxProject

Access VBA: run function after changing data

I have a subform within a form.
In this subform I enter line per line the mail-addresses of customers.
I want to set a mailstring like this into the parent form of the subform.
Example in Subform:
mail1#test.com
mail2#test.com
mail3#anothertest.com
whenever I leave the field "mail" on subform a string such as
mail1#test.com; mail2#test.com; mail3#anothertest.com
should be put into field "Parentmail" on mainform.
It is not the problem to CREATE the mailstring (3 mailaddresses) but on WHICH EVENT on the subform (or field on subform) can I run the function??
I tried
Form_AfterUpdate
(subform !) but it only works when I add new mail-addresses or when I update an existing one, not when I delete one.
Which event will fire when I have maybe 3 mail-addresses and delete one?
Form_Delete
did not do the job.
Thx Michael
Try the subform BeforeDelConfirm event: Replace(mainformfield, subformfield & ";", "").

pass value within one form in ms-access

hey i am a total noob and only found ways to pass a value between two forms.
this is the form i am working with: form.
in this form (event.form) there is a subform (Fundort_kurz-Subform) which lists data. when i click one row inside the subform the value for "objid" appears in textbox "text501" by "=[Fundort_kurz-Subform].[Form]![Objid]". I automatically want the value from field "text501" to be passed to field "Objid_3" which is meant to write the value into the table.
Not sure what you are doing, but use the OnCurrent event of the subform:
Me.Parent!Objid_3.Value = Me!text501.Value
And do rename your controls to something meaningful.

MS Access: Unbound fields in a subform not showing on a report

I'm having a bit of trouble figuring out how to fix an issue with my MS Access database report output.
First:
There are three forms, one of which is for navigation purposes (HOME) that has two navigation buttons in a navigation control (which link to either of the two other forms) and on subform field I use much like an iframe. There are also two reports, one for each of the other forms.
Second:
Both forms have unbound text fields that need to be printed onto the report output. There is no need for these fields to be saved or put into a table. The values of these forms are printed without issue when the form is isolated (i.e.- modal, form view, etc.). The unbound fields are part of the main body of the form where controlled fields are located.
Only when a form is viewed in the "HOME" form in the subform box/iframe does the report show #Name? instead of their intended values. Also worth noting, the subform does not have the Link Master Fields or Link Child Fields options.
The code used on the print command object/button is as follows and is located on the footer of the non-HOME forms:
Private Sub cmdPrint_Click()
Dim strWhere As String
If Me.NewRecord Then 'Check there is a record to print
MsgBox "Select a record to print"
Else
strWhere = "[CustomerID] = " & Me.[CustomerID]
DoCmd.OpenReport "TransferAgreement_EC", acViewPreview, , strWhere
End If
End Sub
Any suggestions? I tried searching here and Googled the issue, but I haven't quite found the solution to my problem. Thanks in advance for your input!
Okay. So, found the solution by just running a whole lot of experiments and this fixed it.
On the report itself, on the data field needs to call to both the navigation form and the subnavigation box, then point to the unbound field. For example, one field in the control source is:
=[Forms]![Home]![NavigationSubform].[Form]![GCSerial_1]
Home = The navigation form
NavigationSubform = The nagivation subform box (what I consider an "iframe")
Form = Just a call to to a form, not specific
GCSerial_1 = The unbound text field
In this way, the report will not print the unbound fields when it is not a subform of the navigation form. For example, by default my two other forms are modal pop ups. When I "form view" them as modal prompts, the unbound fields display #Name? when I click my print command button, which specifies a report (no longer) contingent on a subform and not an isolated form.
THANKS FOR THE HELP AND INSPIRATION! <3

Dlookup in a form with 2 subforms (The requirement for the dlookup is in the second subform)

Access 2007
Form Name is: MineLocationQuery1
1st Subform name is:MineExtractionSubform
2nd Subform name is: ExtractionLineSubform2
I am trying to use dlookup to get check a value in a table and then multiply it by another value. It works with: NO Subform and with 1 Subform. But when I add 2 subforms I cannot get it to work.
Here is what I have tried so far
Simple form lookup that works:
=DLookUp("[Price]","[Resource]","[AtomicRef]='" & [Forms]![ExtractionLine]![AtomicRef] & "'")*[Tonnage]
Here is the working example from a form created with 1 subform:
=DLookUp("[Price]","[Resource]","[AtomicRef]='" & [Forms]![MineExtraction1]![ExtractionLineSubform]![AtomicRef] & "'")*[Tonnage]
And here is 1 of the many attempts I have made with 2 subforms and it is not working.
=DLookUp("[Price]","[Resource]","[AtomicRef]='" & Forms![MineLocationQuery1]!MineExtractionSubform.Form!ExtractionLineSubform2.Form.AtomicRef & "'")*[Tonnage]
What am I doing wrong other than getting tired and frustrated at copying and pasting many attempts into the worths textbox control source and repeatedly getting #Name errors
If I wish to refer to a nested subform in a textbox on the main form, I might say:
=[SubformControl1].[Form].[SubformControl2].[Form].[AControlName]
Note that this uses the name of the subform control and Form as a reference to the object contained by the control.
So this (split for ease of reading) looks right:
=DLookUp("[Price]","[Resource]","[AtomicRef]='" & Forms![MineLocationQuery1]!
MineExtractionSubform.Form!ExtractionLineSubform2.
Form.AtomicRef & "'")*[Tonnage]
However, it is very easy to get names wrong. For example, you may be referring to the form contained, rather than the subform control name. You can use the Expression Builder to troubleshoot, or you can refer to the form bit by bit in the immediate window (Ctrl+G) to ensure you have the names right. For example:
?Forms![MineLocationQuery1].Name
?Forms![MineLocationQuery1]!MineExtractionSubform.Name
More information: http://access.mvps.org/access/forms/frm0031.htm
EDIT re comments
If you work in subform 2, as I believe you should in this case, you can simply refer to [atomicref]:
=DLookUp("[Price]","[Resource]","[AtomicRef]='" & [atomicref] & "'")