Click to See Complete Forum and Search --> : Run Time Error 340
Chuck Sweet
Sep 26th, 2000, 03:03 PM
I posted this under 'General VB Topics' also, but I thought one of you guys has probably run into this problem also. Here it is:
I've got a form that uses an array of controls (commandbuttons) called cmdOptions. Sporadically, the form will not load and it will give me an error of "Control Array Element '0' doesn't exist". The control is created at design time, so this error definitely makes no sense. The only thing I could think of is this: I load the form and then immediately access a user-defined function on it that accesses the control. If the form doesn't load quick enough, the I can see the function getting to the point where it needs the control and it not being there. Basically, I need a way around this little problem.
-chuck
666539
Sep 26th, 2000, 04:17 PM
I think you can say something like
if cmdbtn(x).visible then <your procedure>
If it is visible then it is loaded. I've used this before with a data control, but I don't know if this sugestion will be compatable with the prodedure runing on the buttons.
I assume you call the procedure in the form_load event. You may want to try puting the call in another event. Maybe the from_initialize. (my first sugestion is probably better)
Let me know if this works.
Chuck Sweet
Sep 27th, 2000, 07:57 AM
I call the procedure from another function on another form. See, here's how it works:
'on frmMain
Private Sub mnuList_Click()
Dim mList
'here is where I load the form by calling the function
mList = frmOne.BuildList(Id)
End Sub
'on frmOne
Public Function BuildList(Id As String) As Boolean
Me.Caption = "Build"
'**************************
'*HERE'S WHERE IT BLOWS UP*
'**************************
cmdOptions(0).Caption = "BUILD"
'***********************************************
'* IT TELLS ME THAT cmdOptions(0) DOESN'T EXIST*
'* SOMETIMES... *
'***********************************************
'***********************************************
'* KEEP IN MIND THAT A LOT MORE HAPPENS HERE. *
'* THE ONLY THING THAT EVER HAPPENS TO THE *
'* BUTTON IS THAT IT IS MADE INVISIBLE. *
'***********************************************
End Function
Nothing else happens to that button. It is the first one in the list of things to do, so maybe the button sometimes isn't loaded quick enough?
-chuck
Chuck Sweet
Sep 27th, 2000, 09:19 AM
Thanks to all who replied. I'm not sure, but I think the code problem had something to do with multitasking capabilities of NT. mlewis was right in that the controls should load before any code executes. but i think when the code is actually executed, some strands execute simultaneously (with checks of course to see if these strands affect each other). Here's how I fixed it, in case anyone else has this problem:
instead of just calling the method of the form to load it, ie. formOne.BuildList, do this-
'this will start the create a new instance
dim frmTwo as new formOne
'allow any events to fire that need to
doevents
'finally call method
frmTwo.BuildList
the funny thing is that this error didn't start to happen until after a data access routine (the two shouldn't be related in the least). again, thanks to all who responded.
-chuck
Lafor
Sep 28th, 2000, 01:11 PM
Chuck...
Just saw this.. but my 2 cents.. Try the changes in
between ****
and let me know
Take care!
-------------
'on frmMain
Private Sub mnuList_Click()
Dim mList
'here is where I load the form by calling the function
**********************************
dim thisform as new frmOne
with thisform
.load
.BuildList
end with
**************************************
End Sub
'on frmOne
Public Function BuildList(Id As String) As Boolean
Me.Caption = "Build"
'**************************
'*HERE'S WHERE IT BLOWS UP*
'**************************
cmdOptions(0).Caption = "BUILD"
'***********************************************
'* IT TELLS ME THAT cmdOptions(0) DOESN'T EXIST*
'* SOMETIMES... *
'***********************************************
'***********************************************
'* KEEP IN MIND THAT A LOT MORE HAPPENS HERE. *
'* THE ONLY THING THAT EVER HAPPENS TO THE *
'* BUTTON IS THAT IT IS MADE INVISIBLE. *
'***********************************************
End Function
Nothing else happens to that button. It is the first one in the list of things to do, so maybe the button sometimes isn't loaded quick enough?
-chuck
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.