Export DB

  • Thread starter Thread starter Sam
  • Start date Start date
S

Sam

Hi

Is it possible to export data from my access db to Excel
sheet using VBA because I have a condiction

ex I have to transfer all the clients where there solde
is > 100 $

Thanks
 
Create a query with the criteria of > 100 $ in the Solde field. To export
manually, click on File - Export and follow the instructions. Export the query.
To export via code, look at the Transferspreadsheet method in the Help file.
 
Hi Sam

The simple answer is to create a query returning the fields you want in your
spreadsheet, and with a selection clause to limit the records selected - for
example, [solde]>100.

You can the use the TransferSpreadsheet method to export the results of the
query to Excel.

If you want to vary the selection criteria, or be a bit fancier, you could
use the function below. Just pass it the name of a query of a SQL string,
the name of the output file, and True if you want to leave the resulting
workbook open without saving it.

For example:
ExportToExcel "Select * from MyQuery where [solde]>100", _
"C:\My Folder\MyFile.xls"

--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

========== start code ============
Public Function ExportToExcel(sQuery As String, _
sFileName As String, _
Optional fPreview As Boolean)
Dim oXL As Excel.Application
Dim rs As DAO.Recordset, i As Integer
On Error GoTo ProcErr
Set rs = CurrentDb.OpenRecordset(sQuery)
' Start Excel
Set oXL = CreateObject("Excel.Application")
With oXL
.SheetsInNewWorkbook = 1
.Workbooks.Add.Sheets(1).Activate
With .ActiveSheet
' Create a headings row with the field names
For i = 1 To rs.Fields.Count
.Cells(1, i) = rs(i - 1).NAME
Next
.Rows(1).Font.Bold = True
.Rows(1).HorizontalAlignment = xlCenter
' Copy the data from the recordset
.Cells(2, 1).CopyFromRecordset rs
' Autofit the columns
.UsedRange.Columns.AutoFit
End With
' This freezes the headings row to prevent it scrolling
With .ActiveWindow
.SplitRow = 1
.FreezePanes = True
End With
' Either show the result or save and quit
If fPreview Then
.UserControl = True
.Visible = True
Else
.ActiveWorkbook.SaveAs sFileName
.Quit
End If
End With
Set oXL = Nothing
ProcEnd:
On Error Resume Next
rs.Close
If Not oXL Is Nothing Then
' we had an error - quit Excel without saving
oXL.DisplayAlerts = False
oXL.Quit
Set oXL = Nothing
End If
Exit Function
ProcErr:
MsgBox Err.Description
Resume ProcEnd
End Function
======== end code ===========
 
Back
Top