MS Access Update Unbound Form with Bound Form - vba

I have three forms in a database. Forms A, B, C. The parent form is A and the child form is B at a one to many relationship. As I put information into Form B I would like to click a button on different records and have Form C which is also located on Form A population with the correct information.
I created a query that controls Form C which reads with the following
[Forms]![frm_A1_SiteInformation]![frm_A1A_OutletInformation].[Form]![outletID]
This is to update the query with the new pass through key when I perform the docmd.Requery command.
This is where I get confused because I cannot get Form C to refresh with the correct information. It just keeps saying the first record.

DoCmd.Requery just requeries whatever object has focus. Could do:
Forms!FormA.FormCsubformcontainerName.SetFocus
DoCmd.Requery
Or just:
Forms!FormA.FormCsubformcontainerName.Requery
Or if you want to requery and put focus on FormC:
Forms!FormA.FormCsubformcontainerName.Form.Requery

Related

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;

How to link dynamically loaded unbounded subforms to main form?

I have a bound main form (FormA) with a combobox on it and two unbound subforms (subfrmA & subfrmB). (Both forms are attached to a table however I want them to load onto the main form where I placed an unbound subform as a placeholder.)
The combobox has two values “a” and “b.” When a is selected I want subfrm A to load onto Form A. When b is selected I want subfrmB to load onto Form A. I think have this part working.
However when I select a record on the main form the associated subforms don’t appear. When I try to link the subforms to the main form an error message appears saying I can’t build a link between unbound forms.
The packageID is the link between the main form and subform and is a hidden field on all forms. Whenever the packageID is automatically updated the psckageID in the subform fields are also updated.
Case”A”
Me.subfrmAB.SourceObject=“FormA
Me.packageDetailsID=Me.subfrmAB.packageDetailsID
Case “B”
Me.subfrmAB.SourceObject=“FormB”
Me.packageDetailsID=Me.subfrmAB.packageDetailsID
EDIT: I ended up creating two subforms subfrmA (Form A) and subfrmB (Form B). Then I linked both to the parent form via the master and child links.
I make one of the subforms visible and the other invisible depending on what the user selects in the combobox of the main form.
Everything works except Form B won’t load, but the container loads. I tried loading Form B separately by itself it still won’t load. I also deleted subfrmA and Form B still doesn’t load.
Here is my edited code:
Select Case Me.Authorization.Text
Case “A”
Me.subfrmA.Visible = True
Me.subfrmB.Visible = False
Me.subfrmA.SourceObject = “Form.A”
Case “B”
Me.subfrmB.Visible = True
Me.subfrmA.Visible = False
Me.subfrmB.SourceObject = “Form.B”
End Select
The only line that doesn’t work is the Me.subfrmB.SourceObject=“Form.B” and really there’s something that’s preventing the form specifically loading. I wrote the same code for Form A and Form B but can’t figure out what’s wrong with Form B.
Can certainly be done. Here is a simple example that works for me.
Main form is bound to table Games. Forms used as subform are Umpires and Teams.
Combobox properties:
ControlSource: UNBOUND
RowSource: Umpires;Plate;UmpID;Teams;HomeTeam;TeamID
RowSourceType: ValueList
BoundColumn: 1
ColumnCount: 3
ColumnWidths: 1.0";0";0"
Code:
Private Sub Combo108_AfterUpdate()
With Me
.ctrAB.SourceObject = .Combo108
.ctrAB.LinkMasterFields = .Combo108.Column(1)
.ctrAB.LinkChildFields = .Combo108.Column(2)
End With
End Sub
You could have "A", "B" for the form names in combobox RowSource and then if both forms have same name key fields, don't need them in RowSource, just hard coded. Not entirely clear what the key field names are. Then code like:
.subfrmAB.SourceObject = "subfrm" & .Combo108
.subfrmAB.LinkMasterFields ="packageDetailsID"
.subfrmAB.LinkChildFields = "packageDetailsID"
If you want to save "A" and "B" to main form record, then bind the combobox to field. Then for subforms to change for each record while navigating main form, also have code in form OnCurrent event.
Something to be aware of when coding interaction between form/subform: subforms load before main form - seems odd but is true.
One option is to create a dummy table with one record and bind the subform to that. But you'd have to read and write all the values with code.

Open form B that has a subform based on form A

Form A has a button that opens form B which has a continuous subform which needs to filter records based on a field in form A.
The code attached produces the error: runtime error '2491'
The action or method is invalid because the form or report isnt bound to a table or query
I suspect the continuous form is the problem as it doesn't appear in the Access forms list but it does have a name in form B however I don't know how to reference it.
Form B is opened from elsewhere without a filter so I don't want to change form B.
CODE:
... DoCmd.OpenForm Formname:="B",wherecondition:="woid = " Forms!A!cmbowoid.Column(0)
Let me guess: Form B has no RecordSource. Only the subform under B does. When you execute the OpenForm method on Form B, you get the error because the WhereCondition has no records to act on. The WhereCondition has no knowledge of (and no effect on) B's subform.
The fix:
1) Use OpenArgs:
DoCmd.OpenForm Formname:="B", OpenArgs:="woid = " Forms!A!cmbowoid.Column(0)
2) In Form B's Form_Load event, enter this code:
Me.subFormB.Filter = Me.OpenArgs
As long as your OpenArgs is a valid Filter expression, this should work.

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.

How to refresh an access form

I am building an MS Access application in which all the forms are modal. However, after data change in a form, I want to refresh the parent form of this form with newer data. Is there any way to do it. To elaborate further :
Consider there are two forms, Form A and Form B. Both are modal form. From Form A, I initiate Form B, and now Form B has the user attention. But at the close of form B, I want to refresh the Form A. Is there a way to do it?
You can repaint and / or requery:
On the close event of form B:
Forms!FormA.Requery
Is this what you mean?
No, it is like I want to run Form_Load
of Form A,if it is possible
-- Varun Mahajan
The usual way to do this is to put the relevant code in a procedure that can be called by both forms. It is best put the code in a standard module, but you could have it on Form a:
Form B:
Sub RunFormALoad()
Forms!FormA.ToDoOnLoad
End Sub
Form A:
Public Sub Form_Load()
ToDoOnLoad
End Sub
Sub ToDoOnLoad()
txtText = "Hi"
End Sub
"Requery" is indeed what you what you want to run, but you could do that in Form A's "On Got Focus" event. If you have code in your Form_Load, perhaps you can move it to Form_Got_Focus.
I recommend that you use REQUERY the specific combo box whose data you have changed AND that you do it after the Cmd.Close statement. that way, if you were inputing data, that data is also requeried.
DoCmd.Close
Forms![Form_Name]![Combo_Box_Name].Requery
you might also want to point to the recently changed value
Dim id As Integer
id = Me.[Index_Field]
DoCmd.Close
Forms![Form_Name]![Combo_Box_Name].Requery
Forms![Form_Name]![Combo_Box_Name] = id
this example supposes that you opened a form to input data into a secondary table.
let us say you save School_Index and School_Name in a School table and refer to it in a Student table (which contains only the School_Index field). while you are editing a student, you need to associate him with a school that is not in your School table, etc etc
to refresh the form you need to type -
me.refresh
in the button event on click