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

Open a secured via access database password

Status
Not open for further replies.

jaloes

MIS
Mar 22, 2004
6
ES
I'm trying to open a Ms Access 2000 db secured via de database password from an executable program created with vb6.

The problem is : When the database is open, then visual basic program must be ended without ending Access by destroying then Access.database object.

I don't see the way to do this because I've try to open the mdb via the shell and sending the pwd via senkeys. It does'n work.

As you probably know, it also doesn'n work if you provide the pwd in this way because then Access asumes than the pwd so provide is the workgroup password.


If you can help me, I'll be very gratefull.
Thanks in advance

 
Sub OpenDB()
Dim db As DAO.Database
Dim ws As DAO.WorkSpace
Set ws = DBEngine.WorkSpaces(0)
Set db = ws.OpenDatabase _
("C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb", _
False, False, "MS Access;PWD=northwind")

db.Close
End Sub

Put your own path/database and password in.
 
Thanks, but it doesn't work. My application does't open at all.

With similar code with opendatabase and currentdatabase methods of access.application it works properly, but, ...
when the access object is destroyed with the end of the exe code, the access application closes also. That is the problem and the question also.

 
I can't remember how, but you can make system calls to the windows system instead of creating an object to open. It is of course less secure however there is no object to be destroyed when the VB program closes. Sorry I can't be of more help, but it was just a quick note from my class on fork() and exec() in c++ and multithreading in VB, which I haven't used since then.
 
I think you had better post your code that you are using to open the ACcess instance.
 
OK, I tried doing it in two ways:

Via system call, sendkeys doesn't work properly :

Private Declare Function OpenProcess Lib "kernel32" (ByVal _
dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal _
dwProcessID As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject _
As Long) As Long
Private Const INFINITE = &HFFFF
Private Const SYNCHRONIZE = &H100000 ' Enables using the process handle in any of the wait functions to wait for the process to terminate.
Private Const WAIT_TIMEOUT = &H102
Private Const PROCESS_ALL_ACCESS = &H1F0FFF ' Specifies all possible access flags for the process object.
Private Const PROCESS_CREATE_THREAD = &H2 ' Enables using the process handle in the CreateRemoteThread function to create a thread in the process.
Private Const PROCESS_DUP_HANDLE = &H40 ' Enables using the process handle as either the source or target process in the DuplicateHandle function to duplicate a handle
Private Const PROCESS_QUERY_INFORMATION = &H400 ' Enables using the process handle in the GetExitCodeProcess and GetPriorityClass functions to read information from the process object.
Private Const PROCESS_SET_INFORMATION = &H200 ' Enables using the process handle in the SetPriorityClass function to set the priority class of the process.
Private Const PROCESS_TERMINATE = &H1 ' Enables using the process handle in the TerminateProcess function to terminate the process.
Private Const PROCESS_VM_OPERATION = &H8 ' Enables using the process handle in the VirtualProtectEx and WriteProcessMemory functions to modify the virtual memory of the process.
Private Const PROCESS_VM_READ = &H10 ' Enables using the process handle in the ReadProcessMemory function to read from the virtual memory of the process.
Private Const PROCESS_VM_WRITE = &H20 ' Enables using the process handle in the WriteProcessMemory function to write to the virtual memory of the process.

Private Const ERROR_PROCESO_NO_INICIADO = 32600

Const miPwd = "xxx" ' la contraseña de la bd

Public Function ShellAccess(RutaBd As String, Optional WindowStyle As _
VbAppWinStyle = vbMinimizedFocus) As Long
'*
Dim dwProcessID As Long
Dim hProcess As Long
Dim rutaAcc As String
Dim Cmd As String
On Error GoTo ERRORES_SHELLACCESS
rutaAcc = SysCmd(acSysCmdAccessDir) & "msaccess.exe "
Cmd = Chr(34) & rutaAcc & Chr(34) & " /NoStartup /User Admin /Pwd " & Chr(34) & "" & Chr(34) & " /wrkgrp " & Chr(34) & DBEngine.SystemDB & Chr(34) & Chr(34) & RutaBd & Chr(34)
dwProcessID = Shell(Cmd, WindowStyle)
If dwProcessID = 0 Then
Error ERROR_PROCESO_NO_INICIADO
End If
hProcess = OpenProcess(SYNCHRONIZE, False, dwProcessID)
If hProcess = 0 Then
Exit Function
Else
CloseHandle hProcess
End If
DoEvents
SHELLACCESS_NORMAL_EXIT:
ShellAccess = dwProcessID
Exit Function
ERRORES_SHELLACCESS:
Select Case Err.Number
Case ERROR_PROCESO_NO_INICIADO
MsgBox "No pudo abrirse " & rutaAcc & ". Proceso cancelado", vbCritical + vbOKOnly, "E R R O R"
Resume SHELLACCESS_NORMAL_EXIT
Case Else
MsgBox Err.Number & ": " & Err.Description
dwProcessID = 0
Resume SHELLACCESS_NORMAL_EXIT
End Select
End
End Function

Private Sub Command1_Click()
Dim RutaBd As String
Dim NombreBd As String
Dim rc As Long
'*
RutaBd = App.Path
NombreBd = "bd1.mdb"
rc = ShellAccess(RutaBd & "\" & NombreBd, vbMaximized)
DoEvents
AppActivate rc, False
SendKeys miPwd & "{ENTER}", True
Unload Me
End Sub


- b) Via Access objects:

Option Explicit
' Esta versión abre la base de datos pero se cierra al cancelar
' el programa visual basic.
Private Sub Command1_Click()
' Start a new instance of Access for Automation:
Dim dbRuta As String
Dim dbNombre As String
Dim DBpwd As String
' Define as STatic so the instance of Access doesn't close when
' the procedure ends
Static MAccess As Access.Application
Static oDBEngine As DAO.DBEngine
Dim oDB As DAO.Database
'
dbRuta = App.Path
dbNombre = "bd1.mdb"
'DBPath = "\\s8091s03\\DATOS\SB02G082\Aplic@do\Aplic@do 200403\apli0403.mdb"
DBpwd = "midb" ' Database Password
Set MAccess = New Access.Application
Set oDBEngine = MAccess.DBEngine
Set oDB = oDBEngine.OpenDatabase( _
Name:=dbRuta & "\" & dbNombre, _
Options:=True, _
ReadOnly:=False, _
Connect:=";PWD=" & DBpwd)
MAccess.OpenCurrentDatabase _
filepath:=dbRuta & "\" & dbNombre, _
Exclusive:=True
MAccess.Visible = True
Set oDBEngine = Nothing

End Sub

In this way, Access closes when this executable ends.

I need to open the Access application and finish the exe.

In the future, we'll develope a new project using ADO objects, but it is not possible now.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top