ADO recordset

K

Kirk P.

With considerable help from this DG, I've written this code that successfully
returns records from an Access db to Excel.

Sub Test()

Dim cn As ADODB.Connection
Dim rec As ADODB.Recordset
Dim nCol As Integer

Set cn = New Connection
Set rec = New Recordset

'Open the connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\\oprdgv1\depart\Finance\" & _
"_Health Solutions Group\Customer\Customer.mdb" & ";"

'Open the recordset
rec.Open "SELECT * FROM tblTESTING" & ";", cn, adOpenForwardOnly,
adLockOptimistic

'Returns recordset to Excel, including the header record
For nCol = 1 To rec.Fields.Count
Sheets("Sheet1").Cells(1, nCol).Value = rec.Fields(nCol - 1).Name
Next nCol
Sheets("Sheet1").Cells(2, 1).CopyFromRecordset rec
Set rec = Nothing
Set cn = Nothing

End Sub

What I REALLY want to do now is use a "virtual recordset" as an argument in
another Excel function. Because the set of data I have exceeds Excels row
limitations, I have the data stored in Access, but need to use that data as
an argument in Excel's LINEST function. How would I proceed with this?
 
M

merjet

I have Excel 2002. In VBA the LINEST function will not handle an array
with more rows than 65536. So it seems you need Excel 2007 or you
settle for using only 65536 rows of data.

Merjet
 
J

JMay

Here's code that you might be able to use. It is self
explanatory - Beats having to buy xl2007 for the 65536+ rows
HTH

Sub LargeFileImport()
'Bernie Deitrick's code for opening vary large text files in Excel - more
than 65536 ' rows
'Dimension Variables
Dim ResultStr As String
Dim FileName As String
Dim FileNum As Integer
Dim Counter As Double
'Ask User for File's Name
FileName = Application.GetOpenFilename
'Check for no entry
If FileName = "" Then End
'Get Next Available File Handle Number
FileNum = FreeFile()
'Open Text File For Input
Open FileName For Input As #FileNum
'Turn Screen Updating Off
Application.ScreenUpdating = False
'Create A New WorkBook With One Worksheet In It
Workbooks.Add Template:=xlWorksheet
'Set The Counter to 1
Counter = 1
'Loop Until the End Of File Is Reached
Do While Seek(FileNum) <= LOF(FileNum)
'Display Importing Row Number On Status Bar
Application.StatusBar = "Importing Row " & Counter & " of text file " _
& FileName
'Store One Line Of Text From File To Variable
Line Input #FileNum, ResultStr
'Store Variable Data Into Active Cell
If Left(ResultStr, 1) = "=" Then
ActiveCell.Value = "'" & ResultStr
Else
ActiveCell.Value = ResultStr
End If
If ActiveCell.Row = 65536 Then
'If On The Last Row Then Add A New Sheet
ActiveWorkbook.Sheets.Add
Else
'If Not The Last Row Then Go One Cell Down
ActiveCell.Offset(1, 0).Select
End If
'Increment the Counter By 1
Counter = Counter + 1
'Start Again At Top Of 'Do While' Statement
Loop
'Close The Open Text File
Close
'Remove Message From Status Bar
Application.StatusBar = False
End Sub
 

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