0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AccessのVBAでコンボボックスの選択肢、列数、列幅を初期化(値リストとテーブルクエリ両方)

Last updated at Posted at 2023-03-28

前提

image.png

やり方(値リスト1列)

Option Compare Database
Option Explicit

'フォームを開く時のイベント
Private Sub Form_Open(Cancel As Integer)
    
    '値集合タイプ
    Me.cmb_test.RowSourceType = "Value List"

    '選択肢を全て無くしておかないとフォームを開くたびに選択肢がどんどん追加されてしまう
    Me.cmb_test.RowSource = ""
    
    '列数
    Me.cmb_test.ColumnCount = 1
    
    '選択肢
    Me.cmb_test.AddItem "あ"
    Me.cmb_test.AddItem "い"
    Me.cmb_test.AddItem "う"
    Me.cmb_test.AddItem "え"
    
End Sub

結果。
image.png

やり方(値リスト2列)

Option Compare Database
Option Explicit

'フォームを開く時のイベント
Private Sub Form_Open(Cancel As Integer)
    
    '値集合タイプ
    Me.cmb_test.RowSourceType = "Value List"

    '選択肢を全て無くしておかないとフォームを開くたびに選択肢がどんどん追加されてしまう
    Me.cmb_test.RowSource = ""

    '列数
    Me.cmb_test.ColumnCount = 2
    
    '列幅調整して表示名だけにしたい時は下記を有効に
    'Me.cmb_test.ColumnWidths = "0cm;10cm;"
    
    '選択肢
    Me.cmb_test.AddItem "あ;あです;"
    Me.cmb_test.AddItem "い;いです;"
    Me.cmb_test.AddItem "う;うです;"
    Me.cmb_test.AddItem "え;えです;"
    
End Sub

結果。
image.png

もし列幅調整のコメントアウトを外した場合は下記。
image.png

やり方(テーブルクエリ)

下記のようなテーブルがある前提。
image.png

Option Compare Database
Option Explicit

'フォームを開く時のイベント
Private Sub Form_Open(Cancel As Integer)
    
    '値集合タイプ
    Me.cmb_test.RowSourceType = "Table/Query"
    
    '値集合ソース
    Me.cmb_test.RowSource = "tester"
    
    '列数
    Me.cmb_test.ColumnCount = 2
    
    '列幅を調整して表示名だけにしたい時は下記を有効に
    'Me.cmb_test.ColumnWidths = "0cm;10cm;"
    
End Sub

結果。
image.png

列数が1の場合や列幅調整については『値リスト』で設定する場合と同様なので
割愛。

参考サイトさん

バージョン

Windows 10 Pro 22H2 OSビルド 19045.2788
Microsoft Access for Microsoft 365 MSO (バージョン 2302 ビルド 16.0.16130.20298) 32 ビット

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?