プロが教える店舗&オフィスのセキュリティ対策術

実質、NO.3213956の続きです。
聞いてばかりもだめなので小一時間探したのですが、
見つからないので、また質問させていただきます。

オプションボタンが2個以上(同じオブジェクト名で配列・欠番はないものとする)、テキストボックス(数字しか入力できない)が1個、コマンドボタンが2個あります。

テキストボックスに数字を入力し、コマンドボタンを押すと
入力した数字の配列番号のオプションボタンをTrueにします。

Private Sub Command1_Click()
Dim ctl As Control 'コントロール
Const conCTLNAME = "Option1"

Dim intIndex As Integer
For Each ctl In Me.Controls
'テキストに数字入力確認
If IsNumeric(txtIndex.Text) Then
intIndex = CInt(txtIndex.Text)
Else
MsgBox "数字を入れてください"
Exit Sub
End If
'オプションボタン判定
If InStr(1, conCTLNAME, ctl.Name, vbTextCompare) Then
ctl(intIndex).Value = True '※
End If
Next ctl
End Sub

しかし、※でエラー「オブジェクト配列のインデックスを指定してください」が表示されます。
数字を選ぶところは、実際はDBに格納されているNumber型のフラグを持ってくる形になります。

コントロールを変数に格納している仕様で難しいですが、
アドバイスをお願いします。

A 回答 (3件)

訂正



Public Function SetCtlValue(ByVal ctlName As String, _
              ByVal I As Variant, _
              ByVal V As Variant) As Boolean
On Error Resume Next
  Dim isOK As Boolean
  Dim ctl As Control
  
  For Each ctl In Me.Controls
    Debug.Print ctl.Name
    If ctl.Name = ctlName Then
      If I = -1 Then
        ctl.Value = V
        ctl.Text = V
        isOK=True
        Exit For
      ElseIf ctl.index = I Then
        ctl.Value = V
        ctl.Text = V
        isOK=True
        Exit For
      End If
    End If
  Next ctl
  SetCtlValue = isOK
End Function
    • good
    • 0

ctl自体がコントロール配列にはなりえません


コントロール自体です

  dim ctl as Control
  Const conCTLNAME ="Optiion1"
  if not isNumber(Text1.text) then
    MsgBox "数字を入力してください"
    exit sub
  end if

  for each ctl in Controls
    ' 検査対象がオプションボタンか判断
    if typeof ctl is OptionButton then
      ' 検査対象の名前と一致か
      if ctl.name = conCTLNAME then
        ' 検索対象がコントロール配列なら
        ' Indexプロパティがある
        ' 単独コントロールなら エラーになり
        on error resume next
        if ctl.index = Text1.Text then
          ' エラートラップに引っかからなかった場合
          if err.Number = 0 then
            ctl.value = true
            exit for
          end if
        end if
        On error goto 0
      end if
    end if
  next
    • good
    • 0
この回答へのお礼

教えていただいている身分で、揚げ足をとるのは申し訳ないのですが、
2行目は、
Const conCTLNAME ="Option1" ですね。(iが一個多い)

テキストボックスの名称を変えましたら出来ました。

http://www.geocities.co.jp/SiliconValley/4805/vb …
に if typeof ctl is OptionButton then
の説明が書いてありましたね。

完成ソースは
Private Sub Command1_Click()
Dim ctl As Control
Dim str As String
Const conCTLNAME = "Option1"
str = txtIndex.Text
If Not IsNumeric(str) Then
MsgBox "数字を入力してください"
Exit Sub
End If

For Each ctl In Me.Controls
' 検査対象がオプションボタンか判断
If TypeOf ctl Is OptionButton Then
' 検査対象の名前と一致か
If ctl.Name = conCTLNAME Then
' 検索対象がコントロール配列なら
' Indexプロパティがある
' 単独コントロールなら エラーになり
On Error Resume Next
If ctl.Index = CInt(str) Then
' エラートラップに引っかからなかった場合
If Err.Number = 0 Then
ctl.Value = True
Exit For
End If
End If
On Error GoTo 0
End If
End If
Next
End Sub

ですね。ありがとうございます。

お礼日時:2007/08/01 15:50

ちょっとバグを取るには、色々とあり過ぎます。


そこで、幾つかの対案を示して回答とします。
SetCtlValue 関数が即答に近いかと思います。

プログラムコードの書き方はケースバイケース。

Private Sub Command1_Click()
On Error Resume Next
  Dim intIndex As Integer
  Dim ctl   As Control

  If IsNumeric(txtIndex.Text) Then
    intIndex = txtIndex.Text
    Set ctl = Option1(intIndex)
    ctl.Value = True
  Else
    MsgBox "数字を入れてください"
  End If
End Sub

これでもOKですよね。

Private Sub Command2_Click()
  Dim isOK As Boolean
  
  isOK = SetCtlValue("Option1", 2, True)
  If isOK Then
    MsgBox "値をセットしました!"
  Else
    MsgBox "値をセットに失敗しました!"
  End If
  SetCtlValue "Text1", -1, "TEST"
  SetCtlValue "Check1", -1, 1
End Sub

このように SetCtlValue という関数を用意するのも手ですね。
Cption をセットすることが無ければ、問題はないでしょう。

Public Function SetCtlValue(ByVal ctlName As String, _
              ByVal I As Variant, _
              ByVal V As Variant) As Boolean
On Error Resume Next
  Dim ctl As Control
  
  For Each ctl In Me.Controls
    Debug.Print ctl.Name
    If ctl.Name = ctlName Then
      If I = -1 Then
        ctl.Value = V
        ctl.Text = V
        Exit For
      ElseIf ctl.index = I Then
        ctl.Value = V
        ctl.Text = V
        Exit For
      End If
    End If
  Next ctl
  SetCtlValue = CBool(ctl.Value = V)
End Function
    • good
    • 0
この回答へのお礼

前半部分は、Dim ctl As Control にしない場合の記述ですね。

後半部分は、またおもしろいですね。
No.3の案解析後試させていただきます。

お礼日時:2007/08/01 15:46

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!