scubadiver007
Registered User.
- Local time
- Today, 04:17
- Joined
- Nov 30, 2010
- Messages
- 317
I have made some amendments and, after much struggle, I have got it to do what I need but I now get this run-time error 91 on the line in red almost at the end.
The code worked before I did the amendments but now it isn't.
Object variable or With block variable not set
The code worked before I did the amendments but now it isn't.
Code:
Option Compare Database
Option Explicit
Dim xlApp As Excel.Application
Private Sub Export_statements1_Click()
Dim db As DAO.Database
Dim rstTable As DAO.Recordset
Dim rstExcel As DAO.Recordset
Dim mySQL As String
Dim FileName As String
'Set up
Set db = CurrentDb
Set xlApp = CreateObject("Excel.Application")
Set rstTable = db.OpenRecordset("SELECT distinct KCode FROM tble_activity where kcode='K81638';")
rstTable.MoveFirst
'Don't need to check for .eof and .bof here, you've already got everying in a loop
'If the table has no records, rstTable will just skip the loop
Do Until rstTable.EOF
'You can change the file name to whatever you want as long
'as it will create different file names for each record
FileName = "Quarterly Submissions and Payments - " & rstTable("KCode")
'Set up your SQL and bring in the value of KCode for this record
mySQL = "SELECT * from [statement activity src] where gppracticecode= '" & rstTable("KCode") & "' union " & _
"SELECT * from [statement activity opiates] where KCode= '" & rstTable("KCode") & "' union " & _
"SELECT * from [statement activity R] where KCode= '" & rstTable("KCode") & "' union " & _
"SELECT * from [statement activity NPT] where KCode= '" & rstTable("KCode") & "' ;"
db.QueryDefs("STATEMENT Activity").SQL = mySQL
mySQL = "SELECT * from [statement payment src] where KCode= '" & rstTable("KCode") & "' union " & _
"SELECT * from [statement payment R] where KCode= '" & rstTable("KCode") & "' union " & _
"SELECT * from [statement payment NPT] where KCode= '" & rstTable("KCode") & "' ;"
db.QueryDefs("STATEMENT Payment").SQL = mySQL
mySQL = "SELECT Tble_Services.Service, Tble_Services.ID_Signup " & _
"FROM Tble_Services INNER JOIN Tble_Provision ON Tble_Services.ID_Service = Tble_Provision.ID_Service " & _
"WHERE (((Tble_Provision.Kcode)='" & rstTable("KCode") & "')) "
db.QueryDefs("STATEMENT signup src").SQL = mySQL
'Set up your recordset for export using your export query
Set rstExcel = db.OpenRecordset("STATEMENT export")
'Call our Export function and give it our recordset and new file name
ExportExcel rstExcel, FileName
'Move to the next record
rstTable.MoveNext
Loop
xlApp.Quit
rstTable.Close
[B][COLOR=red]rstExcel.Close[/COLOR][/B]
Set xlApp = Nothing
Set rstTable = Nothing
Set rstExcel = Nothing
End Sub