+ Reply to Thread
Results 1 to 24 of 24

How to use RANGE Statment - Receiving Error 1004

Hybrid View

  1. #1
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: How to use RANGE Statment - Receiving Error 1004

    You must build criteria string from the range for the IN clause:

    Sub CBreader()
    
        'Establish connection and record set with CRMDB
        Dim cnn As ADODB.Connection
        Dim rst As ADODB.Recordset
        
        
        Set cnn = New ADODB.Connection
        Set rst = New ADODB.Recordset
        
        'Dim strSQL As String
        
        'Declare field variables
        Dim GetItem As String
        Dim FPolicy As Range
        Dim FBlock As String
        Dim FMsg As String
        'Dim WS As Worksheet
        
        'Open the ODBC Connection using this statement
        
        With Sheets("EF")
            Set FPolicy = .Range("D7:D" & .Cells(.Rows.Count, 1).End(x1Up).Row).Value
            
            cnn.Open "CRMDB", "XXXXX", "xxxxx"
            rst.ActiveConnection = cnn
            rst.CursorLocation = adUseServer
            
            rst.Source = "Select * FROM u_CloseBlock WHERE [u_CloseBlock].[Policy] in (" & CriteriaFromRange(FPolicy) & ")"
            
            rst.Open
            
            If rst.EOF Then
              GetItem = "Not Found"
            Else
                FPolicy = rst.Fields("Policy")
                FBlock = rst.Fields("Block_No")
            End If
            
            If FBlock = "1011" Then
                FMsg = "Yes"
                .Range("K7", .Range("LastCell")).Value = FMsg
            ElseIf IsNull(FBlock) Then
                FMsg = "No"
                .Range("K7", .Range("LastCell")).Value = FMsg
                
            End If
        End With
        
           
        'Close everything and set the references to nothing
        rst.Close
        Set rst = Nothing
        cnn.Close
        Set cnn = Nothing
         
     End Sub
    
    Function CriteriaFromRange(rgCriteria As Range) As String
       Dim Cell           As Range
       Dim sTemp           As String
       For Each Cell In rgCriteria.Cells
          If Len(Cell.Value) > 0 Then sTemp = sTemp & ",'" & EscapeQuotes(Cell.Value) & "'"
       Next Cell
       ' now strip off leading comma
       CriteriaFromRange = Mid$(sTemp, 2)
    End Function
    Function EscapeQuotes(sInput As String) As String
       EscapeQuotes = Replace(sInput, "'", "''")
    End Function
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  2. #2
    Registered User
    Join Date
    04-30-2007
    Posts
    20

    Re: How to use RANGE Statment - Receiving Error 1004

    One more thing, I forgot to put in my previous post, I highlighted the code that I am using in red. I tried the Offset clause but it did not work for me properly.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1