Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using ADO, how do you copy one column to a certain field

Status
Not open for further replies.

sharonc

Programmer
Jan 16, 2001
189
US
I'm trying to copy data from SQL Server to Excel. In excel I have 10 columns, I want to copy the SQL Server data into columns 2, 4, 7 and 9. How do I do this copy? Here is my code:

Private Sub Workbook_Open()
On Error GoTo Err_Workbook_Open

Dim strSQL As String
Dim intMth As Integer
Dim intYr As Integer
Dim rs As ADODB.Recordset
Dim objmyconn As ADODB.Connection
Dim ObjErr As ADODB.Error
Dim ws As Worksheet
Dim fldcount As Integer
Dim dblBurn As Double
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

'Declare variables
Set objmyconn = New ADODB.Connection

'Open Connection
objmyconn.ConnectionString = "Provider=SQLOLEDB;Data Source=APACHEFIX.AEPNET.COM;User ID=MGS; Password=;Prompt=Complete"
objmyconn.Open

strSQL = "Select FRAME, TCARS, TBTU, TRECIEV, CINV, (U2BURN+U3BURN) as dblBurn, Datepart(yy,Frame) as FrameYr, Datepart(DD,Frame) as FrameDay from dbo.Fuel WHERE Datepart(yy,Frame) = 2003 and Datepart(d,Frame) = 1;"

Set rs = New ADODB.Recordset
rs.CursorType = adOpenKeyset
rs.Open strSQL, objmyconn

'set worksheet as active sheet
Set xlSheet = Sheets("A")

'Add burn 2 and 3
dblBurn = rs(6) + rs(7)
Stop
'Copy Data to Excel
xlSheet.Range("m10:m40").CopyFromRecordset rs(2)
'xlSheet.Range("I10").CopyFromRecordset rs(6)
'xlSheet.Range("M10").CopyFromRecordset rs(2)
'xlSheet.Range("N10").CopyFromRecordset rs(3)
'xlSheet.Range("O10").CopyFromRecordset rs(4)
'xlSheet.Range("P10").CopyFromRecordset rs(5)
'xlSheet.Range("J3").CopyFromRecordset rs(7)


Workbook_Open_Exit:
'Close connection
Set xlSheet = Nothing
Set xlBook = Nothing
objmyconn.Close
Set objmyconn = Nothing
Exit Sub


End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top