ExcelのVBAを使用してデータベースにアクセスするExcelを接続するには?


このチュートリアルでは、ExcelのVBAを使用してデータベースにアクセスするExcelを接続する方法をご案内します.下にしましょう!次のリンクからMS Excelの公式バージョンを取得しますhttps://www.microsoft.com/en-in/microsoft-365/excel

アクセスデータベース
アクセスデータベースは、効果的に組織的に大量のデータを保存する関係データベース管理システムです.

データベースへのアクセスデータベースの接続

ACITVEXデータオブジェクトへのリファレンスの追加
  • まず、Excelのワークシートでは、[開発]タブに移動する必要があります.
  • 次に、コードセクションの下のVisual Basicオプションを選択する必要があります.
  • 選択します
  • さて、マイクロソフトVisual Basicでは、ADOを使用してデータベースにアクセスする必要があります.
  • そのためには、ADOオブジェクトへのリファレンスを追加する必要があります.
  • VBAプロジェクトにモジュールを追加し、ツールをクリックする必要があります.
  • 次に、参照をクリックする必要があります.
  • リファレンスをクリック
  • さて、マイクロソフトActiveXデータオブジェクトライブラリを探してください.
  • ここでは、最新のバージョンをチェックし、OKボタンをクリックする必要があります.
  • 最新バージョンをクリック
  • 最後に、アクセスデータベースへのリンクを作成できます.

  • アクセスデータベースへの接続を確立するために、VBAコードを書き込みます
  • Excelをアクセスデータベースに接続するには、アクセスデータベースが必要です.
  • まず、Excelのワークシートでは、[開発]タブに移動する必要があります.
  • 次に、コードセクションの下のVisual Basicオプションを選択する必要があります.
  • 選択します
  • さて、以下のコードをコピーしてペーストしなければなりません.
  •  Sub ADO_Connection()
    'Creating objects of Connection and Recordset
    Dim conn As New Connection, rec As New Recordset
    Dim DBPATH, PRVD, connString, query As String
    'Declaring fully qualified name of database. Change it with your database's location and name.
    DBPATH = "C:\Users\Admin\Desktop\Test Database.accdb"
    'This is the connection provider. Remember this for your interview.
    PRVD = "Microsoft.ace.OLEDB.12.0;"
    'This is the connection string that you will require when opening the the connection.
    connString = "Provider=" & PRVD & "Data Source=" & DBPATH
    'opening the connection
    conn.Open connString
    'the query I want to run on the database.
    query = "SELECT * from customerT;"
    'running the query on the open connection. It will get all the data in the rec object.
    rec.Open query, conn
    'clearing the content of the cells
    Cells.ClearContents
    'getting data from the recordset if any and printing it in column A of excel sheet.
    If (rec.RecordCount <> 0) Then
     Do While Not rec.EOF
      Range("A" & Cells(Rows.Count, 1).End(xlUp).Row).Offset(1, 0).Value2 = _
      rec.Fields(1).Value
      rec.MoveNext
     Loop
    End If
    'closing the connections
    rec.Close
    conn.Close
    
    End Sub
    
    
  • その後、選択してコードを保存し、ウィンドウを閉じる必要があります.
  • コードを保存する
  • 再度、Excelのスプレッドシートに移動し、[開発]タブをクリックします.
  • コードセクションでマクロオプションを選択する必要があります.
  • マクロオプションの選択
  • ここで、マクロ名が選択されていることを確認し、* Run *ボタンをクリックします.
  • コードを実行する
  • 最後に、Excelのデータとして*出力*を受け取るマイクロソフトExcelのデータベースに接続されます.

  • 閉鎖
    この短いチュートリアルでは、Excel VBAを使用してExcelにデータベースにアクセスするためのガイドラインを提供します任意の*クエリの場合にコメントを残して、同様にあなたの貴重な提案を言及することを忘れないでください.私たちのサイトを訪問していただきありがとうございます!学習を続けるGeek Excel !! *続きを読むExcel Formulas *!!
    続きを読む
  • Options in Quick Access Toolbar in Microsoft Excel 365
  • What is a Name Box and Its Features in Microsoft Excel 365?
  • How to Convert Excel Files to PDF in Microsoft Excel 365
  • How to Write To A Word File Using Macros (VBA) in Excel Office 365?
  • What is CreateObject Method in VBA & How to use CreateObject Method in Excel?