function error (1 Viewer)

boycie

Registered User.
Local time
Today, 05:56
Joined
Sep 19, 2001
Messages
45
Hi,

I have written the following function;

Private Function GetMaxValue(ByRef strTable As String) As Long

'gets the number of records from table which will allow us to determine how many records to add

Dim db As DAO.database
Dim rs1 As DAO.Recordset


Set db = CurrentDb()

Set rs1 = db.OpenRecordset("SELECT Max(" & strTable & ".ID) AS MaxOfID FROM" & " " & strTable & ";")

rs1.MoveFirst

GetMaxValue = rs1![MaxOfID]

MsgBox GetMaxValue

rs1.Close

Set rs1 = Nothing
Set db = Nothing

End Function

when I call the function and pass it a table name as follows;

Private Sub Command2_Click()

GetMaxValue ("t_2")

MsgBox GetMaxValue

End Sub

I get the error; 'argument not optional'. Can anyone see the problem?
It works though if the msgbox is within the function

thanks
 

boblarson

Smeghead
Local time
Yesterday, 21:56
Joined
Jan 12, 2001
Messages
32,059
You have to combine these two lines:

GetMaxValue ("t_2")

MsgBox GetMaxValue


So

MsgBox GetMaxValue("t_2")
 

boblarson

Smeghead
Local time
Yesterday, 21:56
Joined
Jan 12, 2001
Messages
32,059
And if you want to use it later on, you assign it to a variable:

Dim lngValue As Long

lngValue = GetMaxValue("t_2")

MsgBox lngValue
 

boycie

Registered User.
Local time
Today, 05:56
Joined
Sep 19, 2001
Messages
45
thanks Bob, i tried everything except that
 

Users who are viewing this thread

Top Bottom