open access database with password protection

mpires

Member
Joined
Jan 29, 2008
Messages
17
Programming Experience
Beginner
Hi, i need to open one Microsoft Access Database that is password protected, and after open i need to close my vb.net app and let access open. Until now the only thing i get that do what i need is:

VB.NET:
appDatabase = New Access.Application Dim MDBFullPath As String = "c:\x1.mdb" 
appDatabase = New Access.Application() 
appDatabase.OpenCurrentDatabase(MDBFullPath, False, "12345") 
appDatabase.Visible = True 
'Me.Close()

But the problem is that after i run the code i need to close my VB.net app, and wend i close VB.net app access will close as well. Is there any other idea how cold i accomplish this??
 
Your problem here is that in your example MS Access is being started as an object of your own application. So therefore it only exists whilst your application is running. So..... you need to open MS Access using Shell and pass in the command line arguments to open the database you require. Haven't tried this but Process.Start might be able to accomplish this for you. I just can't remember if Process.Start wraps the Shell command 'WinExec'

Anyway that's how you do it... But if I may ask why go to the trouble of writing an app that opens up Access then closes the app. You may just as well leverage the power of the .Net Framework and do all that you need to do through VB.Net. Much cleaner, in my opinion.
 
Hi tks for the fast reply,

Well i'm having this trouble because in my company have 6 users that have develop access db with company data, and i was request to make some kind of protection for that files, well what i have made was in first place authenticate the user using AD then i have password protect the mdb files with password, (is beater then nothing) and now wend they authenticate on my login form made in vb.net i need to open the access databases.

I hope that my explanation is clear.

About the shell, i have try to shell the mdb but i can't find any way to send the psw as argument, i have think to start mdb as a process but the same problem i can't find any info or if is possible to send mdb password.

Any help is welcome, or any other way to accomplish this.


Regards,
 
Ok after doing a bit of digging around it looks like you'll have to do this with Shell. Here are the command line switches for sending a password to Access..

VB.NET:
database
- used to specify the Database to be opened.

VB.NET:
/user
- For the username.

VB.NET:
/pwd
- To send the password


Haven't actually tried this but that's all the info you need to get the job done... I think :)
 
Hey tks again,

Well i have test but don't work because access just use psw don't use user, but i have try just sending the psw and don't work.
I think that i have found my first unsolution problem in my live..... i cant get any way to do this.

Is this so impossible, wend we can shell any thing..... This is incredible
 
VB.NET:
<StructLayout(LayoutKind.Sequential)> _

    Public Structure PROCESS_INFORMATION

        Public hProcess As IntPtr

        Public hThread As IntPtr

        Public dwProcessID As UInteger

        Public dwThreadID As UInteger

    End Structure //PROCESS_INFORMATION

 

    <StructLayout(LayoutKind.Sequential)> _

    Public Structure SECURITY_ATTRIBUTES

        Public nLength As Integer

        Public lpSecurityDescriptor As IntPtr

        Public bInheritHandle As Boolean

    End Structure //SECURITY_ATTRIBUTES

 

    <StructLayout(LayoutKind.Sequential)> _

    Public Structure STARTUPINFO

        Public cb As UInteger

        Public lpReserved As String

        Public lpDesktop As String

        Public lpTitle As String

        Public dwX As UInteger

        Public dwY As UInteger

        Public dwXSize As UInteger

        Public dwYSize As UInteger

        Public dwXCountChars As UInteger

        Public dwYCountChars As UInteger

        Public dwFillAttribute As UInteger

        Public dwFlags As UInteger

        Public wShowWindow As Short

        Public cbReserved2 As Short

        Public lpReserved2 As IntPtr

        Public hStdInput As IntPtr

        Public hStdOutput As IntPtr

        Public hStdError As IntPtr

    End Structure //STARTINFO

    <DllImport("kernel32.dll")> _

    Function CreateProcess( _

    ByVal lpApplicationName As String, _

    ByVal lpCommandLine As String, _

    ByVal lpProcessAttributes As IntPtr, _

    ByVal lpThreadAttributes As IntPtr, _

    ByVal bInheritHandles As Boolean, _

    ByVal dwCreationFlags As UInteger, _

    ByVal lpEnvironment As IntPtr, _

    ByVal lpCurrentDirectory As String, _

    ByRef lpStartupInfo As STARTUPINFO, _

    ByRef lpProcessInformation As PROCESS_INFORMATION) As Boolean

    End Function

Ok, above I have written the prototype for the Win32 call to CreateProcess. I have just done this from a small C++ app I wrote and it seemed to work just fine with a password protected MS Access Database. Included in the above are the structs you need, but the most important parameter for you is the lpCommandline. This is where you will place the password for MS Access. This isn't impossible it's just a bit tricky. I'm even sure that there is a pure .Net way of doing this but my guess is that IF there is a .Net way of doing this then it will call CreateProcess anyway so you may as well just do it yourself. Most of the parameters will be NULL, or nothing, or zero in this case.

Just some further information, CreateProcess starts a process and it's main thread, however it runs in the security context of the calling program. You may need to fiddle about with the securtiy side of things a bit.
 
Hi again,

Extra help :D, you told me that you have test and did went ok, i cant delegate my database to run look:

VB.NET:
myDB = CreateProcess("C:\\Program Files\\Microsoft Office\\Office11\\msaccess.exe", "xxx", IntPtr.Zero, IntPtr.Zero, False, 0, IntPtr.Zero, Nothing, sInfo, pInfo)

This way wend i run my app will open access what means that the code is ok, now were do i delegate my database "c:\\x1.mdb"

"xxx" is the pwd

Tks again for all the help


This is my module:


VB.NET:
Module Module1
    Sub Main()
        StartupDB()
        Console.ReadLine()
    End Sub
    Sub StartupDB()
        Dim myDB As Boolean
        Dim pInfo As PROCESS_INFORMATION = New PROCESS_INFORMATION()
        Dim sInfo As STARTUPINFO = New STARTUPINFO()

       myDB = CreateProcess("C:\\Program Files\\Microsoft Office\\Office11\\msaccess.exe", "xxx", IntPtr.Zero, IntPtr.Zero, False, 0, IntPtr.Zero, Nothing, sInfo, pInfo)
    End Sub
    <StructLayout(LayoutKind.Sequential)> _
    Public Structure PROCESS_INFORMATION
        Public hProcess As IntPtr
        Public hThread As IntPtr
        Public dwProcessID As UInteger
        Public dwThreadID As UInteger
    End Structure 'PROCESS_INFORMATION

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure SECURITY_ATTRIBUTES
        Public nLength As Integer
        Public lpSecurityDescriptor As IntPtr
        Public bInheritHandle As Boolean
    End Structure 'SECURITY_ATTRIBUTES

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure STARTUPINFO
        Public cb As UInteger
        Public lpReserved As String
        Public lpDesktop As String
        Public lpTitle As String
        Public dwX As UInteger
        Public dwY As UInteger
        Public dwXSize As UInteger
        Public dwYSize As UInteger
        Public dwXCountChars As UInteger
        Public dwYCountChars As UInteger
        Public dwFillAttribute As UInteger
        Public dwFlags As UInteger
        Public wShowWindow As Short
        Public cbReserved2 As Short
        Public lpReserved2 As IntPtr
        Public hStdInput As IntPtr
        Public hStdOutput As IntPtr
        Public hStdError As IntPtr
    End Structure 'STARTINFO
    <DllImport("kernel32.dll")> _
    Function CreateProcess( _
    ByVal lpApplicationName As String, _
    ByVal lpCommandLine As String, _
    ByVal lpProcessAttributes As IntPtr, _
    ByVal lpThreadAttributes As IntPtr, _
    ByVal bInheritHandles As Boolean, _
    ByVal dwCreationFlags As UInteger, _
    ByVal lpEnvironment As IntPtr, _
    ByVal lpCurrentDirectory As String, _
    ByRef lpStartupInfo As STARTUPINFO, _
    ByRef lpProcessInformation As PROCESS_INFORMATION) As Boolean
    End Function
End Module
 
You are very nearly there, all the info to start Access should be placed in the lpCommandLine parameter. Like so...


VB.NET:
CreateProcess("C:\\Program Files\\Microsoft Office\\Office11\\msaccess.exe, " database 'PATH TO DATABASE HERE' COMMAND LINE SWITCHES HERE' /pwd /user etc  ,....;


Tried this on the commandline and also works fine here. So long as the Marshaller is doing it's job correctly this should work.

This is what you CreateProcess line should look like just replace the file paths and the password.

VB.NET:
CreateProcess("C:\\Program Files\\Microsoft Office\\Office12\\msaccess.exe", "database [B]PATH:\YOURDATABASE[/B].mdb /pwd [B]YOURPASSWORD[/B]", IntPtr.Zero, IntPtr.Zero, False, 0, IntPtr.Zero, Nothing, sInfo, pInfo)
 
Last edited:
Hey,

Well still no work, your code with my data:

VB.NET:
mydb = CreateProcess("C:\\Program Files\\Microsoft Office\\Office11\\msaccess.exe", "c:\x1.mdb /pwd xpto", IntPtr.Zero, IntPtr.Zero, False, 0, IntPtr.Zero, Nothing, sInfo, pInfo)

This open access but don't open my db....

I think we are having incompatible withe access version i'm using access 2003 and you i think 2007

This cold be the couse for all the things work for you and not for me i can't thing any thing else...
 
You didn't read my post properly. you have to put the parts in bold in...

VB.NET:
CreateProcess("C:\\Program Files\\Microsoft Office\\Office12\\msaccess.exe", "[B]database[/B] PATH:\YOURDATABASE.mdb /pwd YOURPASSWORD", IntPtr.Zero, IntPtr.Zero, False, 0, IntPtr.Zero, Nothing, sInfo, pInfo)


the 'database' command line switch specifies on the cmd line that the following string should be used for the path for the database. There is no comaptibility issue here, it works!
 
Yes is working now :) tks for all the help you have provide me, without you i was unable to end this problem, tks again
 
Back
Top