TheSwamp
Code Red => VB(A) => Topic started 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!
-
i do, be right back
-
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
-
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 ...
-
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?
-
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
-
<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>
-
Something like this then?
Private Sub CommandExit_Click()
MyForm.Hide
End Sub
-
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.
-
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).
:)
-
VB[A]'s 'me' is loosely equivalent to C++/C#'s 'this' (forms are just a specialized class).
That makes sense!
Thanks guys.
-
Thanks guys.
Thank you Mark -- theswamp is the best AutoCAD discussion forum on the net!
:)
-
ditto
Bar none, the best place to learn Autocad and programming
-
OK, quick ( I hope ) question. Why doesn't the following work?
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.
Private Sub UserForm_Initialize()
Me.Label1.Caption = "Hello"
Me.TextBoxMsg.Text = "what"
End Sub
-
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
-
Try this Mark --
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
-
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! :-)
-
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 ....