Phred Bear said:
<< No, that wasn't the question I was asking, though it's always a
good idea to mention the version. But I meant, you have two
databases: one from which you want to extract data (the "source"
database), and one in which you want to store data (the "target"
database). Which of these databases is running the code? And which
one is password-protected?>>
Ahaaa,
I have an Access 2000 db with no content other than the code, to
extract the data, in a module and a single button to execute the
code(db1). It is not protected.
I have another Access 2000 db, which is password protected, with the
data I am looking to extract(db2).
I extract the data from db2 and create a new Project 98 file based on
this data, which isn't protected. All the code runs in db1 which
quits at the end of the procedure.(Issues with tidying up the running
server, which running the code in a seperate 'Switchboard' db and
Quitting afterwards seems to resolve satisfactorily.)
Here is a bit of the code which works:
dbName is a variable containing the path and db name.
SQLString = "SELECT Min([Date ToR Agreed]) AS MinDate FROM BarChart
Barchart IN '" & dbName & "';"
Set db = OpenDatabase(dbName, False, False, "MS Access;
pwd=ThePassword") Set rs = db.OpenRecordset(SQLString)
TempDate = rs!MinDate
ExtractMinDate = DateAdd("m", -1, TempDate)
Actually, that looks to me like you are specifying the database name
redundantly. If you have opened the database and set the object
variable db as a reference to it, then you wouldn't need to specify the
IN clause for that database when you use OpenRecordset to open a query
from it.
Here is the bit which doesn't: (Although it works fine on an
unprotected db) Problem is: how do I insert the password?
SQLString = "SELECT [Ref No], [Date ToR Agreed], EstimatedEndDate,
[Legacy/DWP], NoOfInv, [Lead Investigator], [Support Investigator],
[Ops Mngr] INTO BarChart IN '" & dbName & "' "
SQLString = SQLString & " FROM Main IN '" & dbName & "' WHERE
((([Investigation Type]) <> ""6"") And (([End Date]) Is Null)) "
SQLString = SQLString & " ORDER BY [Ref No], [Investigation Type];"
DoCmd.RunSQL SQLString
Hope this explains it OK.
I don't see where Project 98 comes into it, but I'm going to proceed on
the assumption that it doesn't, as far as this question is concerned. I
note that the above SQL statement is extracting data from a table in
<dbName> and sticking that data into a new table in that same <dbName>
database. So it appears the source and target databases are one and the
same. Is that correct? If so, you don't really need the IN clause in
the SQL statement at all, so long as you execute that statement via a
database object that has been opened directly on the database you want
to manipulate. You should be able to do this:
'----- start of suggested code -----
Set db = OpenDatabase( _
dbName, False, False, _
"MS Access;pwd=ThePassword")
SQLString = _
"SELECT [Ref No], [Date ToR Agreed], EstimatedEndDate, " & _
"[Legacy/DWP], NoOfInv, [Lead Investigator], " & _
"[Support Investigator], [Ops Mngr] " & _
"INTO BarChart " & _
"FROM Main " & _
"WHERE " & _
"((([Investigation Type]) <> ""6"") And " & _
"(([End Date]) Is Null)) " & _
"ORDER BY [Ref No], [Investigation Type];"
db.Execute SQLString, dbFailOnError
'----- end of suggested code -----
As you see, since you specified the password when opening the database
object, and you use that database object to execute the query, that's
the database whose tables are going to be manipulated, and you don't
need an IN clause or any additional specification of the database
password.
It is also possible to specify the necessary connect string, including
the password, in the query itself, and thus not have to open a separate
database object (and be able to use RunSQL instead of db.Execute). But
it seems simpler to do it this way.