Can't get requery to work from form/subform - ms-access

Background:
I am trying to add a new form to an old Access database that was created with Access 2003. Previous forms were based on tables, but the fields for the new form are based on a query.
I have a form called MasterList with subform MasterList_Sub. MasterList contains textboxes that I want to use to filter MasterList_Sub, along with a search and clear button. MasterList_Sub is displayed beneath.
I modified the VBA for the search button from the other forms, but it does not seem to work. Here is the code from a working form:
Private Sub SEARCH_Click()
Forms!mrtgref!Mrtgref_sub.Requery
End Sub
My code is simply
Private Sub SEARCH_Click()
Forms!MasterList!MasterList_Sub.Requery
End Sub
When I press the search button something appears to happen, but the subform does not update. I'm new to programming for Access, but based on what I've read this code looks too simple, like I'm missing something. I can't find any other VBA modules in the DB, and the fields are set up similarly to the other DBs. Any ideas on how to proceed from here?
Also, I've tried some other syntax, and when it doesn't work I get an error that starts with "Mortgagee Inquiry can't find..." Where is it getting the name Mortgagee Inquiry from?

MasterList_Sub is a subform control. .Requery is a method of the Form itself therefore you need to add .Form before .Requery.
The structure:
Forms!MainForm!SubformControl.Form.Requery
In your case:
Forms!MasterList!MasterList_Sub.Form.Requery

Related

Error 3251 when setting record source for subform

I've been having this error where I am unable to set the record source for a subform.
Just a bit of background, the form in question is structured as such: frm1View is my unbound main form which contains 2 subforms, subfrm1Particulars and subfrm1Datasheet. The issue is with subfrm1Particulars, which itself contains 2 subforms in a tab control.
I am attempting to change the recordsource of my subform in subfrm1Particulars dynamically, based on the records found in the other subform (one sub form displays courses completed and the other displays courses scheduled; the idea is to remove scheduled courses they have already completed from that sub form view).
I am using the following code to do this dynamic recordsource change. The code is contained in subfrm1Particulars(which contains subfrmScheduledCourses)
strSQL = *blah blah blah where etc etc not like etc*
Me.subfrmScheduledCourses.Form.Recordsource= strSQL
I've checked numerous times, my subform control name is correct (although my subform shares the same name as the control, so maybe that's the issue). I have almost the exact same form configuration in another form, with the same code (i.e. the same recordsource and SQL statements and method of assigning recordsource) and it works perfectly, so I can't tell why this isn't working.
It gives me an error 3251. I'm running this in the On Enter event of the subform control (in both of my forms where this particular code appears).
Any help at all is much appreciated!!
Referring to subform objects can be tricky, especially from other subforms. Try calling the object differently:
Forms!frm1View!subfrm1Particulars.Form!subfrmScheduledCourses.Form.RecordSource
What this does it begins referring to form objects by navigating from mainform to subform to the desired area.

VBA Access Form Binding Issue

I have a Form in MS Access 2010. The form is bound to a record source (just a simple table). I am trying to use code like the following :
Change a field in the table (the current record) to another value (error is the field is not defined in VBA which causes it to not change in the table)
AFieldInTheTable = Value
Go to another record in the table (error is nothing happens at all)
DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
Is there some option I am not setting because I tried building the form using the built in wizard and from a blank form but still no difference.
It is like Access doesn't know that the form is bound to the table. But I know that it is. So frustrating.
You cannot access / alter properties of a form using public functions which were called by other forms.
Only during events of the form can you perform actions with that form.

need to refresh subform based list box selection in access 2007

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.

MS Access - Write to Table Immediately After Changing Value in Form

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).

Forms and Subforms in Access 2003

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.