Solved

Avoiding Error 343

Posted on 2001-07-30
12
250 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
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 142

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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now