Access VBA: run function after changing data - vba

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 & ";", "").

Related

ms Access: Switching between subform recordsource when changing records

I have a main form and a subform on it. I want the recordsource of the subform to change depending on the value of an ID field of the main form. This works when I do it in the Form_Load event of the parent Form. However, I also want it to update when the user switches to a new record on the main form (single form) while it is open.
The code I have atm is the followng:
Private Sub Form_Load()
'***************************************************************************
'Purpose: Switch subform recordsource based on recipe ID
'Inputs: None
'Outputs: None
'***************************************************************************
Dim strRecipeID As String
Dim strSub1QueryName As String
strRecipeID = Forms![frmMainForm]![RecipeID] 'get RecipeID
strSub1QueryName = DLookup("[QueryName]", "tblRecipeQueries", [RecipeID] = strRecipeID) 'Get name of query
Forms!frmMainForm!frmSub1Sub.Form.RecordSource = MakeSqlString(strSub1QueryName) ' Set Subform1 recordsource
End Sub
MakeSQLString() is a function that gets the SQL SELECT statement as a string from a given access query. I tried the change event, the Click event and the Current event and requerying the subform after that but none have any impact on contents of the subform (the recordsource does not change) but stays the same one that was selected when the form loaded.
I'm not sure which other events could work for this or from where I can execute the change in recordsource most effectively, maybe the subform instead? Maybe it has something to do with the subform loading before the parent form? I'm not sure, might be a rooky mistake.
I appreciate any help. BTW, the fields on the subform stay the same, so changing the recordsource just updates the calculations that are made in the query to get the values for those fields. The query names are stored in a table in my db. Again, It works in the Load event of the main form I just can#t get it to update on the fly while the main form is open and the user navigates to a new RecipeID on said main form.
In your DLookup you are not enclosing the where portion in quotes. You have:
DLookup("[QueryName]", "tblRecipeQueries", [RecipeID] = m_selectedId)
But it should be:
DLookup("[QueryName]", "tblRecipeQueries", "RecipeID =" & m_selectedId)
That should take care of your issue.

How to Update the Values of a Dropdown in a Subform when the Main Form Changes

I have two forms:
InterviewMaster and InterviewDetail
InterviewDetail opens up as a subform in InterviewMaster and these two forms are linked through a common field called InterviewID
In InterviewDetail I have a textbox called Questiontype as well as combobox called InterviewDropdown.
The data in the dropdown varies based on the data on in the textbox. To make this happen, I have a next button to move to the next question. Whenever I click on next the following runs:
Dim ctlCombo As Control
Set ctlCombo = Forms!InterviewDetail!cmbInterviewDropdown
ctlCombo.Requery
The Row Source setting for my combobox is set to look up the required answers, again this is based on the value as per the textbox:
SELECT [queryAnswerOptions].[Answer] FROM queryAnswerOptions ORDER BY [Answer];
So the options are determined by my query called queryAnswerOptions
So as I cycle through my questions using my next and previous buttons, the dropdown options are updated based on the value of my textbox. This works perfectly when I open the subform from the navigation pane. However, when I open the main form and click on the next button my dropdown does not have any values. I've tried requerying the subform with no luck. I've also tried opening the subform in full screen from my main form but this also does not work. I also don't want to go that route as it does not work well with the overall flow of my form.
Any assistance will be greatly appreciated.
Problem with the query WHERE criteria parameter is when form is installed as a subform, the form reference no longer works because it must use the subform container control name. I always name container control different from the object it holds, such as ctrDetails.
Option is to put SQL statement directly in combobox RowSource instead of basing combobox RowSource on a dynamic parameterized query object - then it will work whether form is standalone or a subform.
SELECT Answer FROM InterviewAnswers WHERE QuestionID = [txtQuestionID] ORDER BY Answer;

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

How to reference events on objects in a subform from inside the main form

Imagine a form that, for demontration purposes, contains only a subform and a textbox that need to mirror each other.
I tried to accomplish this by setting the Control Source on the textbox to the value of the field in the subform and this worked to make them mirror each other, but the textbox isn't editable so this is an unsuitable solution.
The next thing I decided to try was using the AfterUpdate event on both controls to run code that sets the value of the other control.
This is easy for the textbox:
'Set value of Notes field on subform whenever value of the corresponding textbox
Private Sub Notes_Textbox_AfterUpdate()
Me.subform.Form![Notes] = Me.Notes_Textbox.Text
End Sub
However, this isn't as simple for the subform field. I don't know how to reference an event on a field in a subform in a way that will still allow me to reference controls outside that subform.
As a demonstration, I need a way to do this:
Private Sub subform_Notes_AfterUpdate()
Me.Notes_Textbox.Text = Me.subform.Form![Notes]
End Sub
I can access the subform fields AfterUpdate event within the scope of the subform but if I do that, then I can't access the Textbox because it is in the main form, not the subform.
So I either need a way to define an event function on a field in a subform from within the scope of the main form, or a way to make the textbox part of the subform while maintaining the subforms ability to open in datasheet view.
Not sure I get the whole problem (how does datasheet view come into play?), but this should do what you want:
(in the subform)
Private Sub Notes_AfterUpdate()
Me.Parent!Notes_Textbox.Value = Me![Notes].Value
End Sub
Edit from comment: By far the easiest solution would be a continuous form instead of a datasheet in a subform.
You can have two textboxes, a one-liner in the Details section, and a large one in the form header or footer. Both have the same control source and are automatically updated when the other one is edited. No code required.
If it must be a subform-datasheet, you can use a similar approach: bind the main form to the same recordsource, and in On Current of the subform, move the main form to the same current record.

VBA - Filter subform on unbound field in form

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!