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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java Code to Start MS Access and Open Specific Database File 1

Status
Not open for further replies.

Dryheat

Technical User
Oct 18, 2004
62
US
I searched and didn't find this answered in the forum yet.
I am developing a Swing form in NetBeans. I need to add a button to my form and build an event that will open MS Access with a specified database file. I can add the button but I don't have a clue how to write the code for opening the MS Access application. Yes, I am new to Java.
Thanks for taking the time to respond.
 
Use JDBC to open the file using the fitting JDBC for Access driver, and use it through there.
 
Thanks for the quick response.
I used your reply to search google for code and found something that looks like it might work. If it does work I will post it to this thread later for others to use.
While I am fairly accomplish VBA coder, Java has me completely baffled.
 
Well I tried searching and again found nothing.
Do to my lack of experience with Java the solution offered by:
TomHu said:
Use JDBC to open the file using the fitting JDBC for Access driver, and use it through there.

Might as well be written in greek. So if anyone has a solution that provides actual code I can copy and paste I would really appreciate it.
 
Ok, finally got it working. The link provided by mikron got me started in the right direction. But the sample code at that link didn't get me to the final solution. I finally ended up doing a google search for examples of "Runtime.getRuntime().exec()" and that landed me here:
tutorialspoint

Wow, so glad I found that site. They provide exactly what I've been searching for the last few weeks. A Java language reference complete with code examples.

So for anyone else who finds this post searching for a similar solution, here is the code that ended up getting the job done for me.

Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
try
{
    //create an array of two strings used to pass the program and file path to the exec process
    String[] cmdArray = new String[2];
    //assign the program path to the first item in the array
    cmdArray[0] = "C:\\Program Files\\Microsoft Office 15\\root\\office15\\MSACCESS.EXE";
    //assign the file path to the second item in the array
    cmdArray[1] = "C:\\Users\\UserName\\Documents\\MyDatabase.accdb";
    //call the exec process using the array as the arguments
    Runtime.getRuntime().exec(cmdArray,null);
}
    catch(Exception e)
{
    System.out.println(e.getMessage());
}
    }

This code is assigned to a button on my Swing form and when the user presses the button an instance of MS Access is opened and the desired database file is loaded. The Swing form remains open in the background and the user can work in the Access platform until the task is completed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top