Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert a String to an Object 2

Status
Not open for further replies.

EwS

Programmer
Joined
Dec 30, 2002
Messages
398
Location
US
This works:
Dim myForm as Form
Set myForm = frmForm1

This doesn't work:
Dim tempForm As String
tempForm = "frmForm1"
Set myForm = tempForm

I want to read the form name from a database and then assign it to myForm. How to convert from a string to an object type?

Thanks.
 
Dim tempObj As object
Dim tempForm As String

tempForm = "frmForm1"
Set tempObj = CreateObject(tempForm)


____________________________________________________
$str = "sleep is good for you. sleep gives you the energy you need to function";
$Nstr = ereg_replace("sleep","coffee",$str); echo $Nstr;

onpnt2.gif
 
Here's one solution:
[tt]
Public Function FormByName(strForm As String) As Form
Dim myForm As Form

' Check to see if form is already loaded
For Each myForm In Forms
If myForm.Name = strForm Then Exit For
Next

' Load named form if not already loaded
On Error GoTo BadForm
If myForm Is Nothing Then Set myForm = Forms.Add(strForm)
On Error GoTo 0

Set FormByName = myForm
Exit Function

BadForm:
If Err.Number = 424 Then Call Err.Raise(vbObjectError + 424, "FormByName", strForm + ": no such form exists in the project", "", 0)
End Function
 
Thanks for help.

onpnt -
Your code didn't work.

strongm -
It works.
 
sorry, still brushing off the VB dust bunnies. I think setting the object to Nothing may need to be done first.

hope my appearance isn't getting anyone irritable. It's like riding a bike, right?!? [wink]

____________________________________________________
$str = "sleep is good for you. sleep gives you the energy you need to function";
$Nstr = ereg_replace("sleep","coffee",$str); echo $Nstr;

onpnt2.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top