Hi All,
I am trying to create VBA in Excel 2003 to open an Access 2003 db, and run a MakeTable Query. I'm not sure if I'm using the wrong vba commands for Excel, or what the problem is- but I can't get my code to work:
I'm getting a runtime error 424 'Object Required' on the DoCmd line. I thought the database was the object?Sub RunQuery() DoCmd.OpenDatabase "S:\path\file.mdb" db.TableDefs.Delete "2010DataSet" db.Execute "MakeDataTable2010" db.Close Set db = Nothing End Sub
For the record, I don't need the Excel users to see the database, so I'd like to add a visible=false in there somewhere.
Any help is appreciated, I get completely lost when I try to cross programs in VBA
Last edited by AnnK; 11-03-2010 at 10:05 AM. Reason: Indicate post has been solved
Hello AnnK,
If this macro is being run from Excel then you need to create an object instance of Access before you can use the Access commands, like DoCmd from Excel.
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
Thanks Leith (cool name by the way),
I changed the code toPublic Sub RunQuery3() Set accApp = CreateObject("Access.Application") accApp.Visible = False accApp.DoCmd.SetWarnings False accApp.OpenCurrentDatabase ("S:\path\file.mdb") accApp.TableDefs.Delete "2010DataSet" accApp.DoCmd.OpenQuery "MakeDataTable2010" accApp.Quit End Sub
Now it's giving me a 'runtime 438- object doesn't support this property or method' on the delete line. I tried removing that line, and I get the 'numeric field overflow' error (as I expected I would if I tried to make a table that was already there). I also get a security message pop-up when it opens the database that I'd like to bypass- but I'll post that separately if I don't find a workaround in the forum.
Hello Annk,
Glad you like my name. Do you know anyone named Leith?
I am not an Access programmer and this getting beyond my area of knowledge. I can do a couple of things for you. Because this is more Access centric, I could move this post to the Access forum for you where you may have a better chance of getting an answer faster or refer your post to some of the moderators who have experience with Access or just leave your post here and hope someone will pick it from here. What would you like to do?
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
No Leiths, just a LeeOur family goes back to the Ayrshire area of Scotland, but we have no interesting names.
I suppose now that I've gotten the application to launch from Excel, the balance of my questions would probably be better suited to the Access area. So if you could transfer it over that would be great.
Thanks so much for your help. I have many more Excel jobs ahead- so I'm sure you'll be seeing me back again.
hi AnnK,
I'm not an Access wizz either but I've been learning quite a lot in my latest job, so I'll see if I can help...
(Leith, I hope this doesn't mess up any of your Mod work)
Here's a couple of UNTESTEDchanges that may help:
Re the Security Message:Public Sub RunQuery3() dim accApp as object 'it's good practice to declare variables & it appears you are using late binding so I've made it an Object. You can change to early binding* by adding a Reference to Access via Excel's VBE - Tools - References - ticking the "Microsoft Access xx.x Object Library" reference. '*(a Google search will bring up a few discussions comparing the two approaches) dim accDb as object 'ditto Dim DoesTableExist As Boolean Set accApp = CreateObject("Access.Application") accApp.Visible = False accApp.DoCmd.SetWarnings False set accDb = accApp.OpenCurrentDatabase ("S:\path\file.mdb") On Error Resume Next ' I'm not sure if the error bypass is needed...? DoesTableExist = IsObject(accDb.TableDefs("2010DataSet")) On Error GoTo 0 ' Turn Error trapping back on If DoesTableExist Then 'you have: accApp.TableDefs.Delete "2010DataSet" 'but shouldn't it be relating to the db not the app? For example, accDb.TableDefs.Delete "2010DataSet" end if with accApp .DoCmd.OpenQuery "MakeDataTable2010" 'perhaps add a line here to ".close" the database before the following line which quits the instance of Access .Quit end with End Sub
Security's normally in place for a reason & some types of discussion are prohibited on this Forum, but if I understand correctly (please provide more detail about the message), I think that setting your "Trusted Locations" may be sufficient. Here's one link which explains Trusted Locations: http://msdn.microsoft.com/en-us/libr...ffice.12).aspx
hth
Rob
Last edited by broro183; 10-27-2010 at 06:00 PM. Reason: add a response about "security message" & remove excessive quote
Rob Brockett
Kiwi in the UK
Always learning & the best way to learn is to experience...
Thanks Rob,
I tried your code, I got the same messages. First I got the 'object required' on the set database line, so I split it out and created an object first like so:Which allowed it to continue, then I got the numeric overflow message on open query line again.Set accDb = CreateObject("Access.Database") accApp.OpenCurrentDatabase ("S:\path\file.mdb")
As far as the security message, it is the basic ' if there is malicious code it could harm your computer' message that seems to be Access's version of the load macros warning in Excel (also annoying). I suspect I'm going to have to do something with a digital signature or certificate. I can make it go away for me...but I don't want to have to explain how to go through the same process to everyone at the company. It would be so nice to have a workaround, perhaps MS could make the software smart enough to see that all the files are on a secure network with 8 million safety systems already in place- making their silly message a no-think click through that is not useful for anyone. To my mind- these messages are as helpful as the 'caution- hot' messages on coffee cups. They only help the legal dept.
I thought I may have made a booboo.
hmmm...
What's the exact wording of the "the numeric overflow message on open query line"?
Have you defined some of the fields in the Table as Integer, and does the query tries to populate them with a value that is greater than +/- 32k?
Does it work if you go back to the ".Execute "MakeDataTable2010"" version?
Or if you try any of the other suggestions in this link?
What's the query's sql string?
Does it work "manually" if you run it in Access?
Does it work "manually" if you attempt to use it in Excel via Data - From Access - choose file...?
LOL, yep they do just seem to be there for the legal dept. How big is your company?
My office mate & I went through 50ish people to set up their Trusted Locations (in excel, Ppt & Access). It's a hassle but only needs to be done once - unless someone gets a major IT overhaul on their computer.
Rob
Last edited by broro183; 10-28-2010 at 03:48 PM.
Rob Brockett
Kiwi in the UK
Always learning & the best way to learn is to experience...
Aha!! I found the problem that was causing the "Numeric Field Overflow" message. I did find that when I ran the query from Access it worked fine, so I did some more searching on the error message. It turns out it is caused by there being mixed data types in the spreadsheet, which when it is linked to Access (which mine is) causes the error upon querying the linked table (but apparently only when the query is initiated from outside of Access). Microsoft provides the solution at
http://support.microsoft.com/kb/815277
(I hope it's okay to post a link that is from Microsoft as opposed to a competing board)
Basically the solution was to run a macro that converts the cells in the mixed columns to text. I already knew that one of my columns was a problem and had done that- but it turns out I had a second column with some entries that were numeric in among the text.
This also allowed me to lose the line about deleting the table before running the query. I thought that I needed it to address the overflow, but since I fixed the text issue, Access just overwrites the table- so it's unneeded. My final -working- code ends up beingThank you for all the help!!Public Sub RunQuery() Set accApp = CreateObject("Access.Application") accApp.Visible = False accApp.DoCmd.SetWarnings False accApp.OpenCurrentDatabase ("S:\path\file.mdb") accApp.DoCmd.OpenQuery "MakeDataTable2010" 'get numeric field overflow on this line if there are mixed text columns ' run the numeric overflow fix if error occurs accApp.Quit End Sub
Last edited by AnnK; 11-03-2010 at 10:06 AM. Reason: spelling error
hi AnnK,
Thank you for posting your solution, marking the thread as Solved & giving me some rep'
Absolutely!
If it wasn't for Microsoft, you wouldn't have the application & therefore you wouldn't have the problem - let alone be asking questions on a Forum.
One of the aims of the Forum is to share knowledge & that's all you're doing
The rule relating to links to other forums explicitly refers to links "in signatures" & when the rule was published (19/01/2010) I believe the intent was to stop spammers using their signatures as "free advertising". Numerous comments have been made about this Rule, but its implementation still riles me...*counting to ten...*
So, all I request, is that you continue to share relevant knowledge in posts where you can - whether the solution is from this, or other Forums.
Rob
Rob Brockett
Kiwi in the UK
Always learning & the best way to learn is to experience...
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks