Link to home
Start Free TrialLog in
Avatar of esbyrt
esbyrtFlag for Canada

asked on

Calculated field needs to be stored in table

I know it's bad form to store a calculated field in a table but here's the situation.  The calculated field is for lab sample numbering. Due to very small labels the number has to be kept as short as possible so I created a combined number using the year followed by four digits like so 13-0001.  This works fine in the query but I also need to store this number to be used / joined to another table for the results of the tests performed on the lab sample.  How can I store the sample number in the underlying table or is there a way to do this numbering right in the table?  Thanks!
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

you can use an update query to update the field in the table

update tablex
set FieldName=<claculated values>
Assuming the calculated number is in a text box on a Form ... you can use the Form Before Update event like so:

Private Sub Form_BeforeUpdate (Cancel As Integer)
    Me![YourTableFieldName] = Me.YourTextBoxName
End Sub

mx
Avatar of esbyrt

ASKER

Capricorn1 can I set the update query to run after each time a record is updated?  This needs to be done ongoing not just once to update the table.

DatabaseMX the calculated field is in a query and the form is based on the query.  So the table isn't directly involved ( I think)  Would the code you posted still work?
you can set the control (textbox) control source to the calculated field of the table

post the query, how is the calculation done?
Avatar of esbyrt

ASKER

This calculated field is in the query
Expr1: make_my_id(Now(),[id])

Here's the make_my_id function

Public Function make_my_id(indate As Date, inID As Integer) As String

Dim myStr As String
Select Case Len(CStr(inID))
Case 1
myStr = "000" & CStr(inID)
Case 2
myStr = "00" & CStr(inID)
Case 3
mySte = "0" & CStr(inID)
Case 4
myStr = CStr(inID)


Case Else
myStr = Right(Trim(CStr(inID)), 4)
End Select

make_my_id = Format(indate, "YY") & myStr

End Function
place this codes in the current event of the form

me.textboxName=make_my_id(Now(),[id])
Avatar of esbyrt

ASKER

I get a Microsoft cannot find the object 'me.passIDtoTable=make_my_id(Now(),[id]) error when I add that to the current event.  passIDtoTable is the name of the text box.
if the message says that it cannot find the object "me.passIDtoTable" then the name of the textbox must be something else
Avatar of esbyrt

ASKER

How is that going to pass the data to the table anyway?  That looks like it will do the calculation in the form but how does that store in the table?
as iv have stated above, set the control source of the textbox to the field of the table.
Avatar of esbyrt

ASKER

Hm there must be a syntax difference somewhere.  I tried it again exactly as you'd typed it and added it to the current event of the form and I get Access cannot find the object 'me.'  I am using Access 2010 if that makes a difference.
thanks for your help!
upload your db
Avatar of esbyrt

ASKER

Okay I have uploaded the db.  The query the form is based on is qrySamples.  The form is frmSamplesQuery.  The sample ID is currently bound to Expr1 from the query and a text field beside it is the passIDtoTable field I was testing with.  I've probably changed things again since my last answer so just go ahead and test your code as you see fit.  What I want to do is pass the Sample ID value to the SampleID field in the tblSamples table.  Thanks again!
NBDCv1.accdb
Avatar of esbyrt

ASKER

I'm signing off for the night, will check back tomorrow.
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of esbyrt

ASKER

That works beautifully!  Thanks so much!