Actually I think you may need to list the other "optional" parameters as
in :
Set db = DAO.OpenDatabase("C:\_pms\dbforum.mdb", false,true,"MS
Access;PWD=mypassword")
Sorry havn't got time to test it. I seem to remember that the optional
parameters become mandatory when using connect.
Well that's what keeps life interesting.

Signature
tony h
stefantem - 25 Jan 2006 07:02 GMT
Before passwording the database I need to declare a function connect()
that opens the database and than db will be recognized outside the
function. I want to use the function in every commandbutton code or
just once, when opening the workbook that contains those buttons. But I
don't know exactly how to do that. Can you help me?

Signature
stefantem
tony h - 25 Jan 2006 11:24 GMT
If the buttons are on a userform then declaring the db outside the main
structure should keep the object persistant.
If the buttons are on a worksheet then when the code execution stops
persistance will be lost.
There is a way to get apparant persistance and that is to declare a
global class module using "as new". This means that any reference to
the class module will create an instance if one does not already
exist.
Then you open the database in the intiialise event and return a
reference to the database object. This also gives a rather nice feature
that you can close the database in the terminate event thus ensuring
references to the database are tidied up in the event of unexpected
code ending.
I will post some code later.

Signature
tony h
tony h - 25 Jan 2006 11:48 GMT
AS with all these sort of things Error handling is necessary and should
be used.
PART1====================== goes in a standard module
Option Explicit
Public glb As New myGlobalsClass
==========================
PART2 ============ this can go anywhere ==========
Sub a()
Dim DB As DAO.Database
Dim rst As Recordset
Dim strSQL As String
Dim rng As Range
'Set db = DAO.OpenDatabase("C:\_pms\dbforum.mdb")
Set DB = glb.DB
strSQL = "select CLNAM1,CLNAM2 from _client where CLSORC<>"""""
Set rst = DB.OpenRecordset(strSQL)
Set rng = ThisWorkbook.Worksheets(1).Range("A1")
rng.CopyFromRecordset rst
Set rst = Nothing
''' DB.Close
''' Set DB = Nothing
MsgBox "done : " & strSQL
End Sub
PART3 ============= This as a Class module called myGlobalClass
Option Explicit
Dim DB1 As DAO.Database
Private Sub Class_Initialize()
Set DB1 = DAO.OpenDatabase("C:\_pms\dbforum.mdb")
End Sub
Public Function DB() As Database
Set DB = DB1
End Function
Private Sub Class_Terminate()
DB1.Close
Set DB1 = Nothing
End Sub

Signature
tony h
stefantem - 25 Jan 2006 12:29 GMT
It works. Thanks a lot.
Now I will test to password the data base.

Signature
stefantem