Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Help with converting code - import 65536 from Access

Hi,

Finally after a lot of searching I've been able to find this code. I pasted
it into Access and got it to work (exporting from Access to a new Excel
workbook) however my real requirement is to have an Excel macro initiate the
importing of the data.

The reason for this is the end users using the data have no Access knowledge
(I have some).

You can assume:

The name of the workbook is Excel_Test.xls,
The Access database is Source_Data.mdb, and
The Access table name is tbl_Comm_Data.


Sub foobar()
Dim rs As ADODB.Recordset
Dim exApp As Excel.Application, exWB As Excel.Workbook
Dim i As Long, j As Long, tmpQuo As Currency, startPos As Long, recCount As
Long
Dim fldArr() As String, varArr() As Variant, tmpArr() As Variant
Dim tmpBool As Boolean
Const maxRows As Long = 65000
Set rs = New ADODB.Recordset
rs.Open "Select * From tbl_Comm_Data WHERE DEPT_NO = '902'",
CodeProject.Connection, _
adOpenStatic, adLockReadOnly
With rs
If Not .EOF Then
Set exApp = New Excel.Application
Set exWB = exApp.Workbooks.Add(1)
Else: .Close: Set rs = Nothing
Exit Sub
End If
ReDim fldArr(0 To .Fields.Count - 1)
For i = LBound(fldArr) To UBound(fldArr)
Let fldArr(i) = .Fields(i).Name
Next

Let recCount = .RecordCount

If recCount <= maxRows Then
With exWB.Worksheets(1)
Let .Range("a1").Resize(, UBound(fldArr) + 1).Value = fldArr
..Range("a2").CopyFromRecordset rs
End With
Else: Let tmpBool = True
Let varArr = rs.GetRows
End If

..Close: Set rs = Nothing
End With
If tmpBool Then
Let tmpQuo = recCount / maxRows

If Int(tmpQuo) = tmpQuo Then
Let j = tmpQuo
Else: Let j = Int(tmpQuo) + 1
End If

With exWB.Worksheets
For i = 1 To j
If i 1 Then .Add after:=.Item(i - 1)
Let startPos = (i - 1) * maxRows + 1
Let tmpArr = TransposeDim(varArr, startPos, maxRows - 1)
With .Item(i)
Let .Range("a1").Resize(, UBound(fldArr) + 1).Value = fldArr
Let .Range("a2").Resize(UBound(tmpArr, 1) + 1, _
UBound(tmpArr, 2) + 1).Value = tmpArr
End With
Next
exApp.Goto .Item(1).Range("a1")
End With
End If
'close and save
exApp.DisplayAlerts = False
exWB.Close True, "T:\foobar.xls"
Set exWB = Nothing
exApp.DisplayAlerts = True
exApp.Quit: Set exApp = Nothing
MsgBox "Ta da"
End Sub
Function TransposeDim( _
ByRef v() As Variant, _
Optional ByRef custStart As Long = 1, _
Optional ByRef custEnd As Long = 65535) As Variant
' Custom Function to Transpose a 0-based array (v) (MSDN)
' Crop-Functionality and Row-Cap Mods by Nate Oliver
Dim X As Long, Y As Long, custUbound As Long
Dim tmpArr() As Variant
Let custUbound = UBound(v, 2) - custStart + 1
If custUbound custEnd Then Let custUbound = custEnd
ReDim tmpArr(0 To custUbound, 0 To UBound(v, 1))
For X = LBound(tmpArr, 1) To UBound(tmpArr, 1)
For Y = LBound(tmpArr, 2) To UBound(tmpArr, 2)
Let tmpArr(X, Y) = v(Y, X + custStart - 1)
Next Y
Next X
Let TransposeDim = tmpArr
End Function

Andrew
251108


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 358
Default Help with converting code - import 65536 from Access

Hi all,

This question hasn't had any posts to it since it was first raised. can
anyone assist me?


--
Andrew


"Andrew @ CrazyCritters" wrote:

Hi,

Finally after a lot of searching I've been able to find this code. I pasted
it into Access and got it to work (exporting from Access to a new Excel
workbook) however my real requirement is to have an Excel macro initiate the
importing of the data.

The reason for this is the end users using the data have no Access knowledge
(I have some).

You can assume:

The name of the workbook is Excel_Test.xls,
The Access database is Source_Data.mdb, and
The Access table name is tbl_Comm_Data.


Sub foobar()
Dim rs As ADODB.Recordset
Dim exApp As Excel.Application, exWB As Excel.Workbook
Dim i As Long, j As Long, tmpQuo As Currency, startPos As Long, recCount As
Long
Dim fldArr() As String, varArr() As Variant, tmpArr() As Variant
Dim tmpBool As Boolean
Const maxRows As Long = 65000
Set rs = New ADODB.Recordset
rs.Open "Select * From tbl_Comm_Data WHERE DEPT_NO = '902'",
CodeProject.Connection, _
adOpenStatic, adLockReadOnly
With rs
If Not .EOF Then
Set exApp = New Excel.Application
Set exWB = exApp.Workbooks.Add(1)
Else: .Close: Set rs = Nothing
Exit Sub
End If
ReDim fldArr(0 To .Fields.Count - 1)
For i = LBound(fldArr) To UBound(fldArr)
Let fldArr(i) = .Fields(i).Name
Next

Let recCount = .RecordCount

If recCount <= maxRows Then
With exWB.Worksheets(1)
Let .Range("a1").Resize(, UBound(fldArr) + 1).Value = fldArr
.Range("a2").CopyFromRecordset rs
End With
Else: Let tmpBool = True
Let varArr = rs.GetRows
End If

.Close: Set rs = Nothing
End With
If tmpBool Then
Let tmpQuo = recCount / maxRows

If Int(tmpQuo) = tmpQuo Then
Let j = tmpQuo
Else: Let j = Int(tmpQuo) + 1
End If

With exWB.Worksheets
For i = 1 To j
If i 1 Then .Add after:=.Item(i - 1)
Let startPos = (i - 1) * maxRows + 1
Let tmpArr = TransposeDim(varArr, startPos, maxRows - 1)
With .Item(i)
Let .Range("a1").Resize(, UBound(fldArr) + 1).Value = fldArr
Let .Range("a2").Resize(UBound(tmpArr, 1) + 1, _
UBound(tmpArr, 2) + 1).Value = tmpArr
End With
Next
exApp.Goto .Item(1).Range("a1")
End With
End If
'close and save
exApp.DisplayAlerts = False
exWB.Close True, "T:\foobar.xls"
Set exWB = Nothing
exApp.DisplayAlerts = True
exApp.Quit: Set exApp = Nothing
MsgBox "Ta da"
End Sub
Function TransposeDim( _
ByRef v() As Variant, _
Optional ByRef custStart As Long = 1, _
Optional ByRef custEnd As Long = 65535) As Variant
' Custom Function to Transpose a 0-based array (v) (MSDN)
' Crop-Functionality and Row-Cap Mods by Nate Oliver
Dim X As Long, Y As Long, custUbound As Long
Dim tmpArr() As Variant
Let custUbound = UBound(v, 2) - custStart + 1
If custUbound custEnd Then Let custUbound = custEnd
ReDim tmpArr(0 To custUbound, 0 To UBound(v, 1))
For X = LBound(tmpArr, 1) To UBound(tmpArr, 1)
For Y = LBound(tmpArr, 2) To UBound(tmpArr, 2)
Let tmpArr(X, Y) = v(Y, X + custStart - 1)
Next Y
Next X
Let TransposeDim = tmpArr
End Function

Andrew
251108


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
how do I import a CSV file with more than 65536 rows Steve Excel Programming 9 July 14th 06 03:47 PM
import tekst 65536 rows Reniek Excel Programming 3 May 22nd 05 10:47 PM
Import extrenal data exceeds 65536 TK Excel Programming 9 November 29th 04 08:10 AM
Import extrenal data exceeds 65536 Tim Williams Excel Programming 0 November 24th 04 02:18 AM
VBA code, Import data from Access Jason Frazer Excel Programming 3 March 2nd 04 11:48 AM


All times are GMT +1. The time now is 09:31 AM.

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"