Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,327
Default ADO Record by Record

The "an error" is trying to tell you what's wrong. What does it say ?

You dont' specify a cursor. If it's forwardonly then you can't move back and
forth.

And your paste method is painfully slow. Looping record by record is only
required if you manipulate them on the fly. This single line ought to do the
whole job:

Range("A2").CopyFromRecordset rstRecordSet

HTH. Best wishes Harald

"Braindeadbeachbum" skrev i
melding ...
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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default ADO Record by Record

Hi Harald

When I import using the complete recordset (Range("A2").CopyFromRecordset
rstRecordSet) I receive the following error:
Error - Automation error Unspecified error

I will bet that it's due to the RTF field being to long or unrecognizable. I
know that the Excel limit per cell is 32 767 characters and don't believe the
field is larger than this but I still think that the RTF field is causing the
problem as it's the only thing that changed since the query worked.

If I use the code attached the error is as follows:
Error - Item cannot be found in the collection corresponding to the
requested name or ordinal.

If I change:
TargetRange.Offset(i, intColIndex).Value = rstRecordSet!strFieldName
To:
TargetRange.Offset(i, intColIndex).Value = rstRecordSet!fldName
,where fldName is the actual field name, the query works but this means I
will have to hard code all the field names.

I even tried the following in vain:
rstRecordSet!rstRecordSet.Fields(intColIndex).Name

I know I know, it was a long shot in frustration!

Bottom line is that I will have to loop through the records to enable me to
edit the RTF field but I don't want to hardcode the field names as this just
does not look right and there must be a easier way of doing this.



"Harald Staff" wrote:

The "an error" is trying to tell you what's wrong. What does it say ?

You dont' specify a cursor. If it's forwardonly then you can't move back and
forth.

And your paste method is painfully slow. Looping record by record is only
required if you manipulate them on the fly. This single line ought to do the
whole job:

Range("A2").CopyFromRecordset rstRecordSet

HTH. Best wishes Harald

"Braindeadbeachbum" skrev i
melding ...
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




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,327
Default ADO Record by Record

Nevermind the names. You should be able to address its index number
directly:

TargetRange.Offset(0, intColIndex).Value = rstRecordSet(intColIndex)

Why insert 32k + fields into Excel ? It will probably not make any sense in
a spreadsheet. What happens if you remove that field from your SQL, does it
still err ?

HTH. Best wishes Harald

"Braindeadbeachbum" skrev i
melding ...
Hi Harald

When I import using the complete recordset (Range("A2").CopyFromRecordset
rstRecordSet) I receive the following error:
Error - Automation error Unspecified error

I will bet that it's due to the RTF field being to long or unrecognizable.

I
know that the Excel limit per cell is 32 767 characters and don't believe

the
field is larger than this but I still think that the RTF field is causing

the
problem as it's the only thing that changed since the query worked.

If I use the code attached the error is as follows:
Error - Item cannot be found in the collection corresponding to the
requested name or ordinal.

If I change:
TargetRange.Offset(i, intColIndex).Value = rstRecordSet!strFieldName
To:
TargetRange.Offset(i, intColIndex).Value = rstRecordSet!fldName
,where fldName is the actual field name, the query works but this means I
will have to hard code all the field names.

I even tried the following in vain:
rstRecordSet!rstRecordSet.Fields(intColIndex).Name

I know I know, it was a long shot in frustration!

Bottom line is that I will have to loop through the records to enable me

to
edit the RTF field but I don't want to hardcode the field names as this

just
does not look right and there must be a easier way of doing this.



"Harald Staff" wrote:

The "an error" is trying to tell you what's wrong. What does it say ?

You dont' specify a cursor. If it's forwardonly then you can't move back

and
forth.

And your paste method is painfully slow. Looping record by record is

only
required if you manipulate them on the fly. This single line ought to do

the
whole job:

Range("A2").CopyFromRecordset rstRecordSet

HTH. Best wishes Harald

"Braindeadbeachbum" skrev

i
melding ...
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






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default ADO Record by Record

Harald Staff is the best! Never knew you could referance the index number
instead of the field name.

Thanks alot!

"Harald Staff" wrote:

Nevermind the names. You should be able to address its index number
directly:

TargetRange.Offset(0, intColIndex).Value = rstRecordSet(intColIndex)

Why insert 32k + fields into Excel ? It will probably not make any sense in
a spreadsheet. What happens if you remove that field from your SQL, does it
still err ?

HTH. Best wishes Harald

"Braindeadbeachbum" skrev i
melding ...
Hi Harald

When I import using the complete recordset (Range("A2").CopyFromRecordset
rstRecordSet) I receive the following error:
Error - Automation error Unspecified error

I will bet that it's due to the RTF field being to long or unrecognizable.

I
know that the Excel limit per cell is 32 767 characters and don't believe

the
field is larger than this but I still think that the RTF field is causing

the
problem as it's the only thing that changed since the query worked.

If I use the code attached the error is as follows:
Error - Item cannot be found in the collection corresponding to the
requested name or ordinal.

If I change:
TargetRange.Offset(i, intColIndex).Value = rstRecordSet!strFieldName
To:
TargetRange.Offset(i, intColIndex).Value = rstRecordSet!fldName
,where fldName is the actual field name, the query works but this means I
will have to hard code all the field names.

I even tried the following in vain:
rstRecordSet!rstRecordSet.Fields(intColIndex).Name

I know I know, it was a long shot in frustration!

Bottom line is that I will have to loop through the records to enable me

to
edit the RTF field but I don't want to hardcode the field names as this

just
does not look right and there must be a easier way of doing this.



"Harald Staff" wrote:

The "an error" is trying to tell you what's wrong. What does it say ?

You dont' specify a cursor. If it's forwardonly then you can't move back

and
forth.

And your paste method is painfully slow. Looping record by record is

only
required if you manipulate them on the fly. This single line ought to do

the
whole job:

Range("A2").CopyFromRecordset rstRecordSet

HTH. Best wishes Harald

"Braindeadbeachbum" skrev

i
melding ...
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









  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,327
Default ADO Record by Record

That's the spirit ! You're welcome.

Best wishes Harald

"Braindeadbeachbum" skrev i
melding ...
Harald Staff is the best! Never knew you could referance the index number
instead of the field name.

Thanks alot!



Reply
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 10:33 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"