Solved

Avoiding Error 343

Posted on 2001-07-30
12
260 Views
Last Modified: 2008-02-01
Hello Experts !

Is it possible in vb6 to determine wheter a control is an array or not without trapping in Error 343 ?

I'm using to write code that only traps in an Error when an exception occured.

So it's much easier to debug your code.

You mustn't halt your program in an error routine and type 'resume' there, continune the program with that statement and abort the changes.



 
0
Comment
Question by:VK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6333771
Try out this:

 put a Picturebox (for example) on your form, and give it index value of 1 and the name Picture1.

 put a second Picturebox on your form, and clear the index value and the name Picture2.

  MsgBox TypeName(Picture1)
  Msgbox TypeName(Picture2)

The code above will give you "Object" and "Picturebox" and info...

Cheers
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6333790
To expand on angelIII's comment, the TypeName function will return "Object" if the control is an array otherwise the type of the control. Using typename on the element of the control array e.g., Msgbox TypeName(Text1(0)) will return the actual type of the control (Textbox in this case).
0
 
LVL 4

Expert Comment

by:nutwiss
ID: 6333804
angelll: your code sample above works fine, but when enumerating controls on a form it does not. thus:

same again, 2 pictureboxes, one with an index, one without:

Private Sub Command1_Click()
 
Dim ctl As Control

For Each ctl In Me.Controls
    If TypeName(ctl) = "Object" Then
        MsgBox ctl.Name & vbTab & ctl.Index
    Else
        MsgBox ctl.Name
    End If
Next

End Sub


this will never return the index, as typename(ctl) always returns the correct type name, not 'object' for a control array.

I assume this is also the case if a control is passed to a function thus: (ctl As Control) ?

is this just yet another niggly bit of VB where you HAVE to have structured error handling to test the simplest situations? (like testing for control arrays and empty dynamic arrays)



0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 43

Expert Comment

by:TimCottee
ID: 6333868
Andy this is true, but this is because the controls collection contains each individual control on the form not the control arrays themselves. As you say it would also be the case if a specific control was passed to a function as it would be the element of the control array that is passed not the array itself. However if you declare the parameter as Object:

Private Function TestControl(Ctrl As Object) As String
    TestControl = TypeName(Ctrl)
End Function

If returns the correct value for typename.

I think that you can avoid using error checking in this case if you think out thoroughly what is needed. However I would agree that properly structured error handling is essential in many cases.
0
 
LVL 4

Expert Comment

by:nutwiss
ID: 6333888
yes, fair enough - so how would you enumerate controls AND find control arrays? (testing Tims knowledge to the limit)
0
 
LVL 6

Author Comment

by:VK
ID: 6333935
hello !

that is the current state of my function:

Public Sub InitgtwCombos(FRM As Form)
    On Error GoTo hErr
   
    Dim Ctrl As Object
    Dim Section As String
    Dim TMP As String
   
    Set FRM.cboName(0).Icons = FRM.ImageList2
    Set FRM.cboName(1).Icons = FRM.ImageList2
   
    Section = "Software\VB and VBA Program Settings\simply\Module\Zahlungsstand\Rechnungen\Layout\Combos\"
    For Each Ctrl In FRM.Controls
        Debug.Print Ctrl.Name
        If TypeName(Ctrl) = "gtwCombo" Then
            TMP = ""
            TMP = "(" & Ctrl.Index & ")"
            Ctrl.LayOutRegKey = Section & Ctrl.Name & TMP
        End If
    Next
   
    Exit Sub
hErr:
    Select Case Err
        Case 343
            Resume Next 'ComboBox ist kein Array
        Case Else
    End Select
End Sub
0
 
LVL 6

Author Comment

by:VK
ID: 6333941
TypeName(Ctrl)is never an "Object"
it is "gtwCommbo", even if it has an index.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6334011
Private Function TestControl(Ctrl As Object) As String
    TestControl = TypeName(Ctrl)
End Function

Private Function IndexOfControl(Ctrl As Object, Container As Object) As String
    If TypeName(Container.Controls(Ctrl.Name)) = "Object" Then
        IndexOfControl = Ctrl.Index
    Else
        IndexOfControl = "N/A"
    End If
End Function

Private Sub Command2_Click()
    Dim Ctrl As Object
    For Each Ctrl In Me.Controls
'        If Ctrl.Name = "Label1" Then
            MsgBox Ctrl.Name & vbLf & TestControl(Ctrl) & vbLf & IndexOfControl(Ctrl, Me)
'        End If
    Next
End Sub

There you go Andy, controls and control arrays enumerated without error handling.
0
 
LVL 6

Author Comment

by:VK
ID: 6334093
To Tim:
So far so good but:

Public Sub InitgtwCombos(FRM As Form)
    On Error GoTo hErr
   
    Dim Ctrl As Object
    Dim Section As String
    Dim TMP As String
   
    Set FRM.cboName(0).Icons = FRM.ImageList2
    Set FRM.cboName(1).Icons = FRM.ImageList2
   
    Section = "Software\VB and VBA Program Settings\simply\Module\Zahlungsstand\Rechnungen\Layout\Combos\"
   
    For Each Ctrl In FRM.Controls
        If TypeName(Ctrl) = "gtwCombo" Then
                If IndexOfControl(Ctrl, Ctrl.Container) <> "N/A" Then
                    TMP = "(" & Ctrl.Index & ")"
                Else
                    TMP = ""
                End If
            Ctrl.LayOutRegKey = Section & Ctrl.Name & TMP
        End If
    Next
   
    Exit Sub
hErr:
    Select Case Err
        Case 343
            Resume Next 'ComboBox ist kein Array
        Case Else
    End Select
End Sub

--->Container.Controls Error 438

Private Function IndexOfControl(Ctrl As Object, Container As Object) As String
   If TypeName(Container.Controls(Ctrl.Name)) = "Object" Then
       IndexOfControl = Ctrl.Index
   Else
       IndexOfControl = "N/A"
   End If
End Function
Container.Controls

doesn't exist
0
 
LVL 6

Author Comment

by:VK
ID: 6334104
The container is a frame, sometimes an array of frames !
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 50 total points
ID: 6334227
VK, just use

If IndexOfControl(Ctrl, FRM) <> "N/A" Then

Rather than

If IndexOfControl(Ctrl, Ctrl.Container) <> "N/A" Then

This is because the frame object doesn't have the controls collection. Whilst the ctrl reports its container as being the frame as it doesn't have the appropriate controls collection so it comes up with this error.

0
 
LVL 6

Author Comment

by:VK
ID: 6334776
Ty Tim, it works fine :-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question