ホームページ >データベース >mysql チュートリアル >Microsoft Access クエリ用に VBA でパラメータを安全に使用する方法

Microsoft Access クエリ用に VBA でパラメータを安全に使用する方法

Susan Sarandon
Susan Sarandonオリジナル
2025-01-23 08:21:09558ブラウズ

How to Securely Use Parameters in VBA for Microsoft Access Queries?

Microsoft Access のさまざまなコンテキストでの VBA パラメータの使用

多くの Microsoft Access 開発者は、パラメーターの正しい使用方法を理解していないため、脆弱なコードに依存しています。この短いガイドでは、このトピックについて光を当てていきます。

フォームとレポート

Access は、ほとんどの場合パラメータを使用せずに、SQL コードでフォームやレポートの値を直接参照する独自の方法を提供します。

<code>DoCmd.RunSQL "INSERT INTO Table1(Field1) SELECT " & _
    "Forms!MyForm!MyTextbox" '插入单个值</code>

TempVars

TempVars は、複数のクエリの値を保存して再利用する方法を提供します:

<code>TempVars!MyTempVar = Me.MyTextbox.Value
DoCmd.RunSQL "INSERT INTO Table1(Field1) SELECT " & _
    "Field1 FROM Table2 WHERE ID = TempVars!MyTempVar"</code>

カスタム関数 (UDF)

UDF は値の保存と取得に使用できます:

<code>Option Compare Database
Option Explicit

Private ThisDate As Date

Public Function GetThisDate() As Date
    If ThisDate = #12:00:00 AM# Then
        ' 设置默认值。
        ThisDate = Date
    End If
    GetThisDate = ThisDate
End Function

Public Function SetThisDate(ByVal NewDate As Date) As Date
    ThisDate = NewDate
    SetThisDate = ThisDate
End Function

'设置值:
SetThisDate SomeDate</code>

DoCmd.SetParameter を使用します

DoCmd.SetParameter では、DoCmd.OpenForm、DoCmd.OpenReport、およびその他の DoCmd ステートメントのパラメーターを設定できます:

<code>DoCmd.SetParameter "MyParameter", Me.MyTextbox
DoCmd.OpenForm "MyForm",,, "ID = MyParameter"</code>

ダオ

DAO では、レコードセットを開いたりクエリを実行したりする前に、DAO.QueryDef オブジェクトを使用してクエリを作成し、パラメータを設定できます。

<code>'执行查询,未命名参数
With CurrentDb.CreateQueryDef("", "INSERT INTO Table1(Field1)" & _
    " SELECT Field1 FROM Table2 WHERE Field1 = ?p1 And " & _
    "Field2 = ?p2")
    .Parameters(0) = Me.Field1
    .Parameters(1) = Me.Field2
    .Execute
End With

'打开记录集,命名参数
Dim rs As DAO.Recordset
With CurrentDb.CreateQueryDef("", "SELECT Field1 FROM Table2" & _
    " WHERE Field1 = FirstParameter And Field2 = " & _
    "SecondParameter")
    .Parameters!FirstParameter = Me.Field1 '感叹号表示法
    .Parameters("SecondParameter").Value = Me.Field2 '更明确的表示法
    Set rs = .OpenRecordset
End With</code>

アド

ADO では、ADODB.Command オブジェクトを使用してパラメータを作成し、Command.Parameters コレクションに追加できます。

<code>'执行查询,未命名参数
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
With cmd
    Set .ActiveConnection = CurrentProject.Connection '使用与当前数据库的连接
    .CommandText = "INSERT INTO Table1(Field1) SELECT " & _
    "Field1 FROM Table2 WHERE Field1 = ? And Field2 = ?"
    .Parameters.Append .CreateParameter(, adVarWChar, adParamInput, Len(Me.Field1), Me.Field1) 'adVarWChar 用于可能包含 Unicode 的文本框
    .Parameters.Append .CreateParameter(, adInteger, adParamInput, 8, Me.Field2) 'adInteger 用于整数(长整数或整数)
    .Execute
End With

'打开记录集,隐式参数
Dim rs As ADODB.Recordset
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
With cmd
    Set .ActiveConnection = CurrentProject.Connection '使用与当前数据库的连接
    .CommandText = "SELECT Field1 FROM Table2 WHERE " & _
    "Field1 = @FirstParameter And Field2 = @SecondParameter"
    Set rs = .Execute(,Array(Me.Field1, Me.Field2))
End With</code>

これらの方法に従うことで、開発者は SQL インジェクションの脆弱性を排除し、Access アプリケーションのセキュリティを向上させることができます。

以上がMicrosoft Access クエリ用に VBA でパラメータを安全に使用する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。