Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I find the maximum value using code

Status
Not open for further replies.

matrec

Technical User
Feb 19, 2005
19
CY
I need to find the maximum value of field IDPost (integer) and then icrease this value by 1, using code. I then declare this value as maxIDPost1 and use it on a form.
qryFindMaxIDPost is the query that gives all the numbers from where I will select the one that has the maximum value.
This is what I wrote:
Dim dbMax As Database, rsMax As Recordset
Dim sqlMax As String
Dim maxIDPost, maxIDPost1 As Integer
Set dbMax = CurrentDb
sqlMax = "SELECT IDPost " & _
"from qryFindMaxIDPost "
Set rsMax = dbMax.OpenRecordset(sqlMax, dbOpenSnapshot)
If Not rsMax.EOF Then
rsMax!IDPost = maxIDPost
maxIDPost1 = maxIDPost + 1
Me.maxIDPost1 = maxIDPost1
End If
What I don't know is where to put a line instructing the selection of the maximum value.

I would appreciate any help this question.
 
Use the DMax() function instead:
[tt]
=DMax("[IDPost]", "tblPosts") + 1[/tt]

VBSlammer
redinvader3walking.gif

"You just have to know which screws to turn." - Professor Bob
 
Yes, I agree implicitly with VBslammer, but just for the sake of it, I'm going to offer this...

"SELECT IDPost from qryFindMaxIDPost ORDER BY IDPost DESC"

rec.MoveFirst

but again, why open a recordset if not necessary, not too mention the lines of code needed.

Good luck either way!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top