beginners question - subtracting values

  • Thread starter Thread starter DeepBLERG
  • Start date Start date
D

DeepBLERG

Guest
Hi,
This is a real stupid beginners question but I dont have a single clue how to do it.

In MS Access 2000, I want to write an sql query that:
subtracts a value in a text box on an access form from a value in a table where the id equals whats selected in a combo box on the form.

Can anyone help out? Thanks.
 
What about a bit of code inserted in the AFTERUPDATE property of the texbox? The code below creates a recordset based on the table that needs editing and then finds the right record, then does the work. This is used commonly for accessing any table, or query from a form that may not be the form's recordsource.

Dim MyDb As database,MySet As Recordset
Set MDb=DBEngine.WorkSpaces(0).Databases(0)
Set MySet=MyDb.OpenRecordset("MyTable")

MySet.MoveFirst
Do Until MySet.EOF
If MySet![MyField] =Me![MycorrespondingFormField] Then
MySet.Edit
MySet![MyOtherField]=MySet![MyOtherField]-Me![MyValue]

MySet.Update
MySet.MoveNext
Loop

MyDb.Close
MySet.Close



The names used above are -
MyTable = the tablename where the records are stored.
MyField = the fieldname of the field to work on in the talbe.
MyCorrespondingFormField= the matching field on the form.
MyOtherField = the table field to be edited.
MyValue = the subtracted amount or the fieldname of the amount to be subtracted.

You could, to save processing time, exit the routine when the matching field has been edited by adding the lines -

MySet.Close
Exit Sub


after the MySet.Update line
You could also check to see if no matching records were found by creating a flag in the routine which is set if the record is found. When the loop finishes and the flag is not set then you could advise that the matching wasn't found.

I hope this is what you're after.

Best of luck,

Dave Eyley
 
Last edited:
Reference the form fields in the query grid
 
Make sure that the query behind the combo box contains the value you are after - SELECT id,name,value... from TABLE.

then either in code or control source of your result box
= Combobox.COLUMN(2) - TEXTBOX

[Column(2) being the 3rd column of the selected item in your combo query]
 

Users who are viewing this thread

Back
Top Bottom