0

Office 2013 バージョンでは ADP がサポートされなくなったため、ADP プロジェクトを ACCDB に変換中です。

ADPでアプリケーション接続を変更する以下のコードがあります

Function ChangeADPConnection(strServerName As String, strDBName As _
   String, Optional strUN As String, Optional strPW As String) As Boolean
Dim strConnect As String
On Error GoTo EH:
Application.CurrentProject.CloseConnection
'The Provider, Data Source, and Initial Catalog arguments are required.
strConnect = "Provider=SQLOLEDB.1" & _
";Data Source=" & strServerName & _
";Initial Catalog=" & strDBName
If strUN <> "" Then
    strConnect = strConnect & ";user id=" & strUN
    If strPW <> "" Then
        strConnect = strConnect & ";password=" & strPW
    End If
Else  'Try to use integrated security if no username is supplied.
    strConnect = strConnect & ";integrated security=SSPI"
End If
Application.CurrentProject.OpenConnection strConnect
ChangeADPConnection = True
Exit Function
EH:
MsgBox Err.Number & ": " & Err.Description, vbCritical, "Connection Error"
ChangeADPConnection = False
End Function

しかし、ACCDB で現在のプロジェクト接続を変更する方法がわかりません。

SQL Server をバックエンドとしてリンク テーブルを使用しています

これが私が試したことです:

Public Function ChangeACCDBConnection(strServerName As String, strDBName As _
   String, Optional strUN As String, Optional strPW As String) As Boolean

    Dim strConnect As String
    Dim cnn As ADODB.Connection
    Application.CurrentProject.Connection.Close
    On Error GoTo EH:
    strConnect = "XXXXX"
    Set Con = New ADODB.Connection
    Con.ConnectionString = strConnect
    CurrentProject.OpenConnection strConnect // Error here
    ChangeConnection = True
    Exit Function

EH:
    MsgBox Err.Number & ": " & Err.Description, vbCritical, "Connection Error"
    ChangeConnection = False
End Function

エラーが発生しました。現在のプロジェクト接続を変更してください。接続文字列に問題はなく、データベースに正常に接続しています。

唯一のことは、アプリケーション全体で使用するために、その接続を Application.CurrentProject に設定する方法です。

どんな助けでも大歓迎です..

4

2 に答える 2

3

私の知る限り、標準の Access データベース アプリケーション (ADP ではない) では、次の方法でのみ接続を操作できます。

  1. リンクされたテーブルを削除し、コードを使用してそれらのリンクを再作成します。これを正しく行うと、有効なデータ ソースにリンク/再リンクできます。これは、ADO 接続には影響しません。リンク テーブルは、実際には DAO と ODBC の組み合わせをデータ アクセス層として使用します。
  2. コード モジュールで ADO 接続オブジェクトをグローバル変数として作成します。データ ソースを切り替えたい場合は、接続を変更する関数を記述します。(これらすべてをクラスでラップすることもできます。)

ADP 以外のアプリケーションには、ADP のようなグローバル ADO 接続が組み込まれているとは思わないので、上記の方法を使用する必要があります。

于 2014-12-30T15:36:53.883 に答える
0

Your code can continue to use currentProject.connection, BUT YOU CAN NOT CHANGE it.

In effect, currentproject connection will ALWAYS resolve to your current front end database.

However, what this means is that existing code that uses this connection object will continue to work.

So, if you had this:

Dim rs As New ADODB.Recordset

rs.Open ("select * from tblHotels"), CurrentProject.Connection
Do While rs.EOF = False
   Debug.Print rs!FirstName
   rs.MoveNext
Loop
rs.Close
Set rs = Nothing

What occurs behind the scenes is the above resolves to our font end, and then to tblHotels (a linked table to sql server). So, Access will use the CURRENT front end database as the current connection. It resolves to that table, but that table is linked (pointed to) sql server, so then access will use the information in the linked table to resolve this connection for you.

I cannot stress more that the current project connection thus resolves to the local object (linked table). Since that table points to sql server, then access will use the information in the linked table, and you be just fine.

So, at a conceptual level: Continue to use current project. You NEVER change, touch, or edit, or modify the current project connection that access returns.

At a practical level: To point, or change what sql server the sql table resoles to, you have to re-link the tables and point them to the new/different database.

So, in place of changing the whole application connection, you simply have to call your re-link table code. Once you done this re-link, then you can continue to use the current project connection object in your code. So the only real difference here is you can’t (never) change the connection object, but you simply will have re-linked the tables to point to the correct database.

At the end of the day, this sill means you NEVER have to deal with connection strings in code, except for the re-link routines.

Note that you should have your re-link code also change/set any PT query you have.

So, to use a PT query, you can go:

Dim rst     As DAO.Recordset

With CurrentDb.QueryDefs("qryPassR")
  .SQL = "select * from tblBooking where id = " & BookingID
  Set rst = .OpenRecordset()
End With

Keep in mind the above is raw T-SQL being sent to SQL Server.

Or, to call a stored procedures, you can go:

Dim rst     As DAO.Recordset

With CurrentDb.QueryDefs("qryPassR")
  .SQL = "exec MyStoredProc " & BookingID
  .Execute
End With

Note how in above we did not deal with connection strings in code, and we did not even have to deal with a connection object. (nor did we even have to create a connection object to call the SQL Server stored procedure. If the stored procedure is to return records, then of course we would use the first syntax:

Dim rst     As DAO.Recordset

With CurrentDb.QueryDefs("qryPassR")
  .SQL = "exec MyStoredProc " & BookingID

  Set rst = .OpenRecordset()
End With

If you had/have forms based on parameters, then you now bind the form DIRECTLY to the linked table.

To pass (restrict) the forms data set, then you can use this

Docmd.OpenForm "frmBooking",,,"id = " & bookingID

The above will load the form, and ONLY pull the ONE record from sql server despite the form being bound directly to the linked table.

So, parameters to forms (to restrict the record(s) pulled) should use the above “simple” where clause of the open form command.

So, you don't need some new global connection object, but can continue to use currentproject connection.

于 2019-03-31T19:17:08.533 に答える