Isskint
Slowly Developing
- Local time
- Today, 05:02
- Joined
- Apr 25, 2012
- Messages
- 1,302
Well i had a few minutes spare, so i took some of Bobs code (many thanks Bob) and adapted it to a situation in this post http://www.access-programmers.co.uk/forums/showthread.php?t=236372. What i have is a generic procedure that can be called by supplying the name of the table or query you wish to save and the field name you wish to use as tab names on the spreadsheet.
This code ran sucessfully a dozen times then tripped at the line in red. the error is Method 'Worksheets' of object '_Global' failed.
I have run it sucessfully since but it does sometimes trip up still and I have NO IDEA WHY:banghead:. Perhaps someone can identify why it should trip up on the Add sheet line 'whenever the mood takes it'.
I don't want to post it to the OP on the other thread until the bug if sorted.
Code:
Public Sub TransferByTab(sFormName As String, sKeyonField As String)
'Based on some code from Bob Larson
'at [URL="http://www.btabdevelopment.com/ts/default.aspx?PageId=10"][COLOR=#810081]http://www.btabdevelopment.com/ts/default.aspx?PageId=10[/COLOR][/URL]
'Many thanks Bob
Dim rst As DAO.Recordset
Dim ApXL As Object
Dim xlWBk As Object
Dim xlWSh As Object
Dim fld As DAO.Field
Dim oLP, xlNxtRw As Integer
Dim xlFound As Boolean
Const xlCenter As Long = -4108
Const xlBottom As Long = -4107
On Error GoTo err_handler
Set rst = CurrentDb.OpenRecordset(sFormName)
Set ApXL = CreateObject("Excel.Application")
Set xlWBk = ApXL.Workbooks.Add
ApXL.Visible = True
'delete all but sheet1
For oLP = 1 To xlWBk.Sheets.Count - 1
xlWBk.Worksheets(xlWBk.Sheets.Count).Delete
Next oLP
rst.MoveFirst
'set name and column headings for first sheet
xlWBk.Worksheets(1).Name = rst(sKeyonField)
For Each fld In rst.Fields
If fld.Name <> sKeyonField Then ApXL.ActiveCell = fld.Name
ApXL.ActiveCell.Offset(0, 1).Select
Next
While Not rst.EOF
xlFound = False 'boolean flag indicates named sheet found
For oLP = 1 To xlWBk.Sheets.Count
If xlWBk.Worksheets(oLP).Name = rst(sKeyonField) Then
Set xlWSh = xlWBk.Worksheets(oLP) 'set reference to this worksheet
xlWSh.Select
xlFound = True
End If
Next oLP
If xlFound = False Then 'no sheet with name of current records sKeyonField
[COLOR=red][B]xlWBk.Worksheets.Add After:=Worksheets(xlWBk.Sheets.Count)[/B][/COLOR]
Set xlWSh = xlWBk.Worksheets(xlWBk.Sheets.Count)
xlWSh.Name = rst(sKeyonField)
For Each fld In rst.Fields
If fld.Name <> sKeyonField Then ApXL.ActiveCell = fld.Name
ApXL.ActiveCell.Offset(0, 1).Select
Next
xlFound = True
End If
xlNxtRw = ApXL.ActiveSheet.UsedRange.Rows.Count + 1 'next empty row on selected sheet
oLP = 1
For Each fld In rst.Fields
If fld.Name <> sKeyonField Then
ApXL.Cells(xlNxtRw, oLP) = rst(fld.Name)
oLP = oLP + 1
End If
Next
rst.MoveNext
Wend
rst.Close
Set rst = Nothing
Exit Sub
err_handler:
DoCmd.SetWarnings True
MsgBox Err.Description, vbExclamation, Err.Number
Exit Sub
End Sub
This code ran sucessfully a dozen times then tripped at the line in red. the error is Method 'Worksheets' of object '_Global' failed.
I have run it sucessfully since but it does sometimes trip up still and I have NO IDEA WHY:banghead:. Perhaps someone can identify why it should trip up on the Add sheet line 'whenever the mood takes it'.