[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?
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
Re: runtime 340 on object
what is the object that is passed as objbox?
are you doing this in VBA?
Re: runtime 340 on object
This is a vb6 application. The object is a TextBox.
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.
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...........
Re: runtime 340 on object
are some of the textboxes members of a control array? are they the ones that error?
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.
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
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.
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 ?
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
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?
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
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?
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.
Re: runtime 340 on object
Just as soon as it hits the line:
NoteDate = TextNoteDate(Index).Text it throws the error????
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
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.
Re: runtime 340 on object
Is the GotFocus event called by clicking on a textbox or it called automatically?