C
Cire via AccessMonster.com
Hi all, i've got a problem with the excel file which is created on a click of
a "export to excel" button. Apparently when i double click on it, it doesnt
launch but if i open it within excel i get a "file is Locked for editing"
msg and the options for Read only and Notify. but this is not what i want..i
used the copy from recordset method to export the query data to excel. the
query is a pass-through query, which i understand is read-only but i remember
being able to export and open the file without the "locked for editng"
message. so i'm kind of confused now..heres my code of the export to excel
button
Public mainqdf As DAO.QueryDef
Public maindb As DAO.Database
Public mainRst As DAO.Recordset
Private Sub cmdExpToExcel_Click()
'code behind command button "Export to Excel"
Dim lngMax As Long
Dim lngCount As Long
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Dim strFile As String
Set maindb = CurrentDb()
Set mainqdf = maindb.QueryDefs("qryCOSearch")
Set mainRst = mainqdf.OpenRecordset(dbOpenDynaset, dbEdit)
'all code below explains exporting the query results to excel
'allow user to choose path to save to
strFile = GetSaveFile_CLT("C:\", "Save this file as", "Untitled.
xls")
If Not strFile Like "*.xls" Then
MsgBox "you have saved in the wrong file format"
Exit Sub
End If
'defining the variables
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets.Add
'formatting cells in excel
With xlSheet
For Each Cell In xlSheet.Range("A1", "J1")
Cell.Font.Size = 10
Cell.Font.Name = "Arial"
Cell.Font.Bold = True
Cell.Interior.Color = rgb(204, 255, 255)
Cell.HorizontalAlignment = xlHAlignCenter
Cell.WrapText = True
Next
.Cells(1, 2).HorizontalAlignment = xlHAlignLeft
.Columns("A").ColumnWidth = 10
.Columns("B").ColumnWidth = 24
.Columns("C").ColumnWidth = 16
.Columns("C").HorizontalAlignment = xlHAlignLeft
.Columns("E").ColumnWidth = 30
.Columns("F").ColumnWidth = 20
.Columns("G").ColumnWidth = 8
.Columns("H").ColumnWidth = 32
.Columns("I:J").ColumnWidth = 24
.Rows(1).RowHeight = 16
End With
'copying the query results from the recordset to the
excel file
With xlSheet
.Name = "Results"
.UsedRange.ClearContents
lngMax = mainRst.Fields.Count
For lngCount = lngMax To 1 Step -1
.Cells(1, lngCount).Value = mainRst.Fields(lngCount -
1).Name
Next lngCount
.Range("A2").CopyFromRecordset mainRst
End With
lngMax = xlBook.Worksheets.Count
'deleting all other worksheets except for "Results"
For lngCount = lngMax To 1 Step -1
If xlBook.Worksheets(lngCount).Name <> "Results" Then
xlBook.Worksheets(lngCount).Delete
End If
Next lngCount
xlBook.SaveAs strFile
MsgBox "Export Completed", vbInformation
End Sub
a "export to excel" button. Apparently when i double click on it, it doesnt
launch but if i open it within excel i get a "file is Locked for editing"
msg and the options for Read only and Notify. but this is not what i want..i
used the copy from recordset method to export the query data to excel. the
query is a pass-through query, which i understand is read-only but i remember
being able to export and open the file without the "locked for editng"
message. so i'm kind of confused now..heres my code of the export to excel
button
Public mainqdf As DAO.QueryDef
Public maindb As DAO.Database
Public mainRst As DAO.Recordset
Private Sub cmdExpToExcel_Click()
'code behind command button "Export to Excel"
Dim lngMax As Long
Dim lngCount As Long
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Dim strFile As String
Set maindb = CurrentDb()
Set mainqdf = maindb.QueryDefs("qryCOSearch")
Set mainRst = mainqdf.OpenRecordset(dbOpenDynaset, dbEdit)
'all code below explains exporting the query results to excel
'allow user to choose path to save to
strFile = GetSaveFile_CLT("C:\", "Save this file as", "Untitled.
xls")
If Not strFile Like "*.xls" Then
MsgBox "you have saved in the wrong file format"
Exit Sub
End If
'defining the variables
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets.Add
'formatting cells in excel
With xlSheet
For Each Cell In xlSheet.Range("A1", "J1")
Cell.Font.Size = 10
Cell.Font.Name = "Arial"
Cell.Font.Bold = True
Cell.Interior.Color = rgb(204, 255, 255)
Cell.HorizontalAlignment = xlHAlignCenter
Cell.WrapText = True
Next
.Cells(1, 2).HorizontalAlignment = xlHAlignLeft
.Columns("A").ColumnWidth = 10
.Columns("B").ColumnWidth = 24
.Columns("C").ColumnWidth = 16
.Columns("C").HorizontalAlignment = xlHAlignLeft
.Columns("E").ColumnWidth = 30
.Columns("F").ColumnWidth = 20
.Columns("G").ColumnWidth = 8
.Columns("H").ColumnWidth = 32
.Columns("I:J").ColumnWidth = 24
.Rows(1).RowHeight = 16
End With
'copying the query results from the recordset to the
excel file
With xlSheet
.Name = "Results"
.UsedRange.ClearContents
lngMax = mainRst.Fields.Count
For lngCount = lngMax To 1 Step -1
.Cells(1, lngCount).Value = mainRst.Fields(lngCount -
1).Name
Next lngCount
.Range("A2").CopyFromRecordset mainRst
End With
lngMax = xlBook.Worksheets.Count
'deleting all other worksheets except for "Results"
For lngCount = lngMax To 1 Step -1
If xlBook.Worksheets(lngCount).Name <> "Results" Then
xlBook.Worksheets(lngCount).Delete
End If
Next lngCount
xlBook.SaveAs strFile
MsgBox "Export Completed", vbInformation
End Sub