Treat a Form Like a Function

Treat a Form Like a Function

Some forms are merely dialog boxes that show something to the user and sometimes get something in return. For example, you might have to create a form that displays a tabulated listbox of information?contacts, for example. The form needs to know which item should be selected initially, and you want to know which item the user chose in the end. You can share this information through public variables, but wrapping the form into a function proves a better way. Create a standard EXE project and add an extra form to it. Place a command button on the first form and a listbox with a button on the second. Place this code in the first form:

 Private Sub Command1_Click()	MsgBox Form2.ShowList(4)End Sub

Place this code in the second form:

 Dim iSelectedIndexPrivate Sub Command1_Click()	Me.HideEnd SubPrivate Sub Form_Load()	Dim i As Long	For i = 0 To 20		List1.AddItem "Item " & i	Next i	List1.ListIndex = iSelectedIndexEnd Sub' This is where the magic happens. Note that we have to ' display a modal form to prevent the continuation of this ' function until we are ready.Public Function ShowList(Initial As Integer) As String	iSelectedIndex = Initial	' Store the parameter for later use	Me.Show vbModal	' Display the form	ShowList = List1.List(List1.ListIndex)	' Return	Unload MeEnd Function

This technique not only lets you avoid using public variables, but it also gives you excellent portability because you can simply copy the form into a different project. None of the code is affected. You have wrapped a form into a function.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes