LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default ADO Record by Record

Hi Guys

I need to go through each record one at a time to convert RTF to Text from
SQL. The code I currently use is the following. My problem is that I cannot
define the rstRecordSet!strFieldName like I'm doing it now as I receive an
error. Is there any way around hard coding each fieldname?


Private Sub GetData()
Dim i As Integer
Dim strCellLocation As String
Dim strRecord As String
Dim strFieldName As String


Sheets("Raw Data").Select
Range("A1").Select

On Error GoTo ErrorHandler

Set connConnect = CreateObject("ADODB.Connection")
Set rstRecordSet = CreateObject("ADODB.Recordset")

ActiveSheet.Cells.Clear
Set TargetRange = ActiveSheet.Cells(1, 1)

With connConnect
.provider = "SQLOLEDB"
.connectionString = "Data Source=?????????;Integrated
Security=SSPI;Initial Catalog=???????"
.Open
End With

With rstRecordSet
.Open strSQLCommAND, connConnect
For intColIndex = 0 To rstRecordSet.Fields.Count - 1
TargetRange.Offset(0, intColIndex).Value =
rstRecordSet.Fields(intColIndex).Name
Next
End With

i = 2
rstRecordSet.movefirst
Do Until rstRecordSet.EOF
For intColIndex = 0 To rstRecordSet.Fields.Count - 1
strFieldName = rstRecordSet.Fields(intColIndex).Name
TargetRange.Offset(i, intColIndex).Value = rstRecordSet!strFieldName
Next
i = i + 1
rstRecordSet.movenext
Loop

rstRecordSet.Close
Set rstRecordSet = Nothing
connConnect.Close
Set connConnect = Nothing

Exit Sub
ErrorHandler:
If connConnect.State = 1 Then
connConnect.Close
Set connConnect = Nothing
End If
MsgBox "Error - " & Err.Description, vbCritical, Err.Source

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
I need to have a record number change for every record Puget Sound Courier Service Excel Discussion (Misc queries) 1 July 12th 09 03:32 PM
Open new record with selected fields from previous record Design by Sue Excel Discussion (Misc queries) 1 June 12th 09 02:24 PM
Finding last record in month for each of several types of record. Richard Buttrey Excel Programming 5 April 4th 05 02:11 AM
Record Macro - Record custom user actions Sal[_4_] Excel Programming 1 December 23rd 04 03:18 PM
Need help autopopulating next new record with previous record data Harry S[_3_] Excel Programming 2 October 1st 03 10:59 PM


All times are GMT +1. The time now is 06:51 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"