Ms Access: Running an update query - ms-access

I got a query which takes out some fields from a table.
How do I use the same query to create a sum of the values of those fields and update the sum to a single field in another table?

If you must do this, the only way I can see to accomplish without VBA uses DSum() domain aggregate function.
UPDATE tablename SET fieldname = DSum("some field", "some table", "some filter criteria here")
Trying to do this with a nested aggregate query in place of the DSum() returns error 'must be an updatable query'.
Really should re-think your db design.

Related

Update Table Column off of Query Column

I am trying to Update a column in my table Inputcounts called concatenate off of a query called InputConcatenates that has a column also called concatenate. I am running an update query with the field name as concatenate the table name as InputCounts and the update to field as [InputConcatenates].[Concatenate]. But every time I run the query it pulls back that 0 records will be updated. Is my syntax wrong possibly?
Update Query SQL:
UPDATE InputCounts INNER JOIN InputConcatenate
ON InputCounts.CONCATENATE = InputConcatenate.CONCATENATE
SET InputCounts.CONCATENATE = [InputConcatenate].[CONCATENATE];
InputConcatenate Query SQL:
SELECT InputCounts.FLEET, InputCounts.AMMs, [FLEET] & [AMMs] AS CONCATENATE
FROM InputCounts;
You reported this query accomplishes what you want ...
UPDATE InputCounts
SET CONCATENATE = [FLEET] & [AMMs]
WHERE CONCATENATE Is Null;
That may be fine. However CONCATENATE is not updated until you execute the UPDATE, and does not get updated (after having previously received a value) in response to changes in FLEET or AMMs
Decide whether CONCATENATE really needs to exist as a field in your table. You could use a query to derive it whenever you need it:
SELECT *, FLEET] & [AMMs] AS CONCATENATE
FROM InputCounts;
With the query, CONCATENATE will always be up to date.
If your database is ACCDB format and your Access version is >= 2010, another possibility is to make CONCATENATE a "calculated field" type in the table's design:
If you prefer CONCATENATE be Null whenever FLEET or AMMs is Null, change the field's Expression property to [FLEET] + [AMMs]
The advantage of a calculated field is that Access automagically updates its value without further effort (like executing an UPDATE) from you.
A disadvantage is that you can't index a calculated field. That means it's not suited for joins, WHERE criteria, ORDER BY, etc. You'll have to decide whether it's a reasonable fit for your application. :-)

How do I set up a calendar in MS Access

I am trying to set up a calendar in access where I can put in Customer information for specific dates. I must be doing something wrong, please help.
The table list that I would like to link has these headers
ID :Customer : Date: Status: Hours
example:
2: Tax Services: 07-Mar-14 :completed: 11-12
5: Discount Tire: 25-Mar-14: Call
In order for a data bound Form to be editable, the form must be either based on the table or on a query that is updatable.
A query is not updatable if it contains a DISTINCT clause (Unique Records property set to Yes) or if it is a UNION query or has a GROUP BY clause. Cross-table queries are not updatable. SQL Pass-Through queries are not updatable. A query joining tables containing a many-to-one-to-many relationship is not. A query containing aggregate functions (COUNT, SUM, MAX etc.). A query containing a sub-select in the field list.
The Form must have the AllowEdits property set to Yes. If you want to be able to add or remove records, also set the AllowAdditions and AllowDeletions properties to Yes.
Set DataEntry to No as this means that a new empty record is created each time you open the form.

Assign form field value to all records in subform

I'm pretty new to Access, so this may be very easy or obvious to some, or maybe it can't be done at all. I have searched this site, but I may not even be using the proper keywords. So far, I haven't found anything that helps me do what I am trying to do.
I have a form with a continuous subform in it. I have an unbound txt field on the main form called txtPO_num. In this form I run a query that displays all records that meet specified criteria in the continuous subform. I also have a control in the subform called PO_Num. My question is, how do I get the main form field value in txtPO_num to populate the PO_Num control in only the records displayed in the subform?
If there is another way to accomplish this I would be interested in knowing that too.
Execute an UPDATE statement which targets the same records which are included in the subform.
You have a SELECT query with a WHERE clause which identifies the records included in the subform's recordset. Build an UPDATE statement using the same WHERE clause. For example, if the SELECT were ...
SELECT field1, field2
FROM YourTable
WHERE field2 = 'foo';
... the UPDATE could be ...
UPDATE YourTable
SET field1 = 'new value'
WHERE field2 = 'foo';
Execute the UPDATE statement using the DAO database object's .Execute method.

Query a recordset object using VBA

I'm using Access 2003 and have a form that allows the user to pick from a variety of filters, and I use VBA to update the recordset of a subform based on those filters (I generate a SQL statement in VBA). This subform can have duplicate client ids, and now I'm trying to get to a unique list of client ids.
Is there any easy way to query out the unique client ids using VBA if I have the source SQL for the subform? I've thought of these options:
Write all the ids to a temp table and then query that table (seems
like more work/resources than are necessary)
Somehow apply a query to a recordset object in VBA (is this possible?); I'd set the recordset object equal to the SQL query and then try to run a SELECT DISTINCT client_id FROM <the recordset object>, but I'm not finding any information that leads me to believe this is possible
Generate a new SQL query based on the original one (I was hoping SELECT DISTINCT client_id FROM ('original select query text here') would work, but it gave me a syntax error in the FROM statement
Aim for the third alternative. This should work if you alias the subquery, and 'original select query text here' can fit.
SELECT DISTINCT sub.client_id
FROM
(
'original select query text here'
) AS sub
If Access still chokes, show us what you have for 'original select query text here'.
You're right about alternative #1 ... that is wasteful.
Alternative #2 is not possible because Access won't let you run a query using a recordset object as the FROM source.

Define a field as a parameter in a Query

I have a field in a table(1) that can be filled with two different fields from another table(2). Both of the tables are linked through their Primary Key. How do I create a Update Query that uses a parameter to define which field will be used at the update?
In this example, tblSource has 2 date fields, date1 and date2. The parameter WhichDate is used with the Switch() function to determine which of those 2 fields is used to update date_assigned in tblTarget.
PARAMETERS WhichDate Long;
UPDATE tblTarget AS t INNER JOIN tblSource AS s ON t.id = s.id
SET t.date_assigned = Switch(WhichDate=1,date1,WhichDate=2,date2);
I don't think you get to do this directly in MS Access. You could mimic this behavior if you are using VBA code, but my best (possibly incomplete) understanding is that you won't be able to use a Parameter to reference a Database object.
With VBA (or any other client code) you could set up a method which accepts a method parameter that establishes which column to update, and then assemble the appropriate SQL statement from there.