TheSwamp

Code Red => VB(A) => Topic started by: Mark on October 17, 2006, 08:47:13 AM

Title: Example code ( userform, module )
Post by: Mark on October 17, 2006, 08:47:13 AM
Does anyone have some simple code that goes from a module ( function ) -> userform and back again?

thanks for putting up with me folks!

Note to self: remember your VB book tomorrow stupid!
Title: Re: Example code ( userform, module )
Post by: David Hall on October 17, 2006, 08:48:01 AM
i do, be right back
Title: Re: Example code ( userform, module )
Post by: David Hall on October 17, 2006, 08:51:19 AM
function or sub?  it shouldn't matter.  Anyway, I couldn't find the exact function I was looking for, but the jist of it is

sub Whatever()
do some stuff

frmFormName.Show
(form does its thing, in the button click event, put me.hide )

Focus returns here

end sub

Title: Re: Example code ( userform, module )
Post by: MP on October 17, 2006, 09:07:14 AM
An aside, Mark if you're looking for a reasonable VB book Francesco Balena's "Programming Microsoft Visual BASIC 6.0" <ISBN 0-7356-0558-0> is worthy.

Back to regularly scheduled programming ...
Title: Re: Example code ( userform, module )
Post by: Mark on October 17, 2006, 09:29:36 AM
Thanks Michael

Ok I have this ( see link ) and all works as, I expect it to.

[ http://www.theswamp.org/screens/mark/images/vba_simple_test-1.png ]

So far so good?
Title: Re: Example code ( userform, module )
Post by: David Hall on October 17, 2006, 09:32:57 AM
MP or Keith might lend a hand here, but I think you would want to change 2 things.  1 would be to make a public sub RunMe instead of a function (Or I could be completely wrong) and 2, does the end in the click event terminate the whole program or just the form?  I thought you would have used a Me.Hide to get rid of the form without exiting the whole program

Hope that helps
Title: Re: Example code ( userform, module )
Post by: MP on October 17, 2006, 09:45:11 AM
<IMO>

I agree with Cmdr, using End is not a good idea (especially if the code finds it way into a dll down the road - very bad indeed). Best to let the caller instantiate and kill the form. Subtitle: The [Exit] button should just hide the form.

I'll try to code up a little framework for you in a bit, tho it may be a stretch for me (I never code in ACAD VBA, only VB6).

:)

</IMO>
Title: Re: Example code ( userform, module )
Post by: Mark on October 17, 2006, 09:53:31 AM
Something like this then?

Code: [Select]
Private Sub CommandExit_Click()
    MyForm.Hide
End Sub
Title: Re: Example code ( userform, module )
Post by: David Hall on October 17, 2006, 09:54:58 AM
Assuming your form name is MyForm then yes, but Me.Hide should work as well.  the Me keyword is nice because VBA knows what has focus, and uses Me in place of MyForm.
Title: Re: Example code ( userform, module )
Post by: MP on October 17, 2006, 10:13:57 AM
Assuming your form name is MyForm then yes, but Me.Hide should work as well.  the Me keyword is nice because VBA knows what has focus, and uses Me in place of MyForm.

Agreed -- one should avoid hard coding as much as possible. You know C++ right Mark? VB[A]'s 'me' is loosely equivalent to C++/C#'s 'this' (forms are just a specialized class).

:)
Title: Re: Example code ( userform, module )
Post by: Mark on October 17, 2006, 10:22:56 AM
VB[A]'s 'me' is loosely equivalent to C++/C#'s 'this' (forms are just a specialized class).

That makes sense!

Thanks guys.
Title: Re: Example code ( userform, module )
Post by: MP on October 17, 2006, 10:43:37 AM
Thanks guys.

Thank you Mark -- theswamp is the best AutoCAD discussion forum on the net!

:)
Title: Re: Example code ( userform, module )
Post by: David Hall on October 17, 2006, 10:48:40 AM
ditto
Bar none, the best place to learn Autocad and programming
Title: Re: Example code ( userform, module )
Post by: Mark on October 17, 2006, 11:58:46 AM
OK, quick ( I hope ) question. Why doesn't the following work?

Code: [Select]
Public Sub RunMe()
    '
    ' test module
    '
    Load MyForm
    MyForm.Show
   
    MyForm.Label1.Caption = "Hello"          <=== here
    MyForm.TextBoxMsg.Text = "what"        <=== here
   
    Unload MyForm
    End
End Sub

This of course does.

Code: [Select]
Private Sub UserForm_Initialize()
    Me.Label1.Caption = "Hello"
    Me.TextBoxMsg.Text = "what"
End Sub
Title: Re: Example code ( userform, module )
Post by: David Hall on October 17, 2006, 12:06:54 PM
question, is MyForm part of the dvb that contains RunMe?  if so, you shouldn't have to load it.
2nd thing - I dont know why it doesn't work.  I will go test it
Title: Re: Example code ( userform, module )
Post by: MP on October 17, 2006, 12:09:09 PM
Try this Mark --

Code: [Select]
Public Sub RunMe()
   
    Dim myFormInstance As MyForm
    Set myFormInstance = New MyForm
   
    With myFormInstance
        .Label1.Caption = "A caption."
        .TextBoxMsg.Text = "Some text."
    End With
   
    myFormInstance.Show
    Unload myFormInstance
   
End Sub
Title: Re: Example code ( userform, module )
Post by: Arizona on October 17, 2006, 12:09:40 PM
Since you are already displaying the form I believe you need to do a "refresh"

Or put the show form statement under the textbox statements

Never mind...I like Michael's answer better! :-)
Title: Re: Example code ( userform, module )
Post by: Mark on October 17, 2006, 12:10:01 PM
question, is MyForm part of the dvb that contains RunMe?  if so, you shouldn't have to load it.
2nd thing - I dont know why it doesn't work.  I will go test it

1) yes
2) OK, thanks.

screen shot coming ....