I have got this code online somewhere in the forum but i've got a problem with this code. I put this on On Click event button and when i run it there is an error pop up "Runtime error 424 Object Required". Can anybody help me for this to work? I am new to vba code. This is for my project.
Thanks and have a nice day!
Code:
Dim rs As Recordset
Dim db As Database
Dim objExcel As Object
Dim wksExcel As Object
Set db = CurrentDb
'get a recordset based on your table or query
Set rs = qdf.OpenRecordset("SELECT * FROM qryMatlCalculatedMIS", dbOpenSnapshot)
'get an Excel instance
Set objExcel = CreateObject("Excel.Application")
'add a workbook and get the instance for it's 1st Worksheet
Call objExcel.Workbooks.Add
Set wksExcel = objExcel.Worksheets(1)
wksExcel.Activate
'copy the name of the columns
For i = 0 To rs.Fields.Count - 1
wksExcel.Cells(1, i + 1) = rs.Fields(i).Name
Next
'copy the recordset to worksheet
Set Rng = wksExcel.Cells(2, 1)
Rng.CopyFromRecordset rs
Set wksExcel = Nothing
'make Excel visible
objExcel.Visible = True
'deallocate the objects
Set objExcel = Nothing
rs.Close
Set rs = Nothing
Set db = Nothing