Author Topic: Generic VBA issue  (Read 1945 times)

0 Members and 1 Guest are viewing this topic.

rogue

  • Guest
Generic VBA issue
« on: August 19, 2008, 06:51:19 AM »
Im (trying to) write some module-level code to perform operations on a form, with a control on the form being passed.
For the purposes of this demonstration, Im using a Frame, and passing the Frame object as a parameter to the sub, all inside the same module.

I was/am having a problem getting the (correct?) parent object of the frame. If I explicitly dim the parent as "MSForms.UserForm", or just "UserForm", I am unable to get the caption property of the form. But when i accidentally misspelled the Object variable in my DIM statement, which will now make the non-dimmed version of the variable a VARIANT, it works! (Take THAT, Option Explicit!)

Im not sure why this is working/not working. To reproduce this, Drop in a form, and a frame. Use the default naming for the Frame, "Frame1". Then drop this sub into the form module:

' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Sub test(objFrame As Frame)

     MsgBox objFrame.Parent.Caption ' this works, without getting the object
   
     Dim tmpMSForm As MSForms.UserForm
     Set tmpForm = objFrame.Parent    'if you change "tmpForm" to "tmpMSForm" as declared, it doesnt work!
     MsgBox tmpForm.Caption

End Sub
' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Call the sub from the userform.initialize event, passing the frame as a parameter:

Private Sub UserForm_Initialize()
test Frame1
End Sub

Notice that both message box calls work. But, in the "test" subroutine, if you change the undeclared "tmpForm" variable (Variant)
to "tmpMSForm" (declared as MsForms.Userform, or even declared as "UserForm"), the caption property comes back empty.

Why is this happening?




Bryco

  • Water Moccasin
  • Posts: 1883
Re: Generic VBA issue
« Reply #1 on: August 19, 2008, 09:55:22 AM »
Does me.caption work?
Also I think you would dim it as a userform1 if you were to dim it, and you shouldn't

rogue

  • Guest
Re: Generic VBA issue
« Reply #2 on: August 19, 2008, 12:28:30 PM »
>> Does me.caption work?

Well, that would be cheating - because all of this is going to end up in a code module. For the purposes of isolating this issue, I stripped everything down.

>>Also I think you would dim it as a userform1 if you were to dim it, and you shouldn't

I didnt dim it as userform1, because the name of the form will not be known until runtime. Basically, this is going to end up as a code module one can drop into a VBA project, pass  a frame control to a sub in the module, and the rest of the code will customize the form thru API calls.