How to display a query record count in a form control - ms-access

I have a query that returns a fluid # of records, depending on criteria selected in the form. I would like to display the total # of records returned to the form.
I have added a unbound text field to the footer in the form that is displaying the controls and resulting records. I tried the following expressions in the text field, both of which result in #error:
=Count([qrnname]![fieldtocount])
=DCount([qrnname]![fieldtocount])
This should be simple.

DCount requires string values for its arguments. Assuming fieldtocount is the name of a field returned by the named query qrnname, use this as your text box's Control Source ...
=DCount("[fieldtocount]", "qrnname")
Since that query depends on criteria selected in the form, Requery the text box whenever those criteria change to update the count displayed in the text box.

use this =DCount([fieldtocount]![qrnname])
The syntax for the DCount function is:
DCount ( expression, domain, [criteria] )
expression is the field that you use to count the number of records.
domain is the set of records. This can be a table or a query name.
criteria is optional. It is the WHERE clause to apply to the domain.
Dcount in detail

An other alternative is to use =Count(primaryKey) in the Control Source property
It seems better if you have some filter on your original query, so you don't have to apply them again in the DCount (expression, domain, [criteria]) function.
A quick method for counting Access records in a form

Related

MS Access Expression That Includes Dynamic Field Names

I have a crosstab query which returns results based on consumer demand for a bunch of material numbers. The material numbers become my field names in the crosstab query, and later the values from those fields are displayed in a form.
In the form, I display the value in a textbox. There are a couple of these textboxes where I need to sum the total of two or more values from these fields. Not a big deal it's a simple expression. For example (in the Control Source property): =[H123457] + [H123456].
This works well UNTIL there is no demand for a particular material number. In this case, the field doesn't show up in the crosstab query and I'm left trying to sum two fields where one doesn't exist.
I've tried IIf(IsError([H123456]), 0, [H123456]), Null expressions, Nz function, etc but cannot figure out how to dynamically solve the #Name issue that ends up populating the text box.
Essentially what I want is for a 0 value for the field that doesn't exist, so I can add it to the value where the field DOES exist - is this possible?
Regards!
June7 provided the answer in the allenbrowne.com link. Essentially, you need to add all of the possible field names to the Column Headings property in the crosstab query property window. Then it's a simple matter of adding an Nz() function to handle null values.
Thanks June7!

MS Access uses a form field in query but prompts for parameter

I have a form with two dependent comboboxes (the second loads its values depending on what is selected in the first one). The second combobox uses this query in its RecordSource property
select... where id = [Forms]![MyForm]![myField]
My problem is that I choose the myField in the expression builder and so it allegedly generates the bracketed part correctly, but when I run the form Access doesn't understand it and always prompts for a parameter named with that expression [Forms]!...etc.
Solved as per post comments by Gustav:
Try specifying the field only: select... where id = [myField].

Dlookup on textbox is not working

I have been researching on MS-Access topics around DLookup, but not being lucky on the resolution of my problem.
I have a query that solely returns one value, which is a lumpsum of credits. So I used the clause "AS TOT_CREDIT" on the query to give the unique column a name.
On access report, I learned that you can't directly set the value of a textbox from a query, but also learned the magic is to set the textbox controlsource property to dlookup, like this: dlookup([TOT_CREDIT]; [QUERY THAT CONTAINS TOT_CREDIT]). When I pull the report from access, the textbox still displays the infamous "#Name?", instead of the query value.
Is anything missing here? What else can I do in order to have the textbox display the query result?
Must use quotation marks:
DLookup("[TOT_CREDIT]"; "[QUERY THAT CONTAINS TOT_CREDIT]").

calling a query from a report textbox

I have a report based on a number of different queries. Most of the queries use the value of a row's customerID textbox as a key to extract specific data from other fields.
Here is what I have for the Control Source property of one of the textboxes:
=DLookUp([Level],[qryLevel],[Me].[customerID])
Here is the SQL for qryLevel:
SELECT TOP 1 Level, myDate FROM sometable WHERE custID=Me.customerID ORDER BY myDate DESC
qryLevel works when tested independently, but the DLookUp function does not seem to be working properly because Access gives a dialog box asking for each parameter and then outputs #NAME? in the textbox when no values are input into the dialog boxes.
How can I get each of these textboxes to output its own result from a separate query?
DLookup function arguments must all be strings: http://allenbrowne.com/casu-07.html
So for the first two arguments, just enclose them in double-quotes.
For the last argument, the documentation says it's equivalent to a SQL where clause, without the word 'where'. Actually since you're returning only a single record from your query you probably don't need the last argument at all:
=DLookup("[Level]", "[qryLevel]")
Although, I don't see how qryLevel can work as an independent query since it refers to Me which implies a container object. Better to express as:
SELECT TOP 1 Level, myDate
FROM sometable
WHERE custID = [Forms]![MyForm]![customerID]
ORDER BY myDate DESC
... which will work in any context--inside or outside a form.

Parameter query doesn't return the value of an auto number control

I have a form linked to a table. The form has 4 text boxes: one linked to the autonumber field, and the other three to text fields.
There is also a subform, from which I wish to launch a query (via button and macro) combining results from the subform and a control on the main form. When I specify any of the three text-based controls in a parameter query, this works fine, but asking for the value of the first (autonumber) control results in a symbol being displayed instead of a value.
I wasn't sure what specific information/images would be helpful. Please ask for specific information if you feel it would help.
I've been given the answer elsewhere. I had to implicitly convert the results of the batch field into an int.
INSERT INTO heat_treat_jobs ( card_id, batch ) SELECT atheattreat.id, CInt([Forms]![heat_treat_loads]![batch]) AS Expr1 FROM atheattreat WHERE (((atheattreat.index)=[Forms]![heat_treat_loads]![atheattreat subform].[Form]![index]));