functions & object references

Results 1 to 3 of 3

Thread: functions & object references

  1. #1
    Emil Guest

    Default functions & object references

    I want to put the code for opening a database connection and a recordset into functions. The function for opening the database connection works fine the code for opening a recordset works fine outside the functions but when I try to do the assignment:<BR><BR>rs = OpenRecordset(dbc, strSQL, adOpenForwardOnly, adLockReadOnly, adCmdText)<BR><BR>I get the following error:<BR><BR>Microsoft VBScript runtime error &#039;800a01c2&#039; <BR>Wrong number of arguments or invalid property assignment <BR>line 38<BR><BR>Here is the full source code:<BR><BR>&#060;%@ Language=VBScript %&#062;<BR>&#060;!-- #include file="inc/" --&#062;<BR>&#060;%<BR>Dim dbc<BR>Dim rs<BR>Dim strSQL<BR><BR>Function OpenDbConn(ConnectMode, CursorLocation)<BR> Dim dbct<BR> Dim strPath<BR> Dim strConn<BR><BR> &#039;MS Access OLEDB Connection<BR> strPath = "d:dbaccess oomba.mdb" <BR> strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&strPath&";Jet OLEDB:Database Password=toomba;"<BR> <BR> Set dbct = Server.CreateObject("ADODB.Connection")<BR> dbct.Mode = ConnectMode<BR> dbct.CursorLocation = CursorLocation<BR> dbct.ConnectionString = strConn<BR> dbct.Open<BR> <BR> OpenDbConn = dbct<BR> Set dbct = Nothing<BR>End Function<BR><BR>Function OpenRecordset(ByRef dbc, ByVal strSQL, ByVal CursorType, ByVal LockType, ByVal Options)<BR> Dim rst<BR> Set rst = Server.CreateObject("ADODB.Recordset")<BR> rst.Open strSQL, dbc, CursorType, LockType, Options<BR> <BR> OpenRecordset = rst<BR> Set rst = Nothing<BR>End Function<BR><BR>dbc = OpenDbConn(adModeRead, adUseClient)<BR>strSQL = "SELECT tblCategory.Name FROM tblCategory;"<BR><BR>&#039;The line causing the error bellow<BR>rs = OpenRecordset(dbc, strSQL, adOpenForwardOnly, adLockReadOnly, adCmdText)<BR><BR>&#039;The two lines bellow work fine outside the function<BR>&#039;Set rs = Server.CreateObject("ADODB.Recordset") <BR>&#039;rs.Open strSQL, dbc, adOpenForwardOnly, adLockReadOnly, adCmdText <BR><BR>Do While Not rs.EOF<BR> Response.Write rs("Name") & "<BR>"<BR> rs.MoveNext<BR>Loop<BR><BR>rs.Close<BR>Set rs = Nothing<BR>dbc.Close<BR>Set dbc = Nothing<BR>%&#062;<BR><BR>If I use global variables and pass them by reference to a Subroutine everything works. This makes the code a bit harder to read though.<BR><BR>Sorry for the humongous post and thanks.<BR>

  2. #2
    Join Date
    Dec 1969

    Default RE: functions & object references

    &#039;You see the order of parameters you are sending to the function:<BR>&#039;The line causing the error bellow<BR>rs = OpenRecordset(dbc, strSQL, adOpenForwardOnly, adLockReadOnly, adCmdText)<BR><BR>Now look at the order of parameters in function:<BR>Function OpenRecordset(...)<BR>...<BR>rst.Open strSQL, dbc, CursorType, LockType, Options<BR>OpenRecordset = rst &#039;is the same as:<BR>rs = OpenRecordset(strSQL,dbc, adOpenForwardOnly, adLockReadOnly, adCmdText) &#039;but not:<BR>rs = OpenRecordset(dbc,strSQL,adOpenForwardOnly, adLockReadOnly, adCmdText)<BR>I think you need to change on a page:<BR>rs = OpenRecordset(strSQL,dbc,adOpenForwardOnly, adLockReadOnly, adCmdText) &#039; or not sure,maybe the order in function as well...<BR><BR>Hope it will help...<BR><BR><BR><BR><BR><BR><BR>&#039;The line causing the error bellow <BR>rs = OpenRecordset(dbc, strSQL, adOpenForwardOnly, adLockReadOnly, adCmdText) <BR><BR><BR>

  3. #3
    Emil Guest

    Default RE: functions & object references

    Thanks Greg for pointing this out. Unfortunately it didn&#039;t work. In any case I though that I only needed to match the order of the arguments from the function definition and the function call, not any code or consecutive calls inside that the funcion being called.<BR><BR>I did try function definition:<BR>Function OpenRecordset(ByVal strSQL, ByRef dbc, ByVal CursorType, ByVal LockType, ByVal Options)<BR>Then inside the function:<BR>rst.Open strSQL, dbc, CursorType, LockType, Options<BR>And the call:<BR>rs = OpenRecordset(strSQL, dbc, adOpenForwardOnly, adLockReadOnly, adCmdText)<BR><BR>Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts