472,782 Members | 1,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 software developers and data experts.

Opening an MSAccess DB

I am struggling with some MSAccess automation issues. Basically, we
have a .Net application that uses MSAccess for reporting (legacy
code). We are able to launch MSAccess fine and even work with the
MSAccess COM objects to run our reports (using GetObject(<MDB Path>) -
see http://support.microsoft.com/default...&Product=vbNET
for info on how to do this).

The problem that we are running into is that if our Access database is
already opened, we want to use it instead of opening up another
instance.

I've included the code I've got so far in case it helps with the
problem. The problem line of code is in ValidateAccessHandle (the
exact line is - mAccessHandle =
GetObject(imsAppConfig.DefaultMainMenuMDB)). If this line is commented
out and an instance of MSAccess is already running with our database,
a new MSAccess window will be launched, but the original MSAccess
window will be returned from GetObject(<MDB Path>). If we leave it in,
on some machines, if MSAccess isn't running, it will create an
instance without the window. Since we need pass MSAccess command-line
parameters, this doesn't work for us (also, a report tends to be more
interesting when you can see it :).

Any help is appreciated, thanks!

Private Function ValidateAccessHandle() As Boolean
If mAccessHandle Is Nothing Then
Try
' Check to see if there is an orphaned window.
' If we don't check, we'll end up opening another
window, but using the existing one anyway.
mAccessHandle =
GetObject(imsAppConfig.DefaultMainMenuMDB)
If mAccessHandle Is Nothing Then Return False
Catch ex As Exception
' On some machines, GetObject throws an untyped
exception when the file is not found.
mAccessHandle = Nothing
Return False
End Try
End If

Try
mAccessHandle.run("")
Return True
Catch ex As System.Runtime.InteropServices.COMException When
ex.ErrorCode = -2146820336
Debug.Assert(ex.Message = "You made an illegal function
call.", "Unexpected error message.", String.Format("The message for
this exception was expected to be 'You made an illegal function call.'
but was actually '{0}'", ex.Message))
' The Access window was closed normally (reproducable by
closing Access) and we need to reopen it.
mAccessHandle = Nothing
Return False
Catch ex As System.Runtime.InteropServices.COMException When
ex.ErrorCode = -2147023174
Debug.Assert(ex.Message = "The RPC server is unavailable.
(Exception from HRESULT: 0x800706BA)", "Unexpected error message.",
String.Format("The message for this exception was expected to be 'The
RPC server is unavailable. (Exception from HRESULT: 0x800706BA)' but
was actually '{0}'", ex.Message))
' The Access window was forced closed (reproducable by
opening Task Manager and killing all instances of MSACCESS.exe after
launching it).
mAccessHandle = Nothing
Return False
Catch ex As System.Runtime.InteropServices.COMException When
ex.ErrorCode = -2146825771
' This is expected. "" is not a valid method.
Return True
End Try
End Function

Private Function LaunchAccessClient() As Boolean
' The Access client is already launched.
If ValidateAccessHandle() Then Return True

Dim cmdLine As String = BuildCommandLine()
Shell(cmdLine, AppWinStyle.MinimizedNoFocus, True,
imsAppConfig.WaitTimeForAccess)

mAccessHandle = GetObject(imsAppConfig.DefaultMainMenuMDB)
Return ValidateAccessHandle()
End Function

Public Function BuildCommandLine() As String
'If the current user is "IMSAdmin" then start the access
client using the SystemAdmin user.
'Otherwise start the access client as the MRPUser.
Dim userName As String
Dim password As String
If imsAppConfig.LoginAsAdmin Then
userName = "IMSAdmin"
password = ""
ElseIf imsAppConfig.LoginAsSystemAdmin Then
'If the Login in as SystemAdmin is checked then log the
user in as SystemAdmin.
userName = "SystemAdmin"
password = "" ' removed for obvious reasons :)
Else
userName = "MRPUser"
password = "" ' removed for obvious reasons :)
End If

Dim runtimeSwitch As String
If imsAppConfig.StartWithRuntime Then
runtimeSwitch = " /Runtime "
Else
runtimeSwitch = ""
End If

'Check whether or not to call Startup
Dim startupSwitch As String
If imsAppConfig.RunAccessStartup Then
startupSwitch = " /x mcrAutoExec "
Else
startupSwitch = ""
End If

Dim cmdLine As String
Dim profile = GetProfile()
cmdLine = String.Format("""{0}"" ""{1}"" {2}{3} /profile
""{4}"" /user {5} /pwd {6} /cmd <User>{7}</User><UseCustom>{8}</
UseCustom><CustomGraphics>{9}</CustomGraphics>", AccessLocation,
imsAppConfig.DefaultMainMenuMDB, runtimeSwitch, startupSwitch,
profile, userName, password, imsContext.Current.UserName,
imsContext.Current.UseCustomObjects,
imsAppConfig.CustomGraphics.ToString())

Return cmdLine
End Function
Jun 27 '08 #1
0 1937

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: (Pete Cresswell) | last post by:
I've got a .BAT file that I use for executing various MS Access apps that I wrote way back in the days of 2.0. It's evolved over time, but it still contains a number of possible paths to...
0
by: Richard Beacroft | last post by:
Trying to write a C# Windows App to export all objects and content from 2 MSAccess 97 databases for comparison analysis. very little documentation found. Have managed to instantiate MSAccess,...
3
by: Giuseppe D'Elia | last post by:
Hi there, I got a problem using an MS Access database through ASP.NET. After updating my system from .NET Framework 1.0 to 1.1, my existing application gives me the following error when...
4
by: Annick Van Hoof | last post by:
Hi, I'm having my ASP.NET application hosted at a Windows 2003 server (IIS6). This works almost all the time (I have uptimes of 100% on most days), but then all of a sudden a few days in a row I...
0
by: markie | last post by:
I need to enable users to run MSAccess 97 macros from my VB.Net 2003 application. Here is a code snippet: Dim app As New Access.Application app.OpenCurrentDatabase(MyMacroPath) ....... ..........
4
by: CliffKing | last post by:
I am trying to use Windows Task Scheduler to run a batch file for an already open MS Access database. Below is the syntax to the batch file: Batch file: DailySalesExport.bat REM This runs the...
0
by: MLH | last post by:
I've been unable to determine the correct setting to force a copy of msaccess.exe to be written to target disk during runtime installation process. It may be that the runtime installation...
2
by: reidarT | last post by:
I am making an installation program for an Access application. The application is run on both Office 2000, 2002, 2003 and soon 2007 versions of Office, When I install the application I need a...
3
by: Salad | last post by:
I have 2 apps; one written in A97, the other in A2003. If I open an app in A97 then close it, then open Explorer and dbl-click on the A2003 mdb, I'm informed it's an unrecognized format. If I...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.