UserForm not requery - vba

I have a form, that is bound to a query. There are sveral condition on filtering the record and one of them is list box where I can select conrete values. I have a macro, that insert IN() statement into the SQL with selected values and the query results working properly and is showing the correct records.. Never the less when I use form.requery it still shows all of the items and it is not showing the results from query). When I change other filters (for example in combobox) then the form requery correctly although both of the changes launch the same macro...

I finally found a solution :) I Just put Me.RecordSource = Me.RecordSource :)

If your recordset is bound to the form just use
Me.Requery
If it is on your form within a form (subform) then use
Me!SubFormName.Form.Requery
If your form is not bound to a recordset/query you need to set the SQL string again.

Related

Changing Query Criteria doesnt change query results viewed on form

I have a simple database I am developing in MS Access 2010 where I have a form with a "view specific" command button and a "view all" command button. Both buttons will run a query: the specific one should apply a criteria to one of the fields, and the all one should run the query with no criteria, or with a * criteria in that field. There is another field that requires criteria for both queries, so essentially the specific button should run a query with two criteria and the all button should run a query with one criteria (or one and an * criteria). The query is a stored query I created via Design View. The "all" SQL statement is something to the effect of:
SELECT tableA.field1, tableA.field2, tableB.field1, tableB.field2
FROM tableA INNER JOIN tableB ON tableA.field3=tableB.field2
WHERE tableB.field1=True;
I want the specific query to run based off of an option chosen in a combo box on the form, so it's SQL statement should look the same, except the WHERE clause would change to:
WHERE tableB.field1=True AND tableB.field2=Forms!ViewQryResults!comboSelectSpecific;
Problem
My problem is that the specific query, I'll call it Query2, works as intended, but the all query, Query1, doesn't change the results viewed on the form. Neither query throws an error message, and it seems that the query is running again as the record total at the bottom of the screen goes away, and then comes back after the query finishes and gets a total number of records again.
Attempted Solutions
I have tried multiple different solutions to attempt to get it to work, none of which have.
I first tried to establish a parameter, but then Access prompts the user for the parameter value as soon as the form opens, which I do not want. If I simply declare the parameter using the parameter button in the ribbon in design view for the query, but do not assign it to a field as a criterion in the design grid, Access no longer prompts me for the parameter value, and I can assign its value via VBA, but then I do not know how to assign the parameter to a field criteria using VBA. My attempts throw errors that say Access doesn't recognize the name of my parameter.
I then tried altering the SQL string for the query and then requerying. I used text manipulation functions to find the WHERE clause, and replace it with a WHERE clause that has both criteria, then requeryed. To determine the proper context for the SQL string, I changed the query in Design View to do what I wanted, and then switched to SQL View and copied the SQL string that Access created to represent the query.
I then inserted a Debug.Print line to print the SQL string at each step, and compared them character by character and they match exactly, meaning the text manipulation is doing what it should be doing and creating the SQL strings correctly, or at least as Access would if I wasn't messing with them at all. I tried this from both directions, i.e. I made the stored query in the way it should be for one button, and then used code to change it for the other button, and then swapped, but neither method was successful in getting both queries to work. The basic code I tried for manipulating the SQL string is as follows:
Private Sub Query2_OnClick()
Dim d As Database
Dim q As Query
Dim strSQL As String
Dim strSQLSelectFrom As String
Dim strSQLWhereNew As String
Set d = CurrentDb
Set q = d.QueryDefs("Query2")
strSQL = q.SQL 'Saves original SQL statement
'Text Manipulations to create strSQLSelectFrom as just the SELECT and FROM portions
strSQLWhereNew = "tableB.field1=True AND tableB.field2=Forms!ViewQryResults!comboSelectSpecific;"
q.SQL = strSQLSelectFrom & strSQLWhereNew
DoCmd.Requery
Me.Refresh
Debug.Print q.SQL 'Verify text manipulations created SQL String I expect
q.SQL = strSQL 'Returns SQL to original statement
Debug.Print q.SQL 'Verify SQL statement properly returned to original statement
End Sub
This arrangement works to allow me to query specifics based on my combo box. When I click the other button, whose code just does a requery because the first button should reset SQL to the way it was, the results do not change. The query appears to run as indicated by the total records counter going away and coming back, but it never changes the results. If I go back and choose another option from my combo box, and query specific, the results update to the new specific selection.
Again, if I click the All Query button, the results remain based on the combo box selection. If I change the combo box selection, and then click All Query (which shouldn't care at all about the combo box because it isn't listed anywhere in the code for that button), the results change to the new selection, like both buttons are running specific queries. That part is the most puzzling, I can figure out why it is doing that.
I know a simple solution would be just to create a second query and stop trying to change one back and forth, but I really feel I am missing a fundamental piece that I am going to need to understand at some point, so I am really looking for that fundamental piece that will make this work.
IIUC - Simply change the form's RecordSource to point to needed saved query with .Requery:
Private Sub ViewAll_OnClick()
Me.Form.RecordSource = "Query1"
Me.Requery
End Sub
Private Sub ViewSpecific_OnClick()
Me.Form.RecordSource = "Query2"
Me.Requery
End Sub
Alternatively, you can keep the same query recordsource and filter form using DoCmd.ApplyFilter, assuming criteria controls are on same form requiring filtering. Any valid SQL WHERE clause can be used in second argument and even dynamically created.
DoCmd.ApplyFilter , "field1 = True AND field2 = '" & Forms!ViewQryResults!comboSelectSpecific & "'"
And to remove filter:
DoCmd.RunCommand acCmdRemoveAllFilters

Access 2013 Form Requery Issue

I have a form that has a parameter query for it's record source - the parameter query asks for an ID number, then the form displays that particular record.
Within the form are a number of buttons that perform various functions (change status etc...) and then requery the form.
My problem is that every requery obviously re-runs the parameter query and asks for the ID number - how can I make the requery use the current ID number and not ask for it?
The button functions are quite simple -
Me.SRStatus = 3
Me.Requery
Regards,
Garry.
Me.Requery will execute the query again, so it will ask for parameters always.
Consider using Me.Refresh instead of Me.Requery
It depends on what you need. You open your form filtered by ID, that's perfect. Then the user makes changes, and here comes the deal.
If those changes are just editing the record, then you don't need to use Me.Requery. Refreshin will update the record and show the changes without calling the query again.
But refreshing won't update if the record is deleted or if you add new records. To show new records/update deleted record/reorder records you are forced to Requery.
If that form is just for editing an existing record, then use Me.Requery so the existing data will be updated without executing the query again (and then asking for a parameter again).
You can read more about this
here
UPDATED ANSWER: OP said that Refresh works, but in event Form_Current got code that enables/disables the buttons according to a field value. So the right answer would be:
Me.Refresh
Call Form_Current

Combobox filter on a query not working while Null (in the form only)

I am currently working on a project on MS-Access 2010 and I am struggling with a issue for hours. I would like to apply a filter on a SELECT/PIVOT statement. The result of that query is displayed in a ListBox.
The issue is that no results are displayed when the Combobox is set to Null. But when I select specific values in that Combobox it works perfectly.
The Cbbox filter is declared as a Parameter
My query WHERE clause looks like this :
WHERE (Jobs.fk_group=[Formulaires]![frm_MAIN]![lst_filterGroup] AND (fk_otherCritera='XXX'))
OR ((([Formulaires]![frm_MAIN]![lst_filterGroup]) Is Null) AND (fk_otherCritera='XXX'))
The query WORKS while I enter manually the value of the parameter (=when I enter an empty string, it displays all the records = what I want)
Idk if it is important, but also the listview that I use swap dynamically its recordsource (=it runs 2 differents queries), depending from another Cbbox
I checked the parameters values into my VBA code just before MyListview.Requery calls and IsNull(myCbboxReference) returns True and my other criteria is also OK.
I have no clue of what I did wrong, I need help :-(
Best regards, LR
I would recommend to use special value in combobox for displaying all records in main table and don't rely on comparing with Null values. Your query, probably, doesn't work because empty combobox returns "" , not Null.
Also be careful with queries based on references to controls/parameters. Access has a bug: if you apply filter on data in the form, based on query with such kind references, it stops read new values from controls/parameter during Requery. It appears at least for subforms in Datasheet mode. Workaround for this bug - using function instead of reference.

MSAccess: Change in subform recordsource causes loss of LinkChildFields binding

I have a problem that Im completely stumped by. Or rather, I have a work-around that is messy and I don't understand why what I want to do doesn't work:
I have a mainform / subform nest. The subform is linked by master/child fields (neither form is editable- so Im using the recordsource field directly rather than a separate control). The subform is a continuous form; so the subform displays the records related to the master in the mainform when the current record changes. The subform is based on a query- there are no parameters or filters being applied- the query presents the recordset exactly as needed. This all works perfectly (in that when the record of the mainform changes, so to do the related subform records).
Problem arises when I want the subform to be based on a different a different query (ie based on different tables)- but with the same unique-key (the 'ID') and fields as the original query. The queries all work fine, and displaying them in the form works fine.
The issue arises when I change the recordsource of the subform (so from QueryA to QueryB). The records the subform are correct (ie the query results are displayed as expected)- but the LinkChildFields is no longer functioning: the subform no longer shows the related records from the query- it displays them all. In other words, the form displays the entire recordset as if I had no LinkChild / LinkMaster set.
So, the code in the AfterUpdate event of the toggle control in the MainForm is ;
Private Sub optActRep_AfterUpdate()
Select Case Me.optActRep
Case 1
MainFormSubFormControl.Form.RecordSource = "QueryA"
Case 2
MainFormSubFormControl.Form.RecordSource = "QueryB"
End Select
Ive debugged and the property 'linkchldfields' is still set correctly ('ID'). Ive tried setting these to vbNullString before the recordsource update and resetting after the recordsource update (same issue). The odd thing is if I update the linkchildfield property in the mainform (in the Current Event)- then it works and seems to fire. So,
Debug.print Me!Subform.LinkChildFields
correctly returns 'ID'. The only way I can make it fire is by coding;
Me!Subform.LinkChildFields = "ID"
into the Current event of the Mainform (ie so that every time the MainForm record changes, the correct QueryB record is displayed)... even though Im only setting the property to what my debug is telling me it is already set to. Its as if it no longer fires.
Very confused.
Access continues to do the daftest things.
Nothing to do with the form setup at all. The underlying query (QueryB) was based on a Crosstab query. Whilst the query worked fine independently of the form (including when directly using the query as the forms record source), it obviously didnt like using the product of one of the fields as the Unique key (even though it was the same format type and Grouped / Row rather than being a cross-tab field).
I changed the crosstab part of the query to a Grouped query (using IIF & Sum to manually create the Crosstab-equivalent field values) and the form now works fine.
My lesson learnt- don't use Crosstab queries! Apologies for the red-herring.
Surferosa
Apologies for the late reply, I only found this thread when I encountered problems similar to yours. I'm not sure the Crosstab query is the cause of your problem.
I can set LinkMasterFields to be either a column name or a control name.
I can only set LinkChildFields to be a column name, setting it to a control name does NOT work. Ouch.
You can change LinkChildFields in the sub-form control either in Design View or via VBA, but in many cases Access continues to use the previous value even though it displays the new value. This is true for sub-forms whose record source is either a table or a simple two table query. I do not have the time or the patience to characterise this problem any further. Sorry !
My solution is to set LinkChildFields in the sub-form control to a value e.g 'ID' and then create an alias with that name in each and every record source used by my different sub-forms. Then you only need to change the Source Object when you switch sub-forms.
Hope this helps !
HB

Microsoft Access Subform won't populate

I have some code to populate a single subform on a main form with a recordset. The problem is that the records are not shown on the subform. If I open the subform as a form on it's own then with almost the same code it works perfectly:
Forms!frmCorrespondanceHolidays.RecordSource = strsql ' WORKS FINE
Me.frmCorrespondanceHolidays.Form.RecordSource = strsql 'SHOWS NO RECORDS
I have tested that if I just paste the value of strSQL into a query the correct records are returned.
I am beginning to think it is a bug in Access. I have tried recreating the database by creating a new one and importing all the objects. I have also compact & repaired. Not sure what else to try!
The name of a subform is different than the Source name of the sub report. Here is where you need to look for the name to reference in VBA. You select the subform once when the Parent form is in Design view and the look at the properties.
After further investigation I have found it is related to the sql rather than the subform. If the sql includes a query that doesn't reference the parent form records are shown. If the sql includes a query that does reference the form then no record are shown. Guess I will have to work around this limitation. Thanks for the pointers.