Re: Loading a form with code




"LordSith" <LordSith.34axxs@xxxxxxxxxxxxxxxxxxxxx> wrote

What the ... is wrong with you people?

Funny you should ask that, replying as you did, to a post from 2005 !!!


How can I reference to the parent form controls through just using the
form name?

That is not the issue for you. The solution you want is the returned values
regardless of which form it was called from. Their problem was storing the
text name, and trying to show the form from just a text reference.

To solve your problem, consider the example below. To run the example,
add a command button to Form1 and set the command button's Index
property to 0. Then add Form2 to the project and to that form, add a
Lable, a Textbox and a Command button, setting all their Index properties to
0. Now paste in the code below in their respective forms, and see what it
does. Note that Form2 never learns who called on it to perform its task....

LFS


'==== Form1 code ====
Option Explicit

Private Sub Command1_Click(Index As Integer)
Dim qry As New Collection
Dim usr As Collection, itm
' Pretend each button is a call from a different form:
Select Case Index
Case 0 ' Name
qry.Add "First Name"
qry.Add "Middle Initial"
qry.Add "Last Name"
Case 1 ' Address
qry.Add "Street Address 1"
qry.Add "Street Address 2"
qry.Add "City"
qry.Add "State / Province"
qry.Add "Zip Code"
Case 2 ' Auto
qry.Add "Make"
qry.Add "Model"
qry.Add "Year"
qry.Add "Color"
End Select

' Each form has this code, no mention of
' which form it came from
Set usr = Form2.UserQuery(qry)
Set Form2 = Nothing

' See the returned result
If Not usr Is Nothing Then
For Each itm In qry
Debug.Print itm; ": "; usr(itm)
Next
Else
Debug.Print "User aborted"
End If
End Sub

Private Sub Form_Load()
Dim idx As Long
For idx = 0 To 2
If idx Then
Load Command1(idx)
Command1(idx).Visible = True
End If
Command1(idx).Move 90 + idx * 840, 90, 750, 360
Command1(idx).Caption = VBA.Array("Name", "Address", "Auto")(idx)
Next
End Sub



' ==== Form2 code =====
Option Explicit

Private Result As Collection

Public Function UserQuery(Query As Collection) As Collection
BuildForm Query
Me.Show vbModal
Set UserQuery = Result
End Function

Private Sub BuildForm(Query As Collection)
Dim idx As Long, itm As Variant
Text1(0).Text = ""
Me.Caption = "User Information"
For Each itm In Query
If idx Then
Load Label1(idx)
Load Text1(idx)
Label1(idx).Visible = True
Text1(idx).Visible = True
End If
Label1(idx).Move 90, 90 + idx * 570, 1800, 210
Label1(idx).Caption = itm
Text1(idx).Move 90, 330 + idx * 570, 1800, 240
Text1(idx).Tag = itm
idx = idx + 1
Next
Command1(0).Move 2200, 330, 1200, 360
Command1(0).Caption = "OK"
Load Command1(1)
Command1(1).Visible = True
Command1(1).Move 2200, 720, 1200, 360
Command1(1).Caption = "Cancel"
Me.Height = (Me.Height - Me.ScaleHeight) + idx * 570 + 300
Me.Move (Screen.Width - 3600) / 2, (Screen.Height - Me.Height) / 2.5, 3600
End Sub

Private Sub CollectData()
Dim ctl
Set Result = New Collection
For Each ctl In Me.Controls
If TypeOf ctl Is TextBox Then
Result.Add ctl.Text, ctl.Tag
End If
Next
End Sub

Private Sub Command1_Click(Index As Integer)
If Index = 0 Then CollectData
Unload Me
End Sub



.


Loading