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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Variable for a form name

Status
Not open for further replies.

ching0418

MIS
Mar 6, 2002
96
HK
hello guys,

i would like to ask a question on how to use a variable as the form name. i'll be using the variable to open the form. for example:

x = PO

instead of using the command PO.Show, i want to use the variable x. how willl i do this?

thanks in advance.

ching
 
include this declaration somewhere in your code:

Dim varForm As New PO

now you may use:

varForm.Show
 
Try this:
Code:
Private Sub Command2_Click()
Dim x As Form
Set x = Form2
x.Show
End Sub
Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 

Remember Jaws the movie? *******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
CCLINT: ?? Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
here's my code:

Dim rsRpt As ADODB.Recordset
Dim sRpt As String
Dim fo As Form

Set rsRpt = New ADODB.Recordset

sRpt = "Select distinct rptfo from RPRTS where rptnm = '" & cboRpt & "'"
rsRpt.Open sRpt, conn, adOpenDynamic, adLockOptimistic

If rsRpt.RecordCount <> 0 Then
Set fo = rsRpt(&quot;rptfo&quot;)
End If
rsRpt.Close
Set rsRpt = Nothing

fo.Show


there's still a type mismatch error on the &quot;Set fo = rsrpt(&quot;rptfo&quot;)&quot; command. The result of rsrpt(&quot;rptfo&quot;) is InRpt...and that's also the name of my form...
 
Hmm. Change
Code:
Set fo = rsRpt(&quot;rptfo&quot;)
to
Code:
Set fo = GetForm(rsRpt(&quot;rptfo&quot;))

Then include this function in a module.

Code:
Public Function GetForm(strName as string) as Form

  Dim frmForm as Form

  set frmForm = Nothing

  For Each frmForm in Forms

    if ucase(trim(frmForm.name)) = ucase(trim(strname)) then

      set GetForm = frmForm
      Exit Function

    endif

  Next frmForm


End Function

The above code should work, but assumes you have the form already loaded in memory...

Let me know if this helps you out.


mmilan
 
You will have to search for a form with the correct name in all your loaded forms like this:

Code:
Dim i As Integer
 
For i = 0 To Forms.Count - 1
    If Forms(i).Name = StringFormName Then
        Forms(i).Show
    End If
Next i

For this you have to make SURE that all needed forms are previously loaded.

Greets,
Thomas
 
a type mismatch will definitely occur because your are trying to assign a recordset value (rsRpt(&quot;rptfo&quot;)) to a form (fo), which is an object
 
&quot;For this you have to make SURE that all needed forms are previously loaded&quot;....
....Therefore, it may not be a good idea.

With-out going into it deeper, try:


'Declarations
Private varForm As New PO
Private varForm As PO

Private Sub SetReportForm()
'ADO code....

If Not (fo Is Nothing) Then
unload fo
Set fo = Nothing
End If

Set fo = nothing
If rsRpt.RecordCount <> 0 Then
Select Case rsRpt(&quot;rptfo&quot;)
Case &quot;Report1&quot;
Set fo = New Report1
Case &quot;Report2&quot;
Set fo = New Report2
End Select

'Close ADO code
End If

fo.Show
End Sub *******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
The following function works whether the form is loaded or not:
[tt]
Option Explicit

Public Function FormByName(strFormName As String) As Form
Dim frm As Form
Dim frmLoaded As Form

' Make sure we only load form once by returning reference
' to form if it is already loaded
For Each frm In Forms
If LCase(frm.Name) = LCase(strFormName) Then
Set frmLoaded = frm
End If
Next
If frmLoaded Is Nothing Then
On Error Resume Next ' skip problem if no form found. Calling procedure should error handle
Set frmLoaded = Forms.Add(strFormName)
On Error GoTo 0
End If
Set FormByName = frmLoaded

' Still nothing? Then no form by this name exists in the project
If FormByName Is Nothing Then
Err.Raise vbObjectError + 512 + 1, &quot;FormByName&quot;, &quot;No such form is available in the project&quot;
End If
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top