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: 12
Default Using ADO Recordsets to pull data from a closed Excel WB.

I am using code from Ron de Bruin (thanks very much by the way!!!) to pull
data from a closed Workbook residing on a server on our network. This closed
workbook holds project historical data used in the workbook I am making the
call from. This closed workbook holds dates, integers, floating point
numbers, and text. The ADO code Ron provides examples of pulls the dates,
integers, and floating point numbers but not the text. The code is as
follows:

Option Explicit

Public Sub GetData(SourceFile As Variant, _
SourceSheet As String, _
SourceRange As String, _
DestinationRange As Range, _
GetHeader As Boolean, _
UseHeaderRow As Boolean)

' Setup variables
Dim RecordSetData As ADODB.Recordset
Dim DBConnectString As String
Dim SQLCommandObject As String
Dim LoopCounter As Long

' Build the SQL Command to pull the data from the source DB.
SQLCommandObject = "SELECT * FROM [" & SourceSheet$ & "$" & SourceRange$
& "];"

' If GetHeader determines whether or not to pull the header information
off the top row of the
' range and put it, if it is true, into the DestinationRange.
If GetHeader = False Then
DBConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data
Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 8.0;HDR=No"";"
Else
DBConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data
Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 8.0;HDR=Yes"";"
End If

On Error GoTo ErrorHandler

Set RecordSetData = New ADODB.Recordset
RecordSetData.Open SQLCommandObject, DBConnectString, adOpenForwardOnly,
adLockReadOnly, adCmdText

' Check to make sure we received data and copy the data
If Not RecordSetData.EOF Then

If GetHeader = False Then
DestinationRange.Cells(1, 1).CopyFromRecordset RecordSetData
Else
'Add the GetHeader cell in each column if the last argument is
True
If UseHeaderRow Then
For LoopCounter = 0 To RecordSetData.Fields.Count - 1
DestinationRange.Cells(1, 1 + LoopCounter).Value =
RecordSetData.Fields(LoopCounter).Name
Next LoopCounter
DestinationRange.Cells(2, 1).CopyFromRecordset RecordSetData
Else
DestinationRange.Cells(1, 1).CopyFromRecordset RecordSetData
End If
End If

Else
MsgBox "No records returned from : " & SourceFile, vbCritical
End If

' Clean up our Recordset object.
RecordSetData.Close
Set RecordSetData = Nothing
Exit Sub

ErrorHandler:
MsgBox "FPE Tool Error: The file name, Sheet name or Range is not valid
of : " & SourceFile, _
vbExclamation, "Error"
On Error GoTo 0
End Sub


I have changed the format of the closed workbook text to "General" then to
"Text" but it doesn't make any difference. The ADO call will not pull text
for what ever reason. Curious though, it will pull the first row if you set
getheader and useheader to true but concatinates sequential integers on the
end of the second column (i.e., name, name1, name2, .... name256). Perhaps
someone can help me understand what is not right or what I need to change to
get an entire set of data that includes all of the information stored in the
closed workbook. BTW, this call works terrific on the local network and even
acceptable accross a VPN interface.

Sincerely,

Bryan44
 
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
LINKEDRANGE function - a complement to the PULL function (for getting values from a closed workbook) [email protected] Excel Worksheet Functions 0 September 5th 06 03:44 PM
CopyFromRecordset will fail if the recordset array data such as hierarchical recordsets ??? Mike[_108_] Excel Programming 0 June 10th 06 03:05 PM
Data from closed workbooks (pull func, indirect.ext, etc ....) [email protected] Excel Worksheet Functions 1 June 22nd 05 03:24 PM
how to create ado recordsets from rows of data? No Name Excel Programming 1 January 22nd 04 05:36 PM
Pull Data from a closed workbook, without opening it. Joe B.[_3_] Excel Programming 7 December 31st 03 05:06 PM


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