473,394 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 1989

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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.