Solved

Calculated field needs to be stored in table

Posted on 2013-01-17
16
447 Views
Last Modified: 2013-01-18
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!
0
Comment
Question by:esbyrt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
16 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 38790177
you can use an update query to update the field in the table

update tablex
set FieldName=<claculated values>
0
 
LVL 75
ID: 38790178
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
0
 

Author Comment

by:esbyrt
ID: 38790222
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?
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 38790234
you can set the control (textbox) control source to the calculated field of the table

post the query, how is the calculation done?
0
 

Author Comment

by:esbyrt
ID: 38790248
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
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 38790267
place this codes in the current event of the form

me.textboxName=make_my_id(Now(),[id])
0
 

Author Comment

by:esbyrt
ID: 38790307
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.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 38790324
if the message says that it cannot find the object "me.passIDtoTable" then the name of the textbox must be something else
0
 

Author Comment

by:esbyrt
ID: 38790440
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?
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 38790442
as iv have stated above, set the control source of the textbox to the field of the table.
0
 

Author Comment

by:esbyrt
ID: 38790508
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!
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 38790519
upload your db
0
 

Author Comment

by:esbyrt
ID: 38791066
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
0
 

Author Comment

by:esbyrt
ID: 38791088
I'm signing off for the night, will check back tomorrow.
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 38791580
check this

click on each record to generate the SampleID
NBDCv1Rev.accdb
0
 

Author Closing Comment

by:esbyrt
ID: 38794544
That works beautifully!  Thanks so much!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question