SQL in VBA - Object 424 - Object Required

herghost

New Member
Joined
Aug 24, 2011
Messages
9
Hi guys

I am getting the above error when executing my code, can anyone spot anything obvious?

Code:
Dim conn As ADODB.Connection    Dim rs As ADODB.Recordset
    Dim sConnString As String
    userid = GetName(2)
    sConnString = "Provider=SQLOLEDB;REMOVEDREST"
    
   
    Set conn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    
    
    conn.Open sConnString
    Set rs = conn.Execute("SELECT polNumber FROM WOFT_tbl_clients WHERE staffName = '" & userid & "';")
 
    If Not rs.EOF Then
    rs.MoveFirst
    
    Do Until rsData.EOF
    Set casesheet.Columns(1) = rs![polNumber]
    rs.MoveNext
    Loop
    rs.Close
    Else
        MsgBox "Error: No records returned.", vbCritical
    End If


    
    If CBool(conn.State And adStateOpen) Then conn.Close
    Set conn = Nothing
    Set rs = Nothing


End Sub


Basically I am opening a worksheet control in a userform in vba and trying to paste the database results to it.

getName is a function which returns the excel user.

Cheers
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
1. What's the intention of this code?
Code:
Set casesheet.Columns(1) = rs![polNumber]
2. Change code to this:
Code:
If CBool(conn.State And adStateOpen)[CODE] = adStateOpen
Then conn.Close[/CODE]
 
Upvote 0
I have now noticed I had an error on the Do unti line, however I have changed this from rsData. to rs. and now have a new error!

Object doesn't support this property or method (Error 438)
 
Upvote 0
You use "rsData" variable in "Do Until" line whereas other places have "rs" variable.
And you can't use "Set" with Columns property. Anyway, your intention is not clear what you try to do. May be this:
Code:
Dim i As Long

i=1
Do Until rs.EOF
    casesheet.Cells(i) = rs("polNumber")
    i = i + 1
    rs.MoveNext
Loop
 
Last edited:
Upvote 0
1. What's the intention of this code?
Code:
Set casesheet.Columns(1) = rs![polNumber]



2. Change code to this:
Code:
If CBool(conn.State And adStateOpen) = adStateOpen Then conn.Close


1) To print the results in column 1 (a:a), I have removed the SET command as I reliase id did not need it, and it now prints the 1st result from the db all the way down the column :(

2) What should I be replacing with this?

Thanks for your reply!
 
Upvote 0
Sorry, there is no notification made if another reply has been made!

Yes that is my intention, but to print vertically down the sheet not horizontally, but leave the 1st cell blank?

Thanks, this is the closest I have got!
 
Upvote 0
Then try this:
Code:
Sub SomeSub()
    
    Dim conn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim sConnString As String
    Dim i As Long
    
    UserId = GetName(2)
    sConnString = "Provider=SQLOLEDB;REMOVEDREST"
    
   
    Set conn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    
    conn.Open sConnString
    Set rs = conn.Execute("SELECT polNumber FROM WOFT_tbl_clients WHERE staffName = '" & UserId & "';")
 
    i = i + 1
    If Not rs.EOF Then
        rs.MoveFirst
        Do Until rsData.EOF
            casesheet.Cells(i, 1) = rs![polNumber]
            rs.MoveNext
            i = i + 1
        Loop
        rs.Close
    Else
        MsgBox "Error: No records returned.", vbCritical
    End If
    If CBool(conn.State And adStateOpen) = adStateOpen Then conn.Close
    
    Set conn = Nothing
    Set rs = Nothing
End Sub
 
Upvote 0
Thanks - This now shows "Invalid procedure call or argument (Error 5)"

Then try this:
Code:
Sub SomeSub()
    
    Dim conn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim sConnString As String
    Dim i As Long
    
    UserId = GetName(2)
    sConnString = "Provider=SQLOLEDB;REMOVEDREST"
    
   
    Set conn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    
    conn.Open sConnString
    Set rs = conn.Execute("SELECT polNumber FROM WOFT_tbl_clients WHERE staffName = '" & UserId & "';")
 
    i = i + 1
    If Not rs.EOF Then
        rs.MoveFirst
        Do Until rsData.EOF
            casesheet.Cells(i, 1) = rs![polNumber]
            rs.MoveNext
            i = i + 1
        Loop
        rs.Close
    Else
        MsgBox "Error: No records returned.", vbCritical
    End If
    If CBool(conn.State And adStateOpen) = adStateOpen Then conn.Close
    
    Set conn = Nothing
    Set rs = Nothing
End Sub
 
Last edited:
Upvote 0
Actually ignore me :) Now working, well apart from it starts from row 1 still, but I will try and work that out :) Your help is greatly appreciated, I have been trying to figure this out for 2 days before I gave in and asked :)
 
Upvote 0
Oh! Change rs![polNumber] to rs!polNumber or rs(polNumber").
 
Upvote 0

Forum statistics

Threads
1,215,564
Messages
6,125,579
Members
449,237
Latest member
Chase S

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