Retrieving Data from A Dialog Form Without Using Globals

From the days of VB3, you no doubt recall the pain of retrievinganswers from a dialog form that you displayed from another form.The use of Globals was about the only way provided. In VB4, formsare objects and you can call methods in them from another formjust as if they were in a code module. This new feature allowsyou to call a function in a dialog form, which will display itself,and when the form is unloaded, return your answers. Add this codeto your main code that needs user input:

         Dim sAnswer As String        sAnswer = frmDialog.Display()        'when control returns sAnswer         'will have the user reply.

To your frmDialog (.frm) file, add this code:

         Dim sRetValue As String        Public Function Display() As String                Me.Show vbModal                 Display = sRetValue        End Function        Private Sub cmdOK_Click()                ' This function must set up the                 ' return value because the                ' Text box will be unloaded when 	                ' "Display" regains control	           sRetValue = Text1.Text		Unload Me	End Sub

Obviously, you can retrieve data from more than one control. In thatinstance you would pass an object ot array to the Display functionfor it to retun multiple values. Userdefined types (UDTs) don’t appear to work.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: