Dim n As Long 'declare n as a long integer, this will be used to store the number of records in db
Dim a, b As String 'declare as string
datDiningClub.Recordset.MoveFirst 'move to the first record of the db
m = datDiningClub.Recordset.RecordCount 'let the variable m to equal the number of records in the db
Set x = CreateObject("Excel.application") 'create an excel application, with variable name of x
With x
.Workbooks.Open FileName:=App.Path & ("\reports") 'open the existing templet report
.Sheets("data").Activate 'open the excel sheet 'data'
n = 2 'to allow for 1 row to enter column headings
a = "A" & n 'calculating on which row to place the data
b = "B" & n
c = "C" & n
d = "D" & n
e = "E" & n
f = "F" & n
g = "G" & n
h = "H" & n
i = "I" & n
j = "J" & n
.Range("A2").Value = datDiningClub.Recordset(0)
.Range("B2").Value = datDiningClub.Recordset(1)
.Range("C2").Value = datDiningClub.Recordset(2)
.Range("D2").Value = datDiningClub.Recordset(3)
.Range("E2").Value = datDiningClub.Recordset(4)
.Range("F2").Value = datDiningClub.Recordset(5)
.Range("G2").Value = datDiningClub.Recordset(6)
.Range("H2").Value = datDiningClub.Recordset(7)
.Range("I2").Value = datDiningClub.Recordset(8)
.Range("J2").Value = datDiningClub.Recordset(9)
Do Until datDiningClub.Recordset.EOF
.Range(a).Value = datDiningClub.Recordset(0)
.Range(b).Value = datDiningClub.Recordset(1)
.Range(c).Value = datDiningClub.Recordset(2)
.Range(d).Value = datDiningClub.Recordset(3)
.Range(e).Value = datDiningClub.Recordset(4)
.Range(f).Value = datDiningClub.Recordset(5)
.Range(g).Value = datDiningClub.Recordset(6)
.Range(h).Value = datDiningClub.Recordset(7)
.Range(i).Value = datDiningClub.Recordset(8)
.Range(j).Value = datDiningClub.Recordset(9)
.Range(a).Select
.Selection.EntireRow.Insert 'now insert the row
n = n + 1 'add 1 to n, so that the next entry is not inserted over the top
datDiningClub.Recordset.MoveNext 'move to the next record in the db
Loop
'case statement to select the correct macro to run
Select Case SearchType
Case "SortMembershipNumber"
'Run the macro to perform sort of data
excel.Application.Run "SortMembershipNumber"
Case "SortSurnameAZ"
excel.Application.Run "SortSurnameAZ"
Case "SortSurnameZA"
excel.Application.Run "SortSurnameZA"
End Select
.Visible = True 'show the spreadsheet on the screen
Application.Quit
Set objexcel = Nothing 'destroy the excel object
SearchType = "" 'clear the variable for next time
Set x = Nothing 'destroy the value of x for next time
End Sub