views:

398

answers:

2

Connect to a linked table with code I have some linked tables from a SQL-server; they are linked with an ODBC connection. The password is not saved with the connection. When I am double clicking on the table in Access table-view I get a prompt for username and password. After entering the password I can view the data in the table.

My problem is when I try to access the table with code before having opened it in this way. What I try to do is to use ADODB to open a recordset with data from the linked table, like:

Dim rst as new ADODB.Recordset
Dim sql as string
Sql = “SELECT * FROM LinkedTable”
rst.Open sql, CurrentProject.Connection, adOpenForwardOnly, adLockReadOnly

Running this code without having access the table before will generate this error: Error# -2147467259, ODBC: connection to dns-name failed.

So, my question is, are there any way to connect to the database with code that can be run when the database is opened? This would also help the users as they would not have to remember a password to the SQL-server.

A: 

You can use a connection string in your code, it is easy enough, seeing you are already using ADO: http://www.connectionstrings.com/

You will need to find out which version of SQL Server you are linking to.

Remou
But will that also work for the times when I use the table as source in a form? I'm talking about those time when I'm not using code to access the table. It would be very good if I could, so to say, open the table without the user having to type a password.
Markus
As a general rule, the user should not be given access to tables, except through forms and reports. Forms can be bound to an ADO recordset (http://support.microsoft.com/kb/281998/EN-US/). If you wish to open the table without a password, you are probably best to set up DSN that way. You may wish to read: http://support.microsoft.com/kb/892490
Remou
I agree with you that users should never get access to the tables. I will have to check out how to use ADO to bind recordsets to forms, reports, listboxes, comboboxes, and so on. Maybe that is a working solution.
Markus
You might like to use adp (project), if you are not already doing so, it can be faster.
Remou
Can't use adp, as only two tables are from the sql-server, the rest is regular access tables.
Markus
+1  A: 

It seems that you are mixing 2 technologies that might not work together, ie linked tables through ODBC and ADODB recordsets. Have you tried to open DAO recordsets on your linked tables?

Dim rst as DAO.Recordset
Dim sql as string
Sql = “SELECT * FROM LinkedTable”
set rst = currentDb.openRecordset(sql,<your parameters>)

You could of course use ADODB recordsets through 2 ADODB connections, one to your access file, the other one to your SQL server:

Dim rsSQL as ADODB.recordset, _
    rsACCESS as ADODB.recordset, _
    connectionSQL as ADODB.connection, _
    connectionACCESS as ADODB.connection

set connectionSQL = New ADODB.connection
set connectionACCESS = New ADODB.connection

connectionSQL.properties(...) = enumerate your SQL parameters
connectionACCESS.properties(...) = enumerate your ACCESS parameters (use currentproject.accessConnection if your access tables are local tables only)

set rsSQl = New ADODB.recordset
set rsACCESS = New ADODB.recordset

rsSQL.open "SELECT * FROM ...", connectionSQL, <other parameters>
rsACCESS.open "SELECT * FROM ...", connectionACCESS, <other parameters>

Linking ADO recordsets to forms and comboboxes in Access is possible. But, when creating forms, this technology has to be mainly managed through VBA code (you will have to write 'on open' events such as set me.recorset = ...), while the standard "linked tables" technology can be easily used through the user-friendly 'form-design' interface.

Philippe Grondier
Hmm, yes I think you are correct. I will see how I will solve it.
Markus