Erlandsen Import from closed wbk (ADO)

G

Guest

Hi,
I have tried all versions of Mr. Erlandsen's examples (Import data from a
closed workbook using ADO) and found none that will work in my case.
1- the workbooks I have to import from contain one single sheet with a named
range that incudes all the data but with no column (fields names) headers.
2- the workbooks are all password protected.
For the field names, i loose the 1rst row of data and for the password, I
get to type it for every workbook.
Does any one know a way out of this problem. Any suggestion?
Thanks.
 
G

Guest

Hi Ron,
Your examples are quite useful and I did checked them out before posting my
questions. I downloaded your code file and found it very interesting. But
unfortunately, all your examples open the source workbook which is what I try
to avoid.
Mr. Erlandsen has many examples which use ADO and copy (or transfer) the
data without opening the workbooks. Unfortunately again, all of them suppose
that the fields names are present on the first data row.
I tried to change the code in different ways but nothing works. I either
loose the 1st row of data or get that row as field names.
Here is some code that I adapted from one version of his examples:
' **************************************************
Sub DoGetData()
' Jac Tremblay - 2007-02-09
Dim strFile As String
Dim strRange As String
strFile = " C:\Data.xls"
strRange = "BdData"
GetDataFromClosedWorkbook strFile, strRange, Range("A1"), False
End Sub
' **************************************************
' If you want to import a lot of data from a closed workbook you can
' do this with ADO and the macro below. If you want to retrieve data
' from another worksheet than the first worksheet in the closed workbook,
' you have to refer to a user defined named range. The macro below can
' be used like this (in Excel 2000 or later):
' GetDataFromClosedWorkbook "C:\FolderName\WorkbookName.xls", _
' "A1:B21", Active - Cell, False
' GetDataFromClosedWorkbook "C:\FolderName\WorkbookName.xls", _
' "MyDataRange", Range("B3"), True
' **************************************************
Sub GetDataFromClosedWorkbook(SourceFile As String, _
SourceRange As String, TargetRange As Range, _
IncludeFieldNames As Boolean)
' Requires a reference to the Microsoft ActiveX Data Objects library.
' If SourceRange is a range reference:
' this will return data from the first worksheet in SourceFile.
' If SourceRange is a defined name reference:
' this will return data from any worksheet in SourceFile.
' SourceRange must include the range headers.
'
Dim dbConnection As ADODB.Connection, rs As ADODB.Recordset
Dim dbConnectionString As String
Dim TargetCell As Range, i As Integer
dbConnectionString = "DRIVER={Microsoft Excel Driver (*.xls)};" & _
"ReadOnly=1;DBQ=" & SourceFile
Set dbConnection = New ADODB.Connection
On Error GoTo InvalidInput
dbConnection.Open dbConnectionString ' Open the database connection.
Set rs = dbConnection.Execute("[" & SourceRange & "]")
Set TargetCell = TargetRange.Cells(1, 1)
If IncludeFieldNames Then
For i = 0 To rs.Fields.Count - 1
TargetCell.Offset(0, i).Formula = rs.Fields(i).Name
Next i
Set TargetCell = TargetCell.Offset(1, 0)
End If
TargetCell.CopyFromRecordset rs
rs.Close
dbConnection.Close ' Close the database connection.
Set TargetCell = Nothing
Set rs = Nothing
Set dbConnection = Nothing
On Error GoTo 0
Exit Sub
InvalidInput:
MsgBox "The source file or source range is invalid!", _
vbExclamation, "Get data from closed workbook"
End Sub
' **************************************************
I got the code from
http://www.erlandsendata.no/english/index.php?d=envbadacimportwbado
Thank you Ron for your reply. I still hope to find an answer to my problem.
When I do, I will share it with the community.
 
R

Ron de Bruin

Hi Jac

Opening the workbooks give you more control than using ADO.
My ADO code will work but not with protected workbooks.




--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


Jac Tremblay said:
Hi Ron,
Your examples are quite useful and I did checked them out before posting my
questions. I downloaded your code file and found it very interesting. But
unfortunately, all your examples open the source workbook which is what I try
to avoid.
Mr. Erlandsen has many examples which use ADO and copy (or transfer) the
data without opening the workbooks. Unfortunately again, all of them suppose
that the fields names are present on the first data row.
I tried to change the code in different ways but nothing works. I either
loose the 1st row of data or get that row as field names.
Here is some code that I adapted from one version of his examples:
' **************************************************
Sub DoGetData()
' Jac Tremblay - 2007-02-09
Dim strFile As String
Dim strRange As String
strFile = " C:\Data.xls"
strRange = "BdData"
GetDataFromClosedWorkbook strFile, strRange, Range("A1"), False
End Sub
' **************************************************
' If you want to import a lot of data from a closed workbook you can
' do this with ADO and the macro below. If you want to retrieve data
' from another worksheet than the first worksheet in the closed workbook,
' you have to refer to a user defined named range. The macro below can
' be used like this (in Excel 2000 or later):
' GetDataFromClosedWorkbook "C:\FolderName\WorkbookName.xls", _
' "A1:B21", Active - Cell, False
' GetDataFromClosedWorkbook "C:\FolderName\WorkbookName.xls", _
' "MyDataRange", Range("B3"), True
' **************************************************
Sub GetDataFromClosedWorkbook(SourceFile As String, _
SourceRange As String, TargetRange As Range, _
IncludeFieldNames As Boolean)
' Requires a reference to the Microsoft ActiveX Data Objects library.
' If SourceRange is a range reference:
' this will return data from the first worksheet in SourceFile.
' If SourceRange is a defined name reference:
' this will return data from any worksheet in SourceFile.
' SourceRange must include the range headers.
'
Dim dbConnection As ADODB.Connection, rs As ADODB.Recordset
Dim dbConnectionString As String
Dim TargetCell As Range, i As Integer
dbConnectionString = "DRIVER={Microsoft Excel Driver (*.xls)};" & _
"ReadOnly=1;DBQ=" & SourceFile
Set dbConnection = New ADODB.Connection
On Error GoTo InvalidInput
dbConnection.Open dbConnectionString ' Open the database connection.
Set rs = dbConnection.Execute("[" & SourceRange & "]")
Set TargetCell = TargetRange.Cells(1, 1)
If IncludeFieldNames Then
For i = 0 To rs.Fields.Count - 1
TargetCell.Offset(0, i).Formula = rs.Fields(i).Name
Next i
Set TargetCell = TargetCell.Offset(1, 0)
End If
TargetCell.CopyFromRecordset rs
rs.Close
dbConnection.Close ' Close the database connection.
Set TargetCell = Nothing
Set rs = Nothing
Set dbConnection = Nothing
On Error GoTo 0
Exit Sub
InvalidInput:
MsgBox "The source file or source range is invalid!", _
vbExclamation, "Get data from closed workbook"
End Sub
' **************************************************
I got the code from
http://www.erlandsendata.no/english/index.php?d=envbadacimportwbado
Thank you Ron for your reply. I still hope to find an answer to my problem.
When I do, I will share it with the community.
 
G

Guest

Hi again Ron,
My problem is that I have very many files to open on a WAN and these open
operations take a very long time. I am trying to find a simple solution to
that problem which I did not create.
If it weren't for that, I sure would open the workbooks and do whatever I
need.
Thanks for your answer and your time.
--
Jac Tremblay


Ron de Bruin said:
Hi Jac

Opening the workbooks give you more control than using ADO.
My ADO code will work but not with protected workbooks.




--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


Jac Tremblay said:
Hi Ron,
Your examples are quite useful and I did checked them out before posting my
questions. I downloaded your code file and found it very interesting. But
unfortunately, all your examples open the source workbook which is what I try
to avoid.
Mr. Erlandsen has many examples which use ADO and copy (or transfer) the
data without opening the workbooks. Unfortunately again, all of them suppose
that the fields names are present on the first data row.
I tried to change the code in different ways but nothing works. I either
loose the 1st row of data or get that row as field names.
Here is some code that I adapted from one version of his examples:
' **************************************************
Sub DoGetData()
' Jac Tremblay - 2007-02-09
Dim strFile As String
Dim strRange As String
strFile = " C:\Data.xls"
strRange = "BdData"
GetDataFromClosedWorkbook strFile, strRange, Range("A1"), False
End Sub
' **************************************************
' If you want to import a lot of data from a closed workbook you can
' do this with ADO and the macro below. If you want to retrieve data
' from another worksheet than the first worksheet in the closed workbook,
' you have to refer to a user defined named range. The macro below can
' be used like this (in Excel 2000 or later):
' GetDataFromClosedWorkbook "C:\FolderName\WorkbookName.xls", _
' "A1:B21", Active - Cell, False
' GetDataFromClosedWorkbook "C:\FolderName\WorkbookName.xls", _
' "MyDataRange", Range("B3"), True
' **************************************************
Sub GetDataFromClosedWorkbook(SourceFile As String, _
SourceRange As String, TargetRange As Range, _
IncludeFieldNames As Boolean)
' Requires a reference to the Microsoft ActiveX Data Objects library.
' If SourceRange is a range reference:
' this will return data from the first worksheet in SourceFile.
' If SourceRange is a defined name reference:
' this will return data from any worksheet in SourceFile.
' SourceRange must include the range headers.
'
Dim dbConnection As ADODB.Connection, rs As ADODB.Recordset
Dim dbConnectionString As String
Dim TargetCell As Range, i As Integer
dbConnectionString = "DRIVER={Microsoft Excel Driver (*.xls)};" & _
"ReadOnly=1;DBQ=" & SourceFile
Set dbConnection = New ADODB.Connection
On Error GoTo InvalidInput
dbConnection.Open dbConnectionString ' Open the database connection.
Set rs = dbConnection.Execute("[" & SourceRange & "]")
Set TargetCell = TargetRange.Cells(1, 1)
If IncludeFieldNames Then
For i = 0 To rs.Fields.Count - 1
TargetCell.Offset(0, i).Formula = rs.Fields(i).Name
Next i
Set TargetCell = TargetCell.Offset(1, 0)
End If
TargetCell.CopyFromRecordset rs
rs.Close
dbConnection.Close ' Close the database connection.
Set TargetCell = Nothing
Set rs = Nothing
Set dbConnection = Nothing
On Error GoTo 0
Exit Sub
InvalidInput:
MsgBox "The source file or source range is invalid!", _
vbExclamation, "Get data from closed workbook"
End Sub
' **************************************************
I got the code from
http://www.erlandsendata.no/english/index.php?d=envbadacimportwbado
Thank you Ron for your reply. I still hope to find an answer to my problem.
When I do, I will share it with the community.
--
Jac Tremblay


Ron de Bruin said:
Hi Jac

I have info here but you have the same problem then with passwords
http://www.rondebruin.nl/ado.htm

Open the files and copy

Read the Tips
http://www.rondebruin.nl/copy3.htm


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


Hi,
I have tried all versions of Mr. Erlandsen's examples (Import data from a
closed workbook using ADO) and found none that will work in my case.
1- the workbooks I have to import from contain one single sheet with a named
range that incudes all the data but with no column (fields names) headers.
2- the workbooks are all password protected.
For the field names, i loose the 1rst row of data and for the password, I
get to type it for every workbook.
Does any one know a way out of this problem. Any suggestion?
Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top