Requery subform (1) after adding a new subform (1) record via pop-up subform (2) - Access 2007 - popup

I have a form with two subforms (1 and 2). Subform 1 stores the data continously for account breakdowns. Using a pop-up subform (subform 2), the user enters data to change or update the account breakdown stored on subform 1. On submit, the information is stored in the sub-table linked to subform 1, but the data does not refresh and add the new information in a new record in subform 1 unless I manually click the refresh all button in the home tab.
So far, I have tried the following code on form_afterupdate of subform 2, which does requery subform 1 automatically but creates a run-time error (2450) when the main form is closed:
Private Sub Form_AfterUpdate()
On Error GoTo Err_Form_AfterUpdate
Forms!frmSpendPlan!frmSpendPlanSub.Form.Requery
Exit_Form_AfterUpdate:
Exit Sub
Err_Form_AfterUpdate:
MsgBox Err.Description
Resume Exit_Form_AfterUpdate
End Sub
If anyone has a way to automatically requery subform 1 data once subform 2 is submitted without creating an error please let me know, I appreciate any help you can provide.
Thanks!

I have the same problem as you sometimes! The form doesn't refresh properly.
Method 1
try this line of code instead of the current requery code. It shouldn't give you an error but may still not work due to bugs...
Form_frmSpendPlanSub.Requery
I use this format to reference across forms and it works well usually.
Method 2
1) What you can try is in design view click on the form that is doing the "After_Update"
2) Right-Click on the form and click "BUILD EVENT.." and select "Expression Builder"
In expression builder you can see exactly how the form you are in links to the form you want to get to and what you want to do...
So you would navigate yourself by going to FORMS--> Loaded Forms--> frmSpendPlanSub and then from the list you can double click on " "
This will get you referenced to the form you want to affect.
Then all you have to do is copy the code that it gives you in the box above into your VBA code and then reference what you want to do...
E.g.
Forms![frmMainForm]![frmSecondForm].Form![frmThirdForm].Form.Requery
Anyway hope that helps...

Related

Referring to a Subform from a Query

In MS Access 2010, I have a Query which quotes the following in the Criteria;
[Forms]![frm_Add_Item_Subform].[ActiveControl].[Caption]
This lets me use the "Caption" text of a Button within the query. The following code is on the Button to capture the click.
Private Sub cmdClickMe_Click()
Debug.Print Me.cmdClickMe.Caption
Debug.Print Screen.ActiveControl.Caption
End Sub
I obtained information on how to do this at the following StackOverflow URL.
use caption of pressed button from main form in query of other form
The functionality works ok when used in a Form. But doesn't when its used within a Subform (which is where I need to use it). I believe I need help tweaking the Query Criteria to refer to the Subform correctly.
Hoping someone will be able to give me some guidance on this. Thanks in advance!
P.S. The main Form name is "frm_Add_Item_Main"
This is how to refer to subforms:
Forms!mainFrm!subFormControlName.Form
so you need:
Forms!mainFrm!subFormControlName.Form.ActiveControl.Caption
where subFormControlName is the name of the subform control (not the name of the subform) in the main form (it shows when the subform control is selected in the main form)
This worked for me :
[Forms]![myMainForm]![mySubForm]![myComboBox]

in access need to doubleclick on record in subform to show detail on main form

I have a main form with a subform. The subform is continuous and shows all of the records. The main form works great for adding and editing records. I need to be able to double click on a record in the subform and that record be displayed on the main form for editing.
By experience I reccomend you to achieve what you are trying by placing two subforms into the master one. In one you have the list and in the other the details.
In the master form you place a hidden link field that is populated when you doubleclik on the list.
This field, let´s call it [link] is the Link Master Field in the detail subform, and the Link Child Field is the id (primary key) to identify your selection.
This will link your double click (or simple) with showing the details.
I found a solution that works to move to display the record from the subform in the master for editing.
In the DblClick event on the subform I put in the following code:
Let Forms!FrmNotes.CboNotesID = TxtNotesID 'TxtNotesID is the ID on the subform
Call Me.Parent.DisplayRecord
On the main for I created
Public Sub DisplayRecord()
CboNotesID.SetFocus
DoCmd.SearchForRecord , "", acFirst, "[NotesID] = " & "'" & Screen.ActiveControl & "'"
End Sub

Access Form Go To Record

In Access, I have a main form with a listbox. The listbox is used to navigate to different records on the main form. The main form also has a button with the following code to open a dialog form on which I add a new record.
Private Sub New_Btn_Click()
DoCmd.OpenForm "New Issue", , , , acFormAdd, acDialog
Requery
Me.ID_Box.SetFocus
End Sub
After I close the dialog form, I want the listbox to update and to select the new record that added on the "New Issue" form AND I want the main form to navigate to the new record.
I cannot figure out how to make either of these actions occur. Please help me find the best method for these steps.
Assuming the listbox has a rowsource, you can requery the ListBox to show the new record like this:
ListBox1.Requery
Then to move the 'main' form to the latest record (assuming the ID/PK is an autonumber field, which is how the form recordsource is sorted):
DoCmd.GoToRecord , "MainFormNameHere",acLast
If you don't have an ordered recordset, you can use
Me.Recordset.FindFirst "PrimaryKeyField = " & NewRecordPK
Where NewRecordPK is a variable with the new records primary key stored.
You could call this from the dialog if you like. Just make sure you save the record first using something like Docmd.Save Then,
Forms!frmMainForm.Form.Recordset.FindFirst "PrimaryKeyField = " & Me.PrimaryKeyField

refresh vb6 form in background without losing user entered data

I'm working with some old VB6 code and have a scenario I'm trying to correct. I have a form that allows you to enter a person ID (use renters this in a textbox at the top) and click "show" it lists the appointment in the textbox on the same form. There is also a button that loads a new form that allows the user to edit the displayed data and save the change.
E.g. Change persons age from 65 to 64. I make this change, and save it. The save is successful and I unload the form to return to Form1. However, I must click "show" again to refresh the displayed data in the textbox to ensure the change is visible. I cannot figure out how to refresh this form, so the user doesn't have to click "show" to repopulate the textbox with the new value. Can anyone assist? I can't just create a new instance of Form1, because if I did that the person ID field would be blank.
Thanks!
Short version: How do I refresh a form to get latest data while still obtaining the relevant person ID.
There's not enough information here to answer your question. The general pattern you'd use in this situation is as follows:
SearchForm launches ViewForm.
ViewForm launches EditForm. When ViewForm constructs its instance of EditForm, it passes Me to EditForm (perhaps by setting EditForm.Parent).
When EditForm's Save button click event fires, it calls Parent.ReloadData (where Parent is the ViewForm that launched the EditForm).
Here is the approach that should work. In the editor have a public property that returns whether the form was cancelled or not. (.Cancelled). You'll have an object that carries attributes of the person that you are trying to change the age of. Then it's pretty simple. Code in the main form:
dim oPerson as clsPerson
dim oFrm as frmAgeEditor
set oPerson = GetCurrentPerson()
set oFrm = new frmAgeEditor
with oFrm
set .Person = oPerson
.Show vbModal
if not .Cancelled then
' Update Main form with the contents of oPerson
end if
end with
You can just call in "Show" function right after updating.
Call Me.frmParent.Refresh
I used this to refresh the screen without losing data.
I had to dig through a lot of old code to figure it out. This section had been written a long time ago, and worked well.
Public Sub Refresh()
Call cmdDetails_Click
If tvwScreeningSchedule.Nodes.Count > 0 Then
tvwScreeningSchedule.SelectedItem = tvwScreeningSchedule.Nodes(1)
Call tvwScreeningSchedule_Click
End If
End Sub
It's quite a specific function, and "cmdDetails_Click" contains alot of custom validation, but it's not too dis-similar to AngryHackers answer.

Access parent and other open forms from a subform

In continuation to my application development, I have encountered another intersting problem.
In my MS Access application, I am using a form which contains only two controls - a textbox and a command button. This form is named as HEADER FORM.
HEADER FORM is used as a subform in header section of various other forms.
Figure below shows the subform and its use in one of the main form.
Now, what I want is that when user clicks on Logout button of the subform, the logout action shall happen (which is fine and I am able to figure it out) and all the forms that are open at that point in time shall be closed / unloaded.
I tried using following code to no avail.
Option Compare Database
Private Sub cmdHeaderLogout_Click()
If (loggedIn = 1) Then
loggedIn = 0
DoCmd.Close acForm, Parent.Form
End If
End Sub
When I try to click on logout button, control reaches DoCmd.Close statement above. And execution stops after giving following error message:
Run-time error: 2498. An expression you entered is the wrong data type
for one of the arguments.
and it points to Parent.Form text.
I am not able to figure out how to refer to parent form and unload the same?
You should refer to the name of the parent of the current form, that is:
DoCmd.Close acForm, Me.Parent.Name
The second argument of DoCmd.Close takes a string.
You can also loop through the forms collection.
FormsCount = Forms.Count - 1
For i = FormsCount To 0 Step -1
If Forms(i).Name <> Me.Name Then
DoCmd.Close acForm, Forms(i).Name
End If
Next