View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Little Penny[_4_] Little Penny[_4_] is offline
external usenet poster
 
Posts: 13
Default Compile Error using Dmax funtion



Sorry typo




Sub GetMaxKey()

Dim db As Database
Dim rs1 As Recordset
Dim pk As Long

Set db = OpenDatabase("C:\AAAOperatorLog\OperatorLog.mdb")

Set rs1 = db.OpenRecordset("tbl_OperatorLogJobData", dbOpenTable)

'Get max key in from OpLogJobDataID field in
tbl_OperatorLogJobData table
pk = Application.WorksheetFunction.DMax([tbl_OperatorLogJobData],
"OpLogJobDataID", [OpLogJobDataID])

rs1.Close

Set rs1 = Nothing

db.Close
Set db = Nothing

Range("A1").Value = pk
End Sub










On Wed, 09 Dec 2009 08:11:35 -0500, Little Penny
wrote:




Check that you have named ranges called tbl_OperatorLogJobData and
OpLogJobDataID?




tbl_OperatorLogJobData is a table in my access database and
OpLogJobDataID is the primary key field.


My code

Sub GetMaxKey()

Dim db As Database
Dim rs1 As Recordset
Dim pk As Long

Set db = OpenDatabase("C:\AAAOperatorLog\OperatorLog.mdb")

Set rs1 = db.OpenRecordset("tbl_OperatorLogJobData", dbOpenTable)

'Get max key in from OpLogJobDataID table
pk = Application.WorksheetFunction.DMax([tbl_OperatorLogJobData],
"OpLogJobDataID", [OpLogJobDataID])

rs1.Close

Set rs1 = Nothing

db.Close
Set db = Nothing

Range("A1").Value = pk
End Sub









On Wed, 09 Dec 2009 17:29:29 +1300, Rob van Gelder
wrote:


Check that you have named ranges called tbl_OperatorLogJobData and OpLogJobDataID?

Cheers,
Rob



Little Penny wrote:
Thanks for your reply. I tried the need line of code and now I'm
getting

Runtime error 424

Object required

Any idea?


Little Penny












On Tue, 08 Dec 2009 19:37:54 +1300, Rob van Gelder
wrote:

You need to prefix it with WorksheetFunction.

WorksheetFunction.DMax(..., ..., ...)

You'll note that the function requires 3 arguments, not 2 as you've supplied.

For example:
pk = WorksheetFunction.DMax([tbl_OperatorLogJobData], "OpLogJobDataID", [OpLogJobDataID])


Cheers,
Rob


Little Penny wrote:
Can any one help me determine why I'm getting a compile error when
using the dmax function?


Dim pk As Long


The line of code
pk = DMax("[OpLogJobDataID]", "tbl_OperatorLogJobData")



Do I need to install a reference or add a public function?


Any help would be appreciated