2

Basically I need to find the maximum value in a given dataset and record that value to apply it to another record.

I tried using the SQL "SELECT MAX(SCID) FROM [Stock Conversion];" but this just input this piece of SQL into the Field on the table.

Now i have created a query to find that value, is there a way to reference the result of the query in the code to achieive this?

Here is the code for context:

Private Sub listResult_DblClick(Cancel As Integer)

Dim sc1 As DAO.Recordset
Set sc1 = CurrentDb.OpenRecordset("Stock Conversion Items", dbOpenDynaset)
Dim strSource3 As String

strSource3 = DONT KNOW

sc1.AddNew
sc1.Fields("[SCID]").Value = strSource3
sc1.Fields("[Result PC]").Value = Me.listResult.Column(0)
sc1.Fields("[Status]").Value = "NEW"
sc1.Update

End Sub

Thanks in advance, Bob P

1
  • Did you try to open the recordset and assign the max value to a variable by asisgning: sc1(0) ? (Max returns just 1 record). Commented Oct 1, 2012 at 13:48

2 Answers 2

2

With MS Access, you can use domain aggregate functions:

strSource3 = DMax("MyField","MyTable")

If you wanted to use SQL, then:

Dim rs As Recordset

Set rs = CurrentDB.Openrecordset( _
   "SELECT MAX(SCID) As MaxSCID FROM [Stock Conversion];")
strSource3 = rs!MaxSCID

Be very careful with max values in a multi-user environment. They could change due to another user inputting data.

From chat

It appears that it may be possible to get the required ID from previous code using the LasModified property. This is far safer in a multi-user environment.

Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("Table1", dbOpenDynaset)

rs.AddNew
rs!AField = "abc"
rs.Update 
rs.Bookmark = rs.LastModified 
strSource3 = rs!SCID
Sign up to request clarification or add additional context in comments.

10 Comments

Hi Remou, is there another way of doing this without the max value?
Hi Bob. I need some background. Is SCID an autonumber? What does Max(SCID) mean? Why do you need it?
I use it to link Stock Order Items with a Stock Order in a one to many relationship (1 stock order has many stock order items) SCID is an autonumber in the Stock Order table, and i'm using MAX to link Stock Order Item records to a Stock Order.
To clarify, when you first start the stock order process, you select the source product and a stock order is created with the source product, then you select the resulting product (stock order items) and they find the right stock order number by selecting the MAX (and therefore newest) number in the stock order table.
"when you first start the stock order process, you select the source product and a stock order is created with the source product" How is the stock order created? Do you run sql/a query against a connection or database object in code? It is at this point that you need to get the SCID using @@identity. You certainly cannot depend on max being the newest if there is even one more person using the system. At the very least, you need a timestamp, and that is not perfect, either.
|
0

try this:

strSource3 = dmax("[field_name]","table_name")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.