Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running Access Query in VB

Status
Not open for further replies.

brandr

IS-IT--Management
Jul 7, 2007
7
GB
Hello, I realize that this is an Access forum but I think that this is an easy question with a quick solution for anyone who might know just a little bit about Access databases in Visual Basic. To let you know, I'm running VB5 Enterprise and&nbsp;&nbsp;Access 97. Now onto my question...<br><br>I have a database (scs.mdb) with six different tables and two different update queries. I would like to know how I can code VB so that when a user hits a command button the two queries automattically run. <br><br>I've been experimenting with shelling Access and then using the SendKeys function, but this is a real pain and it won't work. Please help!<br><br>Thanks in advance,<br>Brandr Beekman<br><A HREF="mailto:bbeekman@rbscorp.com">bbeekman@rbscorp.com</A>
 
Something like this should work: (Change the red)<br><br>Dim app As Access.Application <br>Set app = New Access.Application <br><br>app.OpenCurrentDatabase(&quot;<font color=red>c:\My Documents\YourDatabase.mdb</font>&quot;) <br>app.DoCmd.OpenQuery &quot;<font color=red>QueryYouWanToRun</font>&quot; <br>app.CloseCurrentDatabase <br><br>set app = Nothing <p>Jim Lunde<br><a href=mailto:compugeeks@hotmail.com>compugeeks@hotmail.com</a><br><a href= Application Development
 
I'm not using the VBA in Access but rather creating a stand-alone program in the real Visual Basic. I'm not sure if your code will work for this.
 
I understand. That's why the Access.Application lines are there, to run from Visual Basic. Have you tried it in Visual Basic? I don't have VB on this machine, so I can't test it, but I think it should work.<br><br>All your doing is opening the database, running the query and then closing the database. I know it works for running reports, so I would think it should work for running Queries. <p>Jim Lunde<br><a href=mailto:compugeeks@hotmail.com>compugeeks@hotmail.com</a><br><a href= Application Development
 
Jim - yes, I've tried your code and I get an &quot;Complie Error-User Defined Type Not Defined&quot; and it points to the Dim app as Access.Application line. How is it that your code can launch my Access application, am I missing something?<br><br>Thanks,<br>Brandr
 
Setting a reference to the Microsoft Access object library?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top