OK... I'm not VBA illiterate, but I'm a BA trying to maintain a
code-heavy Access 2002 (XP OS) front-end attached to Oracle tables. I
have an export to Excel button that worked before the SP2 upgrade, and
didn't work afterwards. Research shows me the upgrade caused a problem
with a memo field in the export, causing the CopyFromRecordset of
object Range error. I found code that is supposed to fix it, but it
either doesn't work, or I'm not using it correctly. Here is the code I
started with:
Function exportVarianceExplanations()
Dim filename As String
Dim directory As String
Dim filepath As String
Dim i As Integer
Dim RS As ADODB.Recordset
Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Dim intMaxCol As Integer
Dim intMaxRow As Integer
Call progressform("open", "Connecting to database...", 0, _
DIALOG_TITLE, "accessdb")
ConnectSource "EXPNSUSR.VAREXPLNREPORT", "varexplnreport"
filename = "Variance Explanations " &
getApplicationVariable("varexplnmonth") & _
getApplicationVariable("varexplnyear")
directory = GetSpecialfolder(CSIDL_PERSONAL)
filepath = directory & filename & ".xls"
i = 0
Do Until Dir(filepath) = ""
i = i + 1
filepath = GetSpecialfolder(CSIDL_PERSONAL) & filename & " " &
i & ".xls"
Loop
filename = filepath
Call progressform("close", "", 0, _
DIALOG_TITLE, "")
Call progressform("open", "Retrieving variance explanations...", 0,
_
DIALOG_TITLE, "extractrecords")
SQL = "SELECT * FROM VAREXPLNREPORT "
If intRole <> 6 And intRole <> 2 Then
SQL = SQL & "WHERE BUDGET_CENTER IN (" & _
"SELECT fldBudgetCenter FROM tblRightsBudgetCenter " & _
"WHERE fldUserName = '" & strUserName & "')"
End If
Debug.Print SQL
Set RS = New ADODB.Recordset
RS.Open (SQL), CurrentProject.Connection, adOpenStatic,
adLockReadOnly, adAsyncFetch
Set objXL = CreateObject("Excel.Application")
With objXL
.Visible = False
Set objWkb = .Workbooks.Add
RS.MoveLast
intMaxRow = RS.AbsolutePosition
RS.MoveFirst
intMaxCol = RS.Fields.Count
Call progressform("close", "", 0, DIALOG_TITLE, "")
Call progressform("open", RS.RecordCount & " records
retrieved...", 500, _
DIALOG_TITLE, "exceltransfer")
Set objSht = objWkb.Worksheets.Add
objSht.Name = "Variance Explanations"
Call progressform("other", "Transferring records to Excel
worksheet...", _
1500, DIALOG_TITLE, "exceltransfer")
With objSht
For i = 1 To intMaxCol
.Cells(1, i).Value = RS.Fields(i - 1).Name
Next i
*** Debug brings me to the line below***
.Range(.Cells(2, 1), .Cells(intMaxRow,
intMaxCol)).CopyFromRecordset RS
End With
objSht.Rows("1:1").Select
With .selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.ShrinkToFit = False
.MergeCells = False
End With
.ActiveSheet.Range(.Cells(1, 1), .Cells(1,
intMaxCol)).Select
Call progressform("other", "Formatting Excel worksheet...",
1500, _
DIALOG_TITLE, "exceltransfer")
With .selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
With .selection.Interior
.ColorIndex = 15
.Pattern = xlSolid
End With
.ActiveSheet.Columns("K:M").Select
.selection.NumberFormat = "#,##0.00_);[Red](#,##0.00)"
.ActiveSheet.Columns("I:I").Select
.selection.ColumnWidth = 55
With .selection
.WrapText = True
.Orientation = 0
.ShrinkToFit = False
.MergeCells = False
End With
.ActiveSheet.Columns("J:J").Select
.selection.NumberFormat = "mm/dd/yyyy"
.ActiveSheet.Cells(1, 1).Select
.ActiveSheet.Cells.Select
With .selection.Font
.Name = "Arial"
.Size = 8
End With
.selection.VerticalAlignment = xlTop
.selection.Columns.AutoFit
End With
Call progressform("other", "Saving File...", 1500, _
DIALOG_TITLE, "exceltransfer")
objXL.Application.DisplayAlerts = False
For Each objSht In objWkb.Sheets
If objSht.Name Like "*Sheet*" Then
objSht.Delete
End If
Next
objXL.Application.DisplayAlerts = True
objWkb.SaveAs filepath, xlWorkbookNormal, , , , , xlNoChange, ,
True
objXL.Quit
Set objSht = Nothing
Set objWkb = Nothing
Set objXL = Nothing
RemoveSource "varexplnreport"
Call progressform("close", "Query results export completed.", _
0, DIALOG_TITLE, "exceltransfer")
MsgBox "Your file has been exported to " & filepath & ".",
vbInformation, DIALOG_TITLE
End Function
Code I found for the "fix", with my modifications included:
i = 1
For Each RS In RS.Fields
objSht.Cells(2, i).Value = RS.Name
i = i + 1
Next RS
Dim j As Long, k As Long
With objSht
For j = 1 To RS.RecordCount
For k = 1 To RS.Fields.Count
If IsNull(RS(k - 1)) Then
.Cells(j + 2, k) = Empty
Else
If Len(RS(k - 1)) > 255 Then
For i = 0 To Int(Len(RS(k - 1)) / 255)
.Cells(j + 2, k).Value = .Cells(j + 2,
k).Value & Mid(RS(k - 1),
(i * 255) + 1, 255)
Next i
Else
.Cells(j + 2, k).Value = RS(k - 1)
End If
End If
Next k
RS.MoveNext
Next j
End With
ARGH! Help, PLEASE! Bypassing the memo field is NOT an option. It is
the main reason for the export.
Thanks,
zhollywood 2 4660
An option that has value when using Excel, given its character
limitations, is to convert the output to string using
cstr([MemoFieldName]).
Another option, if the data set is small enough, is to loop through the
recordset, pasting each Access 'cell' into each Excel cell, via code,
although the performance would be intolerable with large recordsets.
James Igoe ja********@gmail.com || http://code.comparative-advantage.com
zhollywood wrote: OK... I'm not VBA illiterate, but I'm a BA trying to maintain a code-heavy Access 2002 (XP OS) front-end attached to Oracle tables. I have an export to Excel button that worked before the SP2 upgrade, and didn't work afterwards. Research shows me the upgrade caused a problem with a memo field in the export, causing the CopyFromRecordset of object Range error. I found code that is supposed to fix it, but it either doesn't work, or I'm not using it correctly. Here is the code I started with:
Function exportVarianceExplanations()
Dim filename As String Dim directory As String Dim filepath As String Dim i As Integer
Dim RS As ADODB.Recordset Dim objXL As Excel.Application Dim objWkb As Excel.Workbook Dim objSht As Excel.Worksheet
Dim intMaxCol As Integer Dim intMaxRow As Integer
Call progressform("open", "Connecting to database...", 0, _ DIALOG_TITLE, "accessdb")
ConnectSource "EXPNSUSR.VAREXPLNREPORT", "varexplnreport"
filename = "Variance Explanations " & getApplicationVariable("varexplnmonth") & _ getApplicationVariable("varexplnyear") directory = GetSpecialfolder(CSIDL_PERSONAL)
filepath = directory & filename & ".xls"
i = 0 Do Until Dir(filepath) = "" i = i + 1 filepath = GetSpecialfolder(CSIDL_PERSONAL) & filename & " " & i & ".xls" Loop
filename = filepath
Call progressform("close", "", 0, _ DIALOG_TITLE, "")
Call progressform("open", "Retrieving variance explanations...", 0, _ DIALOG_TITLE, "extractrecords")
SQL = "SELECT * FROM VAREXPLNREPORT "
If intRole <> 6 And intRole <> 2 Then SQL = SQL & "WHERE BUDGET_CENTER IN (" & _ "SELECT fldBudgetCenter FROM tblRightsBudgetCenter " & _ "WHERE fldUserName = '" & strUserName & "')" End If
Debug.Print SQL
Set RS = New ADODB.Recordset RS.Open (SQL), CurrentProject.Connection, adOpenStatic, adLockReadOnly, adAsyncFetch
Set objXL = CreateObject("Excel.Application")
With objXL .Visible = False Set objWkb = .Workbooks.Add
RS.MoveLast intMaxRow = RS.AbsolutePosition RS.MoveFirst intMaxCol = RS.Fields.Count
Call progressform("close", "", 0, DIALOG_TITLE, "")
Call progressform("open", RS.RecordCount & " records retrieved...", 500, _ DIALOG_TITLE, "exceltransfer")
Set objSht = objWkb.Worksheets.Add objSht.Name = "Variance Explanations"
Call progressform("other", "Transferring records to Excel worksheet...", _ 1500, DIALOG_TITLE, "exceltransfer")
With objSht For i = 1 To intMaxCol .Cells(1, i).Value = RS.Fields(i - 1).Name Next i
*** Debug brings me to the line below***
.Range(.Cells(2, 1), .Cells(intMaxRow, intMaxCol)).CopyFromRecordset RS
End With
objSht.Rows("1:1").Select
With .selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = False .Orientation = 0 .ShrinkToFit = False .MergeCells = False End With
.ActiveSheet.Range(.Cells(1, 1), .Cells(1, intMaxCol)).Select
Call progressform("other", "Formatting Excel worksheet...", 1500, _ DIALOG_TITLE, "exceltransfer")
With .selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter End With
With .selection.Interior .ColorIndex = 15 .Pattern = xlSolid End With
.ActiveSheet.Columns("K:M").Select .selection.NumberFormat = "#,##0.00_);[Red](#,##0.00)"
.ActiveSheet.Columns("I:I").Select .selection.ColumnWidth = 55 With .selection .WrapText = True .Orientation = 0 .ShrinkToFit = False .MergeCells = False End With
.ActiveSheet.Columns("J:J").Select .selection.NumberFormat = "mm/dd/yyyy"
.ActiveSheet.Cells(1, 1).Select
.ActiveSheet.Cells.Select With .selection.Font .Name = "Arial" .Size = 8 End With
.selection.VerticalAlignment = xlTop
.selection.Columns.AutoFit
End With
Call progressform("other", "Saving File...", 1500, _ DIALOG_TITLE, "exceltransfer")
objXL.Application.DisplayAlerts = False
For Each objSht In objWkb.Sheets If objSht.Name Like "*Sheet*" Then objSht.Delete End If Next
objXL.Application.DisplayAlerts = True
objWkb.SaveAs filepath, xlWorkbookNormal, , , , , xlNoChange, , True objXL.Quit Set objSht = Nothing Set objWkb = Nothing Set objXL = Nothing
RemoveSource "varexplnreport"
Call progressform("close", "Query results export completed.", _ 0, DIALOG_TITLE, "exceltransfer")
MsgBox "Your file has been exported to " & filepath & ".", vbInformation, DIALOG_TITLE
End Function
Code I found for the "fix", with my modifications included:
i = 1 For Each RS In RS.Fields objSht.Cells(2, i).Value = RS.Name i = i + 1 Next RS
Dim j As Long, k As Long
With objSht For j = 1 To RS.RecordCount For k = 1 To RS.Fields.Count If IsNull(RS(k - 1)) Then .Cells(j + 2, k) = Empty Else If Len(RS(k - 1)) > 255 Then For i = 0 To Int(Len(RS(k - 1)) / 255) .Cells(j + 2, k).Value = .Cells(j + 2, k).Value & Mid(RS(k - 1), (i * 255) + 1, 255) Next i Else .Cells(j + 2, k).Value = RS(k - 1) End If End If Next k RS.MoveNext Next j End With
ARGH! Help, PLEASE! Bypassing the memo field is NOT an option. It is the main reason for the export.
Thanks, zhollywood
I hate to sound stupid, but how would I incorporate your convert to
string suggestion? I might figure it out before you answer, but my
initial thought is, I have no idea what to do with it. LOL
Holly ja********@gmail.com wrote:
An option that has value when using Excel, given its character
limitations, is to convert the output to string using
cstr([MemoFieldName]).
Another option, if the data set is small enough, is to loop through the
recordset, pasting each Access 'cell' into each Excel cell, via code,
although the performance would be intolerable with large recordsets.
James Igoe
ja********@gmail.com || http://code.comparative-advantage.com
zhollywood wrote:
OK... I'm not VBA illiterate, but I'm a BA trying to maintain a
code-heavy Access 2002 (XP OS) front-end attached to Oracle tables. I
have an export to Excel button that worked before the SP2 upgrade, and
didn't work afterwards. Research shows me the upgrade caused a problem
with a memo field in the export, causing the CopyFromRecordset of
object Range error. I found code that is supposed to fix it, but it
either doesn't work, or I'm not using it correctly. Here is the code I
started with:
Function exportVarianceExplanations()
Dim filename As String
Dim directory As String
Dim filepath As String
Dim i As Integer
Dim RS As ADODB.Recordset
Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Dim intMaxCol As Integer
Dim intMaxRow As Integer
Call progressform("open", "Connecting to database...", 0, _
DIALOG_TITLE, "accessdb")
ConnectSource "EXPNSUSR.VAREXPLNREPORT", "varexplnreport"
filename = "Variance Explanations " &
getApplicationVariable("varexplnmonth") & _
getApplicationVariable("varexplnyear")
directory = GetSpecialfolder(CSIDL_PERSONAL)
filepath = directory & filename & ".xls"
i = 0
Do Until Dir(filepath) = ""
i = i + 1
filepath = GetSpecialfolder(CSIDL_PERSONAL) & filename & " " &
i & ".xls"
Loop
filename = filepath
Call progressform("close", "", 0, _
DIALOG_TITLE, "")
Call progressform("open", "Retrieving variance explanations...", 0,
_
DIALOG_TITLE, "extractrecords")
SQL = "SELECT * FROM VAREXPLNREPORT "
If intRole <6 And intRole <2 Then
SQL = SQL & "WHERE BUDGET_CENTER IN (" & _
"SELECT fldBudgetCenter FROM tblRightsBudgetCenter " & _
"WHERE fldUserName = '" & strUserName & "')"
End If
Debug.Print SQL
Set RS = New ADODB.Recordset
RS.Open (SQL), CurrentProject.Connection, adOpenStatic,
adLockReadOnly, adAsyncFetch
Set objXL = CreateObject("Excel.Application")
With objXL
.Visible = False
Set objWkb = .Workbooks.Add
RS.MoveLast
intMaxRow = RS.AbsolutePosition
RS.MoveFirst
intMaxCol = RS.Fields.Count
Call progressform("close", "", 0, DIALOG_TITLE, "")
Call progressform("open", RS.RecordCount & " records
retrieved...", 500, _
DIALOG_TITLE, "exceltransfer")
Set objSht = objWkb.Worksheets.Add
objSht.Name = "Variance Explanations"
Call progressform("other", "Transferring records to Excel
worksheet...", _
1500, DIALOG_TITLE, "exceltransfer")
With objSht
For i = 1 To intMaxCol
.Cells(1, i).Value = RS.Fields(i - 1).Name
Next i
*** Debug brings me to the line below***
.Range(.Cells(2, 1), .Cells(intMaxRow,
intMaxCol)).CopyFromRecordset RS
End With
objSht.Rows("1:1").Select
With .selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.ShrinkToFit = False
.MergeCells = False
End With
.ActiveSheet.Range(.Cells(1, 1), .Cells(1,
intMaxCol)).Select
Call progressform("other", "Formatting Excel worksheet...",
1500, _
DIALOG_TITLE, "exceltransfer")
With .selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
With .selection.Interior
.ColorIndex = 15
.Pattern = xlSolid
End With
.ActiveSheet.Columns("K:M").Select
.selection.NumberFormat = "#,##0.00_);[Red](#,##0.00)"
.ActiveSheet.Columns("I:I").Select
.selection.ColumnWidth = 55
With .selection
.WrapText = True
.Orientation = 0
.ShrinkToFit = False
.MergeCells = False
End With
.ActiveSheet.Columns("J:J").Select
.selection.NumberFormat = "mm/dd/yyyy"
.ActiveSheet.Cells(1, 1).Select
.ActiveSheet.Cells.Select
With .selection.Font
.Name = "Arial"
.Size = 8
End With
.selection.VerticalAlignment = xlTop
.selection.Columns.AutoFit
End With
Call progressform("other", "Saving File...", 1500, _
DIALOG_TITLE, "exceltransfer")
objXL.Application.DisplayAlerts = False
For Each objSht In objWkb.Sheets
If objSht.Name Like "*Sheet*" Then
objSht.Delete
End If
Next
objXL.Application.DisplayAlerts = True
objWkb.SaveAs filepath, xlWorkbookNormal, , , , , xlNoChange, ,
True
objXL.Quit
Set objSht = Nothing
Set objWkb = Nothing
Set objXL = Nothing
RemoveSource "varexplnreport"
Call progressform("close", "Query results export completed.", _
0, DIALOG_TITLE, "exceltransfer")
MsgBox "Your file has been exported to " & filepath & ".",
vbInformation, DIALOG_TITLE
End Function
Code I found for the "fix", with my modifications included:
i = 1
For Each RS In RS.Fields
objSht.Cells(2, i).Value = RS.Name
i = i + 1
Next RS
Dim j As Long, k As Long
With objSht
For j = 1 To RS.RecordCount
For k = 1 To RS.Fields.Count
If IsNull(RS(k - 1)) Then
.Cells(j + 2, k) = Empty
Else
If Len(RS(k - 1)) 255 Then
For i = 0 To Int(Len(RS(k - 1)) / 255)
.Cells(j + 2, k).Value = .Cells(j + 2,
k).Value & Mid(RS(k - 1),
(i * 255) + 1, 255)
Next i
Else
.Cells(j + 2, k).Value = RS(k - 1)
End If
End If
Next k
RS.MoveNext
Next j
End With
ARGH! Help, PLEASE! Bypassing the memo field is NOT an option. It is
the main reason for the export.
Thanks,
zhollywood
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ange T |
last post by:
Hi there,
I'm having pain with the VB behind an Access form. The form is used to
create reports in Excel based on the details entered in the form. This
has always worked without error on my...
|
by: Sarah |
last post by:
I know there have been plenty of posts online about this issue, but I
have yet to find a solution. I am desperate for a good answer.
The issue is: with newly-built PCs and, as it happens, our web...
|
by: al |
last post by:
Greetings,
I'm wondering if Excel object CopyFromRecordset is still supported in
VB.NET?? If not, what is the alternative, looping through dataset????
MTIA,
Grawsha
|
by: rudevincy |
last post by:
Hello I am new to VBA and I am trying to create this program however I get the run time error 3061 Too few parameter expected_1, what does this mean, my code is:
Sub...
|
by: cycnus |
last post by:
Does anyone else have the same issue?
I'm using Access 2007 and trying to export a DAO recordset to excel using
CopyFromRecordset but I systematically get a "Run-Time error 430, Class does
not...
| |
by: Cor Pruim |
last post by:
I have a very strange problem. I have written a Windows Service with VS2003 in vb.net. This service does some calculations and after that it needs to produce some Excel reports by getting data from...
|
by: il0postino |
last post by:
Apologies in advance for this newbie question!
I have an Access form with an unbound embedded Excel chart on it(Called, OLEUnbound39) (Done on Access form by Insert > Object > Microsoft excel...
|
by: mac6777 |
last post by:
I am having a problem with the CopyFromRecordset function in VBA Access. I am attempting to run VBA in an Access Database that copies query results into an Excel spreadsheet. The VBA opens an Excel...
|
by: Tempalli |
last post by:
I am exporting the data from ms access to excel where the error displays as Run-time error -2147467259(800004005)
Method 'Copyfromrecordset' of object 'Range' faild.
...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |