LoginSignup
0
0

More than 1 year has passed since last update.

VBScript で起動済みの Access を操作する

Last updated at Posted at 2022-08-07

VBScript で、起動済みの Access で開いているフォームのテキストボックスに値を入れてみよう。

VBScript
Option Explicit

Call ControlAccessSingle
Call ControlAccessMulti
Call ControlAccessMultiJ

GetObject は、最初に起動されたインスタンスをつかまえる。

VBScript
'// 起動済み Access のインスタンスが一つだけの場合
Sub ControlAccessSingle()
    With GetObject(, "Access.Application").Forms("Form1")
        .text1_box.Value = "Hello, World!"
    End With
End Sub

起動済みのインスタンスがひとつだけの場合は上記のコードで問題ないが、複数のインスタンスが起動されている場合は、つかまえたいインスタンスをファイルパスで指定する必要がある。

VBScript
'// 起動済み Access のインスタンスが複数ある場合
Sub ControlAccessMulti()
    With GetObject("C:\Access\Database2.accdb").Application.Forms("Form1")
        .text1_box.Value = "Hello, World!"
    End With
End Sub

また、VBA は日本語の変数名が使えるが、VBScript はそのままでは日本語の変数名が使えないので、日本語の変数名を使いたい場合や、Access のコントロール名が日本語の場合はその名前を [ ] で囲う必要がある。

VBScript
Sub ControlAccessMultiJ()
    Dim [挨拶1], [挨拶2]    
    [挨拶1] = "See you, World!"
    [挨拶2] = "おやすみ!"    
    With GetObject("C:\Access\Database2.accdb").Application.Forms("Form1")
        .text1_box.Value = [挨拶1]
        .[テキスト1_box].Value = [挨拶2]
    End With
End Sub

<参照サイト>

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0