using functions to build connection & recordset

Results 1 to 2 of 2

Thread: using functions to build connection & recordset

  1. #1
    Young Student Guest

    Default using functions to build connection & recordset

    I&#039;m simply trying to clean up my code, and separate my scripting from my html as much as possible. I have one function that builds an ADO connection based on my arguements, and another that builds a Recordset.<BR><BR>The problem:<BR><BR>When I get to the .ActiveConnection property of the recordset object, I can&#039;t figure out how to pass the connection string from the connection-function, into the recordset-function, to create a successful connection to the db and build a recordset!<BR><BR>I built one function that assembles a dsn-less connection and one that uses the system DSN, neither work and cause the same error on the next page where I call the functions:<BR><BR>Error Type:<BR>Microsoft VBScript compilation (0x800A0401)<BR>Expected end of statement<BR><BR>Here&#039;s the code, go ahead, rip into me you animals!<BR><BR> &#039;Functions for calling/closing a db connection<BR> &#039;and building/closing a recordset.<BR> <BR> &#039;build a dynamic, dsn-less connection<BR> Function buildConn(provider, ds, cat, uid, pass)<BR> Dim myConn<BR> Dim strConnString<BR> strConnString = "Provider=" & provider & "; Data Source=" & ds & "; " _<BR> & "Initial Catalog=" & cat & "; User ID=" & uid & "; Password=" & pass & ";"<BR> <BR> On Error Resume Next<BR> Set myConn = Server.CreateObject("ADODB.Connection")<BR> myConn.Open(strConnString)<BR> If (IsObject(myConn)) Then<BR> On Error Goto 0<BR> End If<BR> <BR> buildConn = myConn<BR> End Function<BR> <BR> &#039;build a dynamic, dsn connection<BR> Function buildConnDSN(dsn, user, pass)<BR> Dim myConnDSN<BR> &#039;Dim strConnDSN<BR> &#039; strConnDSN = dsn & ", " & user & ", " & pass<BR> <BR> On Error Resume Next<BR> Set myConnDSN = Server.CreateObject("ADODB.Connection")<BR> myConnDSN.Open dsn, user, pass<BR> If (IsObject(myConnDSN)) Then<BR> On Error Goto 0<BR> End If<BR> <BR> buildConnDSN = myConn<BR> End Function<BR> <BR> &#039;Function that builds a Recordset<BR> Function buildRS(aconn, source, curtype, curloc, loctype)<BR> Dim myRS<BR> On Error Resume Next<BR> Set myRS = Server.CreateObject("ADODB.Recordset")<BR> If (IsObject(myRS)) Then<BR> On Error Goto 0<BR> With myRS<BR> .ActiveConnection = aconn<BR> .Source = source<BR> .CursorType = curtype<BR> .CursorLocation = curloc<BR> .LockType = loctype<BR> .Open()<BR> End With<BR> End If<BR> buildRS = myRS<BR> End Function

  2. #2
    Join Date
    Dec 1969

    Default Well...

    You open your connection, and build your recordset inside the function. Which makes that record set and connection object local to that function. As soon as the function dies, so does your object.<BR><BR>-- Whol

Posting Permissions

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