hello all,
I'm stuck after trying to combine parts of 3 separate
code to export to excel (including what to keep or delete).
First, can I combine a text primary key and an autonumber
in an AND within a WHERE statement?
Second, what is the proper sequence of recordset and Excel
lines of code? Thanks.
John
----Code ----
'Get recordset from Client's table
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strFilter As String, Dim qDef As QueryDef
Dim strSaveFileName As String, Dim fPath As String
Dim intLastCol As Integer
Dim oApp As New Excel.Application
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
'Ask for SaveFileName
strFilter = ahtAddFilterItem("", "Excel Files (*.xls)", "*.xls")
strSaveFileName = ahtCommonFileOpenSave(OpenFile:=False, Filter:=strFilter, _
Flags:=ahtOFN_OVERWRITEPROMPT Or ahtOFN_READONLY)
'Test if Save As... cancelled
If strSaveFileName = "" Then
GoTo Exit_cmd?_Click
End If
'Save to file
Set db = CurrentDb
Set oApp = New Excel.Application
Set rs = db.OpenRecordset("Select * FROM tblClients WHERE tblClients.Subgect = Nz(Me![Subgect]) And tblClients.CustomerID
= " & Me.CustomerID", _
dbOpenSnapshot)
'Indicate task complete
MsgBox "File " & strSaveFileName & " successfully saved"
'Start a new workbook in Excel
Set oBook = oApp.Workbooks.Add
Set oSheet = oBook.Worksheets(1)
'Add the field names in row 1
Dim i As Integer
Dim iNumCols As Integer
iNumCols = rs.Fields.Count
For i = 1 To iNumCols
oSheet.Cells(1, i).Value = rs.Fields(i - 1).Name
Next
'Add the data starting at cell A2
oSheet.Range("A2").CopyFromRecordset rs
'Format the header row as bold and autofit the columns
With oSheet.Range("a1").Resize(1, iNumCols)
.Font.Bold = True
.EntireColumn.AutoFit
End With
oApp.Visible = True
oApp.UserControl = True
With oApp
.Visible = True
Set oBook = .Workbooks.Open(strSaveFileName)
On Error Resume Next
Set oSheet = oBook.Worksheets(Sheet1)
If Not Err.Number = 0 Then
Set oSheet = oBook.Worksheets.add
oSheet.Name = conSHT_NAME
End If
Err.clear
On Error GoTo 0
intLastCol = oSheet.UsedRange.Columns.count
With oSheet
.Range(.Cells(1, 1), .Cells(conMAX_ROWS, _
intLastCol)).ClearContents
.Range(.Cells(1, 1), _
.Cells(1, rs.Fields.count)).Font.Bold = True
.Range("A2").CopyFromRecordset rs
End With
End With
oBook.Close True
Set oSheet = Nothing
Set oBook = Nothing
oApp.Quit ' NOTE - changed sequence
Set oApp = Nothing
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
' Set qDef = Nothing
End Sub
I'm stuck after trying to combine parts of 3 separate
code to export to excel (including what to keep or delete).
First, can I combine a text primary key and an autonumber
in an AND within a WHERE statement?
Second, what is the proper sequence of recordset and Excel
lines of code? Thanks.
John
----Code ----
'Get recordset from Client's table
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strFilter As String, Dim qDef As QueryDef
Dim strSaveFileName As String, Dim fPath As String
Dim intLastCol As Integer
Dim oApp As New Excel.Application
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
'Ask for SaveFileName
strFilter = ahtAddFilterItem("", "Excel Files (*.xls)", "*.xls")
strSaveFileName = ahtCommonFileOpenSave(OpenFile:=False, Filter:=strFilter, _
Flags:=ahtOFN_OVERWRITEPROMPT Or ahtOFN_READONLY)
'Test if Save As... cancelled
If strSaveFileName = "" Then
GoTo Exit_cmd?_Click
End If
'Save to file
Set db = CurrentDb
Set oApp = New Excel.Application
Set rs = db.OpenRecordset("Select * FROM tblClients WHERE tblClients.Subgect = Nz(Me![Subgect]) And tblClients.CustomerID
= " & Me.CustomerID", _
dbOpenSnapshot)
'Indicate task complete
MsgBox "File " & strSaveFileName & " successfully saved"
'Start a new workbook in Excel
Set oBook = oApp.Workbooks.Add
Set oSheet = oBook.Worksheets(1)
'Add the field names in row 1
Dim i As Integer
Dim iNumCols As Integer
iNumCols = rs.Fields.Count
For i = 1 To iNumCols
oSheet.Cells(1, i).Value = rs.Fields(i - 1).Name
Next
'Add the data starting at cell A2
oSheet.Range("A2").CopyFromRecordset rs
'Format the header row as bold and autofit the columns
With oSheet.Range("a1").Resize(1, iNumCols)
.Font.Bold = True
.EntireColumn.AutoFit
End With
oApp.Visible = True
oApp.UserControl = True
With oApp
.Visible = True
Set oBook = .Workbooks.Open(strSaveFileName)
On Error Resume Next
Set oSheet = oBook.Worksheets(Sheet1)
If Not Err.Number = 0 Then
Set oSheet = oBook.Worksheets.add
oSheet.Name = conSHT_NAME
End If
Err.clear
On Error GoTo 0
intLastCol = oSheet.UsedRange.Columns.count
With oSheet
.Range(.Cells(1, 1), .Cells(conMAX_ROWS, _
intLastCol)).ClearContents
.Range(.Cells(1, 1), _
.Cells(1, rs.Fields.count)).Font.Bold = True
.Range("A2").CopyFromRecordset rs
End With
End With
oBook.Close True
Set oSheet = Nothing
Set oBook = Nothing
oApp.Quit ' NOTE - changed sequence
Set oApp = Nothing
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
' Set qDef = Nothing
End Sub