Author Topic: Example code ( userform, module )  (Read 4307 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Example code ( userform, module )
« 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!
TheSwamp.org  (serving the CAD community since 2003)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Example code ( userform, module )
« Reply #1 on: October 17, 2006, 08:48:01 AM »
i do, be right back
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Example code ( userform, module )
« Reply #2 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

« Last Edit: October 17, 2006, 09:19:09 AM by CmdrDuh »
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Example code ( userform, module )
« Reply #3 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 ...
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Re: Example code ( userform, module )
« Reply #4 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?
TheSwamp.org  (serving the CAD community since 2003)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Example code ( userform, module )
« Reply #5 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
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Example code ( userform, module )
« Reply #6 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>
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Re: Example code ( userform, module )
« Reply #7 on: October 17, 2006, 09:53:31 AM »
Something like this then?

Code: [Select]
Private Sub CommandExit_Click()
    MyForm.Hide
End Sub
TheSwamp.org  (serving the CAD community since 2003)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Example code ( userform, module )
« Reply #8 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.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Example code ( userform, module )
« Reply #9 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).

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Re: Example code ( userform, module )
« Reply #10 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.
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Example code ( userform, module )
« Reply #11 on: October 17, 2006, 10:43:37 AM »
Thanks guys.

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

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Example code ( userform, module )
« Reply #12 on: October 17, 2006, 10:48:40 AM »
ditto
Bar none, the best place to learn Autocad and programming
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Re: Example code ( userform, module )
« Reply #13 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
TheSwamp.org  (serving the CAD community since 2003)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Example code ( userform, module )
« Reply #14 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
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)