LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
Bam Bam is offline
external usenet poster
 
Posts: 48
Default CopyFromRecordset

Hi All,

I'm having an issue where I would like to return all records based on a
cell, and copy them across columns until they stop finding a matching record.

The Select statement may return up to 4 or 5 values, but it is only using
the last value it finds, then moves down to the next row.

I want the 3 Fields for each record found to be put into the same row as the
strTargetStock, and keep repeating across the columns until the last record
is found.

I'm sure this is easy, but i've not been able to figure it out.
Any help would be appreciated.

Cheers,
Bam.

Option Explicit

Private con As Object
Private rst As Object

Sub ADOData(sSQL As String, rg As Range)
Dim sConn As String
Dim sheet As Worksheet
Dim ws As Worksheet
Set ws = ActiveSheet

On Error GoTo ErrHandler

'Create a new recordset object
Set con = CreateObject("ADODB.Connection")
'Set con = New ADODB.Connection
con.Open "Driver={SQL
Server};Server=SERVER;Database=DB171;Uid=USER;Pwd= PASSWORD;"

Set rst = CreateObject("ADODB.Recordset")

Set rst = con.Execute(sSQL, , 1)

rg.CopyFromRecordset rst

rst.Close

con.Close

'Clean up.
Set rst = Nothing
Set rg = Nothing
Exit Sub
ErrHandler:
MsgBox "Sorry,an error occured." & Err.Description & " " & sSQL, vbOKOnly

End Sub

Sub Customer()

Dim i As Integer
Dim c As Integer
Dim j As Byte
Dim intHowManyRow As Integer
Dim intStartRow As Integer
Dim intEndRow As Integer
Dim strTargetStock As String
Dim Customer As String
Dim DelCode As String
Dim Mysht As Worksheet

Set Mysht = ActiveSheet

intHowManyRow = Mysht.UsedRange.Rows.Count
intStartRow = 2
c = 3
intEndRow = intStartRow + intHowManyRow - 1
Range("C1") = "LM Code"
Range("D1") = "Width"
Range("E1") = "Length"
For i = intStartRow To intEndRow
strTargetStock = RTrim(LTrim((Cells(i, 1))))
Application.ScreenUpdating = False
Cells(i, 3).Select
'Call Data
If strTargetStock < "" Then
Call ADOData("SELECT IM_STOCK, IM_WIDE, IM_LEN " & _
"FROM IMI1 WITH (NOLOCK) " & _
"WHERE IM_CUST_STOCK = ('" & strTargetStock & "') AND
(IM_ACTIVE = 1)", Cells(i, c))
End If
Application.ScreenUpdating = True
Next i

ActiveCell.Select

End Sub
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL CopyFromRecordset Bret Excel Programming 2 July 23rd 06 06:56 AM
CopyfromRecordset MattShoreson[_67_] Excel Programming 2 March 15th 06 01:36 PM
CopyFromRecordset Ernst Guckel[_4_] Excel Programming 3 May 1st 05 08:03 PM
CopyFromRecordset does nothing Hafeez Excel Programming 2 August 13th 04 07:20 PM
CopyFromRecordset does nothing E Harris Excel Programming 5 January 8th 04 04:29 PM


All times are GMT +1. The time now is 06:46 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"