Solved

Avoiding Error 343

Posted on 2001-07-30
12
255 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 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

856 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