I'd like to refresh the table located under the History tab whenever I enter new data under Payments. I tried adding an onclick macro when History tab is clicked but the new macro editor in Access 2010 confuses me.
As can be seen in the picture below, I've already added a fourth entry in Payments subform and the new entry does not show up in the table under History Tab.
I think Refresh or requery is the way to go but I don't know how to set it up to get it to work. Any help would be appreciated.
The best place to run the requery would be the After Update event for the payments subform.
Private Sub Form_AfterUpdate()
Me.Parent.HistorySubformConrolName.Form.Requery
End Sub
Make sure you use the name of the subform control, not the name of the form contained. See http://access.mvps.org/access/forms/frm0031.htm
Creating events: http://office.microsoft.com/en-us/access-help/about-events-and-event-procedures-HP005186744.aspx
EDIT in this particular case:
Me.Parent.Payment_Info.Form.Requery
Me.qryListExpertsHistory_subform.Requery
' qryListExpertsHistory_subform (is the embedded form)
Related
I have a database with three tables: Employees, Consultations, and Customers. Here are the relationships:
Employees<--->Consultations
Consultations<--->Customers
I have a form for entering consultations (called "Consultations"), and the user can select the Customer from a combo box. This then displays the customer information on the form in a read-only format. If the user needs to update the customer information (name, type, department, etc.) they can click a button that opens another form to that customer's record. They can update the info, then close that form, and the "Consultations" form is updated with the new customer information. This all works fine.
I also want users to be able to enter a new customer record if they don't exist in the customers table. Currently, the user can click on a button, and a form is opened where they can enter all of the new customer information, called "Add-Customer". Once they close the form, the "Consultations" form is displayed again. Here is where I am having issues.
What I want to happen is that after the user enters the new customer, the new customer should be selected in the combo box. The combo box is holding the customer's "LastName, FirstName". I was able to get the new record to appear in the combo box, but the user still needs to manually select it. I want this to happen automatically.
Here is my code that runs when the user clicks OK on the "Add-Customer" form:
Private Sub Command1_Click()
'save customer record
DoCmd.RunCommand acCmdSaveRecord
'make add-customer form invisible
Me.Visible = False
'requery the customerlastname field on consultations form
DoCmd.Requery "CustomerLastName"
'close add-customer form
DoCmd.Close acForm, "Add-Customer"
End Sub
I tried adding a variable that stores the ID of the new record, then tried to have the combo box select that record, but couldn't get it to work. I've removed that from the above code.
Thanks!
UPDATED: Realizing you are working with a second form - which is not a subform, here is one possible solution.
You will need to pass the new value back to the original form. You can do this in several ways: (a) a global variable; (b) a hidden textbox; (c) create public function in main form. I chose to use a hidden field.
I assume you are opening the ‘Add’ form as modal. If not, then you need to do that.
(You will need to change the following code to use your combobox name and form name)
Add a hidden textbox on the main form i.e. named txtNameHidden
In the close code for the 'Add' form, add the following:
Forms!frmMyMainForm!txtNameHidden = me.txtName ' The value as it will appear in the combobox
In the Main form, after the line of code that opened the form, add the following code:
Me.cboNames.Value = Me. txtNameHidden
I am curious about your code 'DoCmd.Requery "CustomerLastName"' - does this work? How does it know to reference the control on the main form?
Preface: I have a form which has tabs. In one of the tabs, I have a list box, a button, and a sub-form. The list box is populated from two tables and has a bound column.
Needed: I need the sub-form to edit existing records of one of the tables the list box is built on and append records to the same table on button click. The sub form is to be linked to the non-bound column of the list box. Please help.. I have tried a few things in Vb but could not complete..
--Chegu.
I am not sure if I understand you correctly, but if you want to select a line in the listbox, edit the respective record in the underlying table and then save the record and update the listbox, you could work along those lines:
The listbox should not be linked, neither should the subform.
Create a procedure
Sub UpdateSubform()
subform![id]=listbox!changetableID]
End sub
In the OnLoad event of the form and in the OnChange event of the listbox call this procedure
In the afterUpdate event of the subform:
Private Sub Form_AfterUpdate()
me.parent.requery
End Sub
I didn't check this out myself, because I am away from my access - but it should work along those lines.
If thats not what you are looking for, please edit your post and at least post the information whats in the listbox and where your subform should link to.
I use a subform to show the result of a query, but at the end of record there is a *(New) for adding new records. I don't want the user to be able to add new records through this subform. How can I get rid of this?
With the subform in Design View, open its property sheet. Then select the Data tab on the property sheet, find the property named "Allow Additions" and set it to No.
The "Allow Additions" property will not display if you open the design view of the parent form and click on the subform. The subform must be opened in Design View independent of the parent form.
Had exactly the same problem.
My DB is to keep track of basketball box scores. Every new main form created a new blank subform to enter the quarter scores into. The problem was as I Entered through the fields once I hit enter on the last quarter score value a new record was created for the table that Quarter Scores was based on.
I could not use Allow Additions = no. If I did not allow additions there was no quarter score input created when a new main form (for a new game) was created.
I used the code below for a key down event of the Enter key to set the focus on another subform before a new quarter score record was created.The commented lines were to help trouble shoot when creating the code. Key code 13 is the Enter key.
Hope this helps someone, took a while to get this right.
Den
Private Sub HOT2_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode <> 13 Then Exit Sub
'MsgBox "Enter Pressed"
KeyCode = 0
'MsgBox "KeyCode=0"
Forms!FRM_BoxScores.Scrimmage.SetFocus
Forms!FRM_BoxScores!subform_qryReturnVisitingPlayers_BosScores.Form!subform_tblPlayerPoints_BoxScores.Form!PlayerPoints.SetFocus
End Sub
Change Record-set Type to Screenshot in Data Form Properties > Data Tab. Please note user cannot change any Data in form you once you change this
in your subform design grid, open the properties.
Where is says recordset type, set it to snapshot. That will remove that line.
The recordset will NOT be updateable at that time. So if you want to edit records, you have to change it back
Is there a way to make Access write values immediately to a table when changing values in a form? Right now, I have to change a value in the form, and then click off the field onto the subform before the value is written to the corresponding table.
Sounds like no big deal, but certain controls require current data to give valid options. If the user doesn't know enough to click into the subform then the data they view could be out-of-date.
The behavior in your requirement are asking for is by default how access functions. In most typical scenarios your master form is going to be the parent table, and as a result before you can add child records to such a relational setup, then the parent record would have to be written and saved to disk.
And the reverse while not a requirement is also the default operation for access.
In other words it's not clear why when the focus moves from your parent form to a sub form, that the parent form's record is not been written to the table.
So something in your setup is incorrect here, or you'll have to expand on the behavior you're witnessing.
By default changing the focus from the main form to a sub form will cause the main form record to be written to the table, and changing the focus from a sub form tool main form as a general rule should also cause a sub form record to be written to the table.
If you are matching a combo box, you can use that as your link master field, that is, the name of the control itself. Your subform should update as soon as you select a ticket with the combo.
Link Master Fields: Combo1
Link Child Fields: Field1
You can force the save of the record when a user moves off a field in the Lost_Focus event.
Private Sub MyField_LostFocus()
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
End Sub
I was also facing the same problem, but in another scenario. I am not having sub-form, but a datasheet view of the same table on a split screen, which was not updating immediately after save button. On pressing DUPLICATE button, instead of getting duplicate data of latest record, I was getting pointer shifted to first record, and then the data from the first record was getting duplicated.
By inserting
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
before data duplicating code, I am now getting the data updated immediately with record pointer stick to the current record, and the same last record gets duplicated as required.
Sharing the experience.
Thanks ChrisPadgham
Just use this in your form model:
Private Sub {your textbox name her}_AfterUpdate()
Me.Refresh
End Sub
Essentially: in the AfterUpdate event of every desired control execute a acCmdSaveRecord command.
So:
In the design view of the form click on your control.
Menu > Design > Property Sheet (to bring up the property sheet for your control)
Property Sheet > Event [tab] > After Update. Choose "[Event Procedure]". Click on the elipsis "..." to take you through to the Code-Behind-Form VBA dev environment.
Supply your code as follows ...
Private Sub [Control Name]_AfterUpdate()
DoCmd.RunCommand acCmdSaveRecord
End Sub
E.g.
Private Sub cboFinancialYear_AfterUpdate()
DoCmd.RunCommand acCmdSaveRecord
End Sub
Repeat the above for every relevant control on the form.
The acCmdSaveRecord avoids an unecessary visual refresh of the current form, as when using Me.Refresh (which #medjahed mohamed has suggested).
I have only one table and I would like to create a form for the users to populate the table easily. There are at least 5 fields in the table that only needs to be completed if a certain type of inspection (Fire) is selected from a drop down list and is left blank otherwise.
I would like to create a subform that will only pop up when inspection type "Fire" is selected from the drop down list in the main form. How can I do this?
I used the wizard to create the form and I am stuck because I really don't know VBA. So far, I went to the inspection type field on the form, clicked on "Properties", then clicked on "After Update", then selected the Macro that I created to open the subform when the inspection type = "Fire", but it isn't working.
What happens is the subform gets opened up no matter what type of inspection I select, then the ID number on the subform doesn't correspond to the main form (the subform ID will remain on id#1). Also, when I do input data in the subform, the information ends in the next record.
I was wondering if that is happening because I am using both the form and the subform to enter data into the same table. I hope this is a clear explanation of what I want to do.
Just to try to improve a bit on Kovags excellent proposition:
Sub InspectionType_AfterUpdate
Subform1.Visible=(InspectionType.Text="Fire")
End Sub
Sub Form_Current
InspectionType_AfterUpdate
End Sub
Just change this code to adapt your needs and add it to the Change Event of the Inspection Type textbox:
Sub InspectionType_Change
if InspectionType.Text="Fire" Then
Subform1.Visible=True
else
Subform1.Visible=False
endif
End Sub
If the fields are on the same table then I'd suggest placing the fields on the subform onto the main form. Then making them visible or not depending on the state of the combo box.
Using a subform gets a bit more complex as, among other things, you will need to save the record in the main form before opening the subform. Otherwise the subform won't be able to update the record and will give you a locking message.