Recordset confusion and stuck

tatendatiffany

Board Regular
Joined
Mar 27, 2011
Messages
103
ok so i am having difficulties with creating a recordset for a function. The recordset is suppose to execute the strings returned from the look up

the function is
Code:
getrecordset

and the whole code is
Code:
Function getrecordset(str) As String
 'create recordset object
     Dim rs As adodb.Recordset
     Set rs = New adodb.Recordset
     With rs
    .ActiveConnection = cn
    'using the str variable open the recordset
    .Source = str
    .Open
     End With
    'if recordset NOT NULL
     If Not (rs.BOF And rs.EOF) Then
    getrecordset = rs(0)
        getrecordset = Recordset
     'else
        'getrecordset = "No Result"
    getrecordset = vbNullString
    End If
                                'close and destroy recordset object
     Set rs = Nothing
    End Function

Sub getresults()
  Dim r As Integer                    'row counter
    Dim c As Integer                    'column counter
    Dim str As String
    Dim rs As Recordset
    Dim cn As adodb.Connection
    Set cn = New adodb.Connection
    With cn
       .ConnectionString = "Password=" & password & ";Persist Security Info=True;User ID=" & username & ";Data Source=" & DatabaseEnv & ";Mode=ReadWrite;"
       .Provider = "IBMDADB2.DB2COPY1"
       ' .Open
    'End With
    rs = "select * from worksheet" & Range("Environ1") & ("Environ3") & ("Environ4") & ("Environ5") & ("Environ7")
   Set rs = Db.OpenRecordset
    For c = 31 To 35                 'columns AE to AI
        For r = 2 To 39                             'start with first row
            If Cells(r, c).Value <> "" Then            'is there a value in the row?
                str = Cells(r, c).Value                 'this is the SQL string we need for the recordset
                Cells(r, c - 5).Value = getrecordset(str) 'use a function to open the recordset and return the value to column 3
            End If
        Next r
    Next c                              'next column
End With
End Sub


how to a create a recordset for the function i have. I want it to execute the returned results (being sql queries)
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top