[RESOLVED] runtime 340 on object-VBForums
Results 1 to 20 of 20

Thread: [RESOLVED] runtime 340 on object

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    788

    Resolved [RESOLVED] runtime 340 on object

    This line is throwing a runtime 340 control array error

    TagStr = Trim(objBox.Tag)


    TagStr is a string variable.
    The line is in a user defined function:
    Public Function ControlBox_GotFocus(objBox As Object) As String

    Any ideas how I can figure out why?

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    788

    Re: runtime 340 on object

    Here is the full function
    Code:
    Public Function ControlBox_GotFocus(objBox As Object) As String
    Dim tempStr As String, tempChr As String
    Dim TagStr As String, i As Long, ChrPos As Long
    ''On Error GoTo ERRHANDLE
    ControlBox_GotFocus = "Error: "
    
    TagStr = Trim(objBox.Tag)
    i = InStr(TagStr, "Type=D;")
                            'Perform special cursor if this
                            ' is a Date type control...
    If i > 0 Then
    
        i = InStr(TagStr, "ChrPos=")
        If i < 1 Then
            TagStr = TagStr + "ChrPos=00000;"
            objBox.Tag = TagStr
        End If
        i = InStr(TagStr, "ChrPos=")
        tempStr = Mid(TagStr, i + 7, 5)
        ChrPos = Val(tempStr)
        If ChrPos < 1 Then
            ChrPos = 1
        End If
    
        ChrPos = 1
        objBox.SelStart = ChrPos - 1
        objBox.SelLength = 1
    
        tempStr = Trim(str(ChrPos))
        Do While Len(tempStr) < 5
            tempStr = "0" + tempStr
        Loop
        Mid(TagStr, i + 7, 5) = tempStr
        objBox.Tag = TagStr
    
    Else
        tempStr = objBox.Text
        If Len(Trim(objBox.Text)) > 0 Then
            objBox.SelStart = 0
            objBox.SelLength = Len(tempStr)
        End If
    End If
    
    ControlBox_GotFocus = ""
    
    End Function

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    20,096

    Re: runtime 340 on object

    what is the object that is passed as objbox?

    are you doing this in VBA?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    788

    Re: runtime 340 on object

    This is a vb6 application. The object is a TextBox.

  5. #5
    PowerPoster
    Join Date
    Jan 2008
    Posts
    9,345

    Re: runtime 340 on object

    If objBox is actually a textbox I don't see how you can get a control array error on

    TagStr = Trim(objBox.Tag)

    Are you absolutely sure that is where the error is coming from? How do you know this, are you debugging the code and when it tries to execute that function you get the error?

    BTW: Just out of curiosity why do you do this?
    Code:
       '
       '
      ChrPos = Val(tempStr)
        If ChrPos < 1 Then
          ChrPos = 1
        End If
    
        ChrPos = 1
       '
       '
    You put the value of tempStr in ChrPos. If value is less than 1 make it equal to 1. Now, no matter what happened above make ChrPos equal to 1.
    Last edited by jmsrickland; May 8th, 2012 at 06:47 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    788

    Re: runtime 340 on object

    Don't know why that is in there? This is code I am debugging for someone. The line that the debugger is indicating is above:
    TagStr = Trim(objBox.Tag)

    Does not happen all the time either. Seems a bit random as far as I can tell. In other words I have not nailed down the sequence of events that causes this to error...........

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    20,096

    Re: runtime 340 on object

    are some of the textboxes members of a control array? are they the ones that error?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    788

    Re: runtime 340 on object

    There are 4 textboxes with the same name. Their indexes are: 0,1,2,3. The error occurs on those.

  9. #9
    PowerPoster
    Join Date
    Jan 2008
    Posts
    9,345

    Re: runtime 340 on object

    The call statement should be something like this:

    ControlBox_GotFocus Textbox(index) or Call ControlBox_GotFocus(Textbox(index))

    If index is out of range you will get that error

    example:

    ControlBox_GotFocus Textbox(4) or no index ControlBox_GotFocus Textbox
    Last edited by jmsrickland; May 9th, 2012 at 11:37 AM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    788

    Re: runtime 340 on object

    r = ControlBox_GotFocus(TextNoteDate(Index))

    This is the call I have. It is called when the textbox gets focus. Here is the whole GotFocus event for the textbox
    Code:
    Private Sub TextNoteDate_GotFocus(Index As Integer)
    
    r = ControlBox_GotFocus(TextNoteDate(Index))
    
    End Sub
    The index is 0.

  11. #11
    PowerPoster
    Join Date
    Jan 2008
    Posts
    9,345

    Re: runtime 340 on object

    0 is a valid index so that shouldn't cause an array error. Something else is going wrong. You stated earlier that this error does not happen all the time so I suspect that when it does not happen then the call uses a correct index for the textboxes. Is there any other call to the same function that could be passing an invalid index ?


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    788

    Re: runtime 340 on object

    Isolated the error to the Validate event of the textbox with index = 0.
    Change the validate code from calling the Check_Date user defined function to use IsDate. So now this line is throwing error.
    Code:
    Private Sub TextNoteDate_Validate(Index As Integer, Cancel As Boolean)
    Call WriteErrLog("FormPhoneListNotes", "TextNoteDate_Validate", "errlog", "index=" & Index)
    Dim NoteDate As String
    NoteDate = TextNoteDate(Index).Text   '''<---this line gives runtime 340 control array element '0' does not exist
    If IsDate(NoteDate) = False Then
        Cancel = True
    End If
        
    'r = Check_Date(TextNoteDate(Index))
    
    'If InStr(r, "Error:") Then
    '    Cancel = True
    'End If
    
    End Sub

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    788

    Re: runtime 340 on object

    If this form was copied and renamed from another form, could this cause issues with the objects on the form?

  14. #14
    PowerPoster
    Join Date
    Jan 2008
    Posts
    9,345

    Re: runtime 340 on object

    Only if objects are referenced to by using the name of the original Form but that would cause a 424 Object required error.

    NoteDate = originalFormName.TextNoteDate(Index).Text

    but originalFormName was copied and renamed to newFormName


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    788

    Re: runtime 340 on object

    I found this topic discussed here http://www.tek-tips.com/viewthread.cfm?qid=880211
    Do you think that their ideas are valid for my issue?

  16. #16
    PowerPoster
    Join Date
    Jan 2008
    Posts
    9,345

    Re: runtime 340 on object

    I really have no way of knowing. Have you tried to follow through with debugging from the time it errors and then manually return to the caller to see where it came from? Also, if it isn't too much trouble can you zip up your project and post it? I would be able to do a lot better in helping you if I can run the code myself otherwise I am just second-guessing everything.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    788

    Re: runtime 340 on object

    Just as soon as it hits the line:
    NoteDate = TextNoteDate(Index).Text it throws the error????

  18. #18
    PowerPoster
    Join Date
    Jan 2008
    Posts
    9,345

    Re: runtime 340 on object

    I don't think you understood my question. When it errors out and while you are still in the debug mode move your pointer down to the End Sub line and then click on Step Into and this will take you back to the caller


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    788

    Re: runtime 340 on object

    I will try that tomorrow in the office when I am debugging it again. Thanks for all your posts, btw.

  20. #20
    PowerPoster
    Join Date
    Jan 2008
    Posts
    9,345

    Re: runtime 340 on object

    Is the GotFocus event called by clicking on a textbox or it called automatically?


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.