1

getdate()SQL Serverの機能を使用する MS Access のリンク テーブルに対するクエリがあります。ただし、クエリを実行しようとすると、次のエラーが発生します。

関数内の未定義関数 GetDate

SQL Server T-SQL 構文を使用できるリンク テーブルを作成するにはどうすればよいですか? これは a と呼ばれてpass through queryいるようですが、リンク テーブルの接続をパス スルー クエリとして使用するように設定する方法がわかりません。

現在 Access 2010 を使用しています。クエリは次のとおりです。

select getdate()

それが役立つ場合は、SQL Server へのテーブル リンクを生成する次の vba コードを使用しました。

Function LinkTable(LinkedTableAlias As String, Server As String, Database As String, SourceTableName As String, OverwriteIfExists As Boolean, Username As String, Password As String)
    'This method will also update the link if the underlying table definition has been modified.
    If (InStr(1, LinkedTableAlias, "MSys") > 0) Then
        Log "Skipping " & LinkedTableAlias
        Exit Function
    End If
    'The overwrite parameter will cause it to re-map/refresh the link for LinktedTable Alias, but only if it was already a linked table.
    ' it will not overwrite an existing query or local table with the name specified in LinkedTableAlias.

    'Links to a SQL Server table without the need to set up a DSN in the ODBC Console.
    Dim tdfLinked As DAO.TableDef

    ' Open a database to which a linked table can be appended.
    Dim dbsCurrent As Database
    Set dbsCurrent = CurrentDb()

    'Check for and deal with the scenario ofthe table alias already existing
    If TableNameInUse(LinkedTableAlias) Then
        'If InStr(dbsCurrent.TableDefs(LinkedTableAlias).Connect, "AccessBackup") Then
        '    Exit Function
        'End If

        If (Not OverwriteIfExists) Then
            Log "Can't use name '" + LinkedTableAlias + "' because it would overwrite existing table."
            Exit Function
        End If
        'delete existing table, but only if it is a linked table
        'If IsLinkedTable(LinkedTableAlias) Then
            dbsCurrent.TableDefs.Delete LinkedTableAlias
            dbsCurrent.TableDefs.Refresh
        'Else
        '    Log "Can't use name '" + LinkedTableAlias + "' because it would overwrite an existing query or local table."
        '    Exit Function
        'End If
    End If

    'Create a linked table
    Set tdfLinked = dbsCurrent.CreateTableDef(LinkedTableAlias)
    tdfLinked.SourceTableName = SourceTableName

    tdfLinked.Connect = "ODBC;DRIVER={SQL Server};SERVER=" & Server & ";DATABASE=" & Database & ";UID=" & Username & ";PWD=" & Password & ";"

    On Error Resume Next
    dbsCurrent.TableDefs.Append tdfLinked
    If (err.Number = 3626) Then 'too many indexes on source table for Access
            err.Clear
            On Error GoTo 0

            If LinkTable(LinkedTableAlias, Server, Database, "vw" & SourceTableName, OverwriteIfExists, Username, Password) Then
                Log "Can't link directly to table '" + SourceTableName + "' because it contains too many indexes for Access to handle. Linked to view '" & "vw" & SourceTableName & "' instead."
                LinkTable = True
            Else
                Log "Can't link table '" + SourceTableName + "' because it contains too many indexes for Access to handle. Create a view named '" & "vw" & SourceTableName & "' that selects all rows/columns from '" & SourceTableName & "' and try again to circumvent this."
                LinkTable = False
            End If
            Exit Function
    End If
    On Error GoTo 0

    '** Turn on error handling
  On Error GoTo ErrorHandler:
    tdfLinked.RefreshLink


    LinkTable = True

    Exit Function
ErrorHandler:
    Log "refreshlink failed for " & tdfLinked.Name
    LinkTable = True
4

4 に答える 4

1

エラーが発生する理由は、MSAccess 内の関数でGETDATE()ないためです。おそらくNow()日付と時刻を取得する必要があるか、日付Date()を提供するを使用することができます

于 2014-08-31T19:51:33.830 に答える
1

私はこの声明をよく理解していません:

SQL Server T-SQL 構文を使用できるリンク テーブルを作成するにはどうすればよいですか?

ただし、これは既存の MS Accessquerydefをパススルー クエリに変換する方法です。

クエリでデザイン モードに移動し、Queryメニュー コマンドをSQL Specific押してから、Pass Through

スクリーンショットについては、これを参照してください。

http://www.mssqltips.com/sqlservertip/1482/microsoft-access-pass-through-queries-to-sql-server/

于 2014-09-01T02:13:13.897 に答える
0

t-sqlクエリをパススルーとして簡単に保存します

Select GetDate()

次に、VBA コードでは、次のように移動できます。

TheSqlDate =  currentdb.QueryDefs("qPass").OpenRecordset()(0)

ADO を使用し、接続文字列をハードコーディングし、ここに投稿された他のコードの膨大な量を使用することは、課金対象の時間を稼ぎ、世界の貧困を生み出す方法にすぎません。私の投稿されたソリューションは、コードの 1 行だけです!

于 2014-09-01T20:41:43.203 に答える
0

パススルー クエリを作成する簡単で汚い VBA の方法を次に示します。

Set qdf = CurrentDb.CreateQueryDef("testqry")
' this is just your connection string
qdf.Connect = "ODBC;Driver={SQL Server};Server=MSSQL1; Database=MyDB;Trusted_Connection=Yes"
'anything here gets passed directly to and executed on the SQL Server  
qdf.SQL = "select getdate()"
Set qdf = Nothing

これで、「testqry」を他の Access クエリのように使用できます (とにかく、そこからの SELECT に関する限り)。

于 2014-09-01T03:08:14.217 に答える