Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2004
    Location
    Glasgow, Scotland
    Posts
    107

    Unanswered: Return Recordset From Class

    I'm relatively new to class programming and I am trying to wtite a class that returns a recordset according to the SQL string specified. Here's what I have so far:

    Option Explicit

    Private cn As ADODB.Connection
    Private rst As ADODB.Recordset

    Private Sub Class_Initialize()

    Set cn = New ADODB.Connection
    cn.Open "Provider=sqloledb;" & _
    "Data Source=Northnet-18;" & _
    "Initial Catalog=IDLS_Test;" & _
    "Integrated Security=SSPI"

    End Sub

    Public Function OpenRecordset(strSQLStatement As String) As ADODB.Recordset

    Set rst = New ADODB.Recordset
    rst.Open strSQLStatement, cn, adOpenKeyset, adLockReadOnly

    End Function

    Public Function CloseRecordset()

    rst.Close
    Set rst = Nothing

    cn.Close
    Set cn = Nothing

    End Function

    I can open and close the recorsdset fine but I can't actually get the field values of the records out of the class. I think that I may have to use Get, Let Functions and a Collection but I'm not sure how this works. Could anyone point me in the right direction?

    Thanks

  2. #2
    Join Date
    Jan 2005
    Posts
    18
    Option Explicit

    Private cn As ADODB.Connection
    Private rst As ADODB.Recordset

    Private Sub Class_Initialize()

    Set cn = New ADODB.Connection
    cn.Open "Provider=sqloledb;" & _
    "Data Source=Northnet-18;" & _
    "Initial Catalog=IDLS_Test;" & _
    "Integrated Security=SSPI"

    End Sub

    Public Function OpenRecordset(strSQLStatement As String) As ADODB.Recordset

    Set rst = New ADODB.Recordset
    rst.Open strSQLStatement, cn, adOpenKeyset, adLockReadOnly

    OpenRecordset = rst

    End Function

    Public Function CloseRecordset()

    rst.Close
    Set rst = Nothing

    cn.Close
    Set cn = Nothing

    End Function


    Just Try adding that extra line in the function
    if it doesnt work u should go for get or let function
    but this sure will work
    try this and give me a feedback yaar
    bye

  3. #3
    Join Date
    Mar 2004
    Location
    Glasgow, Scotland
    Posts
    107
    Thanks, I'd already tried that but after reading your post thought I would give it another go!

    Turns out you also need to use the SET command:

    Option Explicit

    Private cn As ADODB.Connection
    Private rst As ADODB.Recordset

    Private Sub Class_Initialize()

    Set cn = New ADODB.Connection
    cn.Open "Provider=sqloledb;" & _
    "Data Source=Northnet-18;" & _
    "Initial Catalog=IDLS_Test;" & _
    "Integrated Security=SSPI"

    End Sub

    Public Function OpenRecordset(strSQLStatement As String) As ADODB.Recordset

    Set rst = New ADODB.Recordset
    rst.Open strSQLStatement, cn, adOpenKeyset, adLockReadOnly

    Set OpenRecordset = rst

    End Function

    Public Function CloseRecordset()

    rst.Close
    Set rst = Nothing

    cn.Close
    Set cn = Nothing

    End Function

  4. #4
    Join Date
    Jan 2005
    Posts
    18
    yes yaar sorry

    I made a mistake.

    As rst is an Object we should use set to assign a value

    bye

  5. #5
    Join Date
    Jan 2005
    Posts
    18
    yes yaar sorry

    I made a mistake.

    As rst is an Object we should use set to assign a value

    bye

Posting Permissions

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