HELP "Object Required"

tatendatiffany

Board Regular
Joined
Mar 27, 2011
Messages
103
Hi,
could some one help me i am facing a problem on my code "object required" but i have specified in the function what could i have done wrong: the line higlighted in bold is where i am getting the error but i thought i had specified in the function the recordset??:confused::confused:

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;"   'Assumption based on your connection string being valid
       .Provider = "IBMDADB2.DB2COPY1"      'assumption based on your provider being correct
       ' .Open                       'if the above are true then this will open DB
    'End With
    
   [B]Set rs = Db.OpenRecordset
[/B]    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
 
so my connectionstring is declared in a in a public function called DBConnection. how about would declare this in my get results sub??
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
You need to declare the Connection object rather than connection string as public (cn in your examples) and then refer to cn in your routines. Note: do not declare cn anywhere else - i.e. remove any other 'Dim cn...' statements
 
Upvote 0
one last question so i have done what you have suggested and it seems to be working but i have one lat question. so should i delete these lines from my sub
Code:
 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

and just have
Code:
Set rs = MFcnn.OpenRecordset
 
Upvote 0
No, since OpenRecordset is not a method of a Connection. You need something like:
Code:
set rs = new adodb.recordset
rs.open strSQL, MFcnn
where strSQL is your SQL string.
 
Upvote 0
but my sqlstrings are in the excel sheets and are called in this part of the code
Code:
 For c = 31 To 35                
        For r = 2 To 39                          
            If Cells(r, c).Value <> "" Then          
                str = Cells(r, c).Value                  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                            
End With
End Sub
 
Upvote 0
Then you need to create the recordset in the GetRecordset function using the sql string that is passed to it.
 
Upvote 0
see this is the part i am trully failing to grasp because i thought my sql in the sheets are the recordsets and the "for loop" i just mentioned is the method of going the recordsets.
so now let me get this clearly i need to do an recordset("select from ") that is telling excel to go into the sheet and run those sqls but arent i doing that already with the for loop
 
Upvote 0
It is the getrecordset function that has to create a recordset based on the specified SQL string passed to it, therefore that is the place to do it.
 
Upvote 0
but i dont want the sql to be hardcoded that why i had it on the excel sheet. so then how do i proceed from here i'm so confused right now
so in my code
Code:
getrecordset(str)
opens the recordsets them being (str)
so do i need to write a string for str that being " go into excel sheets then run theses sql in the cells??
 
Upvote 0
No. Your loop currently picks up the string from the cells and passes it to the Getrecordset function. It is in that function that you need to create the recordset.
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,135
Members
452,890
Latest member
Nikhil Ramesh

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