2016-03-29 11 views
0

クエリのパラメータを入力するだけでなく、接続に必要なデータベースサーバーを選択するアクセスフォームを設定しています。これは、それぞれ異なるサーバー名を持つ複数の建物からデータを収集するために実行する必要があります。すべてのテーブル名とフィールドはユニバーサルです。フォームコンボボックスを使用して接続文字列のサーバー名を変更する

SQLサーバーへの接続に使用されるサーバー名、汎用IDとPW、データベース名、ネットワーク名、およびポートがあります。サーバー名を除くすべての建物ですべてが同じです。そのため、接続文字列でサーバー名のみを変更する必要があります。この情報はすべてテーブルに保存されます。

私はスタックや他のサイトからいくつかの投稿を読みましたが、何もできません。

私は、コンボボックスを使用して、接続する建物の名前を選択し、そのコンボボックスにその建物のサーバー名を出力させたいと考えています。 (これは私がプロパティウィンドウ内で行う方法を知っている)そして、新しいサーバーにリンクテーブルを更新するVBAコードを持っている。

コンボボックスの出力を接続文字列で使用するVBA変数に渡す方法と、接続文字列を更新するコードを書き込む方法がわかりません。私が見つけたほとんどのものは、DSNをDSNなしに変更する方法、または特定のテーブルを変更する方法でした。すべてのリンクテーブルを一度に更新したいと思います。

このコードは一般にどのようにコードが動作するかを理解したいので、ELI5のコメントは大歓迎です。ここで

答えて

0

は、接続文字列を作成するための機能です:

Public Function ConnectionString(_ 
    ByVal Hostname As String, _ 
    ByVal Database As String, _ 
    ByVal Username As String, _ 
    ByVal Password As String) _ 
    As String 

' Create ODBC connection string from its variable elements. 
' 2016-04-24. Cactus Data ApS, CPH. 

    Const AzureDomain As String = ".windows.net" 
    Const OdbcConnect As String = _ 
     "ODBC;" & _ 
     "DRIVER=SQL Server Native Client 11.0;" & _ 
     "Description=Your Application;" & _ 
     "APP=Microsoft® Access;" & _ 
     "SERVER={0};" & _ 
     "DATABASE={1};" & _ 
     "UID={2};" & _ 
     "PWD={3};" & _ 
     "Trusted_Connection=No;" 

    Dim FullConnect  As String 

    If Right(Hostname, Len(AzureDomain)) = AzureDomain Then 
     ' Azure SQL connection. 
     ' Append servername to username. 
     Username = Username & "@" & Split(Hostname)(0) 
    End If 
    FullConnect = OdbcConnect 
    FullConnect = Replace(FullConnect, "{0}", Hostname) 
    FullConnect = Replace(FullConnect, "{1}", Database) 
    FullConnect = Replace(FullConnect, "{2}", Username) 
    FullConnect = Replace(FullConnect, "{3}", Password) 

    ConnectionString = FullConnect 

End Function 

そしてここでは、テーブルやパストラフのクエリを再リンクするための機能である:

Public Function AttachSqlServer(_ 
    ByVal Hostname As String, _ 
    ByVal Database As String, _ 
    ByVal Username As String, _ 
    ByVal Password As String) _ 
    As Boolean 

' Attach all tables linked via ODBC to SQL Server or Azure SQL. 
' 2016-04-24. Cactus Data ApS, CPH. 

    Const cstrQuery1 As String = "_Template" 
    Const cstrQuery2 As String = "_TemplateRead" 
    Const cstrQuery3 As String = "VerifyConnection" 

    Const cstrDbType As String = "ODBC" 
    Const cstrAcPrefix As String = "dbo_" 

    Dim dbs    As DAO.Database 
    Dim tdf    As DAO.TableDef 
    Dim strConnect  As String 
    Dim strName   As String 

    On Error GoTo Err_AttachSqlServer 

    Set dbs = CurrentDb 
    strConnect = ConnectionString(Hostname, Database, Username, Password) 

    For Each tdf In dbs.TableDefs 
     strName = tdf.Name 
     If Asc(strName) <> Asc("~") Then 
      If InStr(tdf.Connect, cstrDbType) = 1 Then 
       If Left(strName, Len(cstrAcPrefix)) = cstrAcPrefix Then 
        tdf.Name = Mid(strName, Len(cstrAcPrefix) + 1) 
       End If 
       tdf.Connect = strConnect 
       tdf.RefreshLink 
       Debug.Print Timer, tdf.Name, tdf.SourceTableName, tdf.Connect 
       DoEvents 
      End If 
     End If 
    Next 
    dbs.QueryDefs(cstrQuery1).Connect = strConnect 
    dbs.QueryDefs(cstrQuery2).Connect = strConnect 
    dbs.QueryDefs(cstrQuery3).Connect = strConnect 
    Debug.Print "Done!" 

    AttachSqlServer = True 

Exit_AttachSqlServer: 
    Set tdf = Nothing 
    Set dbs = Nothing 
    Exit Function 

Err_AttachSqlServer: 
    Call ErrorMox 
    Resume Exit_AttachSqlServer 

End Function 

これは、あなたが軌道に乗る必要があります。

関連する問題