Code check, please

ExcelChampion

Well-known Member
Joined
Aug 12, 2005
Messages
976
I cannot figure out what is wrong with this code. I get the error, "Object variable or With block variable not set."

I just don't see the problem...

Code:
Sub remDuplicates()
    Dim sSql As String
    Dim RFQ_NUM As String
    Dim REVISION As String
    Dim DOC_LINK As String
    Dim DATE_ISSUED As String
    Dim lRows As Long
    Dim con As ADODB.Connection
    Dim rs As ADODB.Recordset
    
    sSql = "Select RFQ_NUM, REVISION, DOC_LINK, DATE_ISSUED " _
         & "From tbl_Docs " _
         & "Group By RFQ_NUM, REVISION, DOC_LINK, DATE_ISSUED " _
         & "Having Count(*) > 1"
    
    Set rs = New ADODB.Recordset
    
    With rs
        .ActiveConnection = CurrentProject.AccessConnection
        .CursorLocation = adUseClient
        .CursorType = adOpenForwardOnly
        .LockType = adLockReadOnly
        .Source = sSql
        .Open
        
        While Not .EOF
            sRFQ_NUM = .Fields(0).Value
            sREVISION = .Fields(1).Value
            sDOC_LINK = .Fields(2).Value
            sDATE_ISSUED = .Fields(3).Value
            
            sSql = "Delete*  " _
                 & "From tbl_Docs " _
                 & "Where RFQ_NUM = '" & sRFQ_NUM & "' " _
                 & "And REVISION = '" & sREVISION & "' " _
                 & "And DOC_LINK = '" & sDOC_LINK & "' " _
                 & "And " & sDATE_ISSUED & "<> " _
                 & "(Select Min(" & sDATE_ISSUED & ") " _
                 & "From tbl_Docs " _
                 & "Where RFQ_NUM = '" & sRFQ_NUM & "' " _
                 & "And REVISION = '" & sREVISION & "' " _
                 & "And DOC_LINK = '" & sDOC_LINK & "')"
                 

            con.Execute sSql, lRows
            
            
            .MoveNext
        Wend
    End With
    
    rs.Close
    Set rs = Nothing
    Set con = Nothing
End Sub


Thanks for taking a look.

Todd
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Craps out here: con.Execute sSql, lRows

I'm thinking its a grammatical error somewhere...missing comma, asterisk, ??? But maybe its something else I'm over looking.

Thanks.
 
Upvote 0
I would say it is most likely an issue with your SQL code. Shouldn't there be a space between the "Delete" and "*"?

One trick I often use in these cases is to return sSql to a MsgBox and review your SQL code, to see if it looks right.
Try running that same code in a Access Query (going into SQL code and typing that exact same code in there).
Does that work? If it does, your SQL code should be valid.
If not, play around with it and get it working in the Query first. Then, go to SQL View and note the code. This what your VBA code needs to mirror.
 
Upvote 0
I would say it is most likely an issue with your SQL code. Shouldn't there be a space between the "Delete" and "*"?

I agree, it cannot return effected row count and likely cause object variable not set error.
 
Upvote 0

Forum statistics

Threads
1,221,889
Messages
6,162,627
Members
451,778
Latest member
ragananthony7911

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