Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / January 2007

Tip: Looking for answers? Try searching our database.

Access to Excel Automation Runtime Error

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AirgasRob - 30 Jan 2007 21:56 GMT
I am trying to learn the basics of Access for VBA but so far it is kicking my
butt.

I am trying to export a table to specific ranges in the excel spreadsheet.
Every time I try and run the code I get an error at "myrecordset.Open mysql"
stating that a variable is missing.

Below is the code I have so far, if you could please look at it and tell me
where I am going wrong I would greatly appreciate it.

Public Sub createexcel()

   Dim cnn As ADODB.Connection
   Set cnn = CurrentProject.Connection
   Dim myrecordset As New ADODB.Recordset
   myrecordset.ActiveConnection = cnn

   Dim mysql As String
   
   mysql = " SELECT QryMainTbl.Loc, tblmain.item"

   myrecordset.Open mysql
   
   Dim mysheetpath As String

   mysheetpath = "C:\TransferStation\MarketIntelligence.xls"
   
   Dim xl As Excel.Application
   Dim xlbook As Excel.workbook
   Dim xlsheet As Excel.worksheet

   Set xl = CreateObject("excel.application")
   Set xlbook = GetObject(mysheetpath)

   xlbook.Windows(1).Visible = True

   Set xlsheet = xlbook.worksheets(1)

   xlsheet.range("a3").CopyFromRecordset myrecordset
   
   myrecordset.Close
   
   xlbook.SaveAs ("C:\TransferStation\MarketIntelligence2.xls")

   xlsheet.Application.Quit

   Set xl = Nothing
   Set xlbook = Nothing
   Set xlsheet = Nothing

End Sub
Perry - 30 Jan 2007 22:08 GMT
myrecordset.Open mysql, cnn

Connection as second parameter to yr .Open()
While there, look at the other (optional) parameters to ADO recordset Open()
command

Krgrds,
Perry

>I am trying to learn the basics of Access for VBA but so far it is kicking
>my
[quoted text clipped - 50 lines]
>
> End Sub
Ed Adamthwaite - 31 Jan 2007 06:36 GMT
Gidday Rob,
it looks like your SQL for the recordset is wrong. Where is the "FROM"
clause?
Try pasting your SQL into the SQL view of a query and check that it works.
HTH,
Ed.
>I am trying to learn the basics of Access for VBA but so far it is kicking
>my
[quoted text clipped - 50 lines]
>
> End Sub
AirgasRob - 31 Jan 2007 13:58 GMT
You were right it was my sql that was stopping the code. The only way I was
able to clear it up was deleteing it all together and replacing it with  

myRecordSet.Open "[Tblmaintbl]"

> Gidday Rob,
> it looks like your SQL for the recordset is wrong. Where is the "FROM"
[quoted text clipped - 56 lines]
> >
> > End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.