Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Sub Export_To_Excel()
Dim strSql As String
Dim dBase As DAO.Database
Dim rs As DAO.Recordset
Dim i As Integer
Dim uRow As Integer
Dim xlApp As Object
Dim Sht As Object
DoCmd.SetWarnings False
[COLOR=green] 'setup query to get all records from a table[/color]
strSql = "SELECT * FROM [TWPW AGM];"
Set dBase = CurrentDb()
Set rs = dBase.OpenRecordset(strSql, dbOpenDynaset)
[COLOR=green] 'Create Excel object[/color]
Set xlApp = CreateObject("Excel.Application")
xlApp.Workbooks.Add
xlApp.Visible = True
Set Sht = xlApp.ActiveWorkbook.Sheets(1)
uRow = 1
With rs
[COLOR=green] ' Loop through the field names[/color]
For i = 0 To .Fields.Count - 1
Sht.Cells(uRow, i + 1).Value = rs.Fields(i).Name
Next i
uRow = 2
End With
[COLOR=green] ' Loop through the records and copy them to worksheet.[/color]
Do Until rs.EOF
For i = 0 To rs.Fields.Count - 1
Sht.Cells(uRow, i + 1).Value = rs(i)
Next i
rs.MoveNext
uRow = uRow + 1
Loop
[COLOR=green] 'Here's where the formatting happens[/color]
With Sht
.Name = "Give the sheet a name"
[COLOR=green] 'set cell format to general[/color]
.Range("A1:A200").NumberFormat = "@"
'[COLOR=green] 'make titles bold[/color]
.Range("A1:F1").Font.Bold = True
End With
[COLOR=green] 'Other stuff[/color]
ActiveWorkbook.SaveAs "Full Path & Name go here"
[COLOR=green] 'Clear variables[/color]
Set Sht = Nothing
xlApp.Quit
Set xlApp = Nothing
Set rs = Nothing
Set dBase = Nothing
End Sub