Pangu
Pangu

Reputation: 3819

How to get the properties of the current active form from a class module?

I currently have 2 forms that contains a List Box of records with some Text Box to allow user input. I have a Command Button that inserts the data input into a table, then I requery the List Box which is bounded to the table get the updated data.

The functionality code in both forms are the same so I like to reduce as much repetitive code as possible and am basically trying to do something like this issue:

How to get instance of actual form from MS Access in a Module (VBA)

I have created a class module to try and take the data input in the Text Box from the currently active form and store the value of each Text Box in each property of the class, then reference that in my form's On Click save command.

Option Explicit

Public number As String
Public name As String

Public Function getData()

    Dim frmCurrentForm As Form
    Dim frmName As String

    Set frmCurrentForm = Screen.ActiveForm
    frmName = frmCurrentForm.Name

End Function

I am able to get the current active form's name, but am unsure where to go from here to get the form's Text Box property data input, which as an example, are named textBox_Number and textBox_Name?

The answer for the above issue states to do the following:

Public Sub mFormLoad(p_form As Form)
    'do stuff with p_form
end sub

But using the p_form object to access its property, I can only find the generic properties of a Form, and not specific properties tied to my active form, i.e. textBox_Number and textBox_Name.

Being new to VBA programming, can someone guide me through?

Thanks!

Upvotes: 1

Views: 2126

Answers (1)

Erik A
Erik A

Reputation: 32682

Use the Controls collection:

p_form.Controls("textbox_Number").Value should refer to the value of your textbox.

Alternatively, you could only take a specific form as a parameter:

Public Sub mFormLoad(p_form As Form_MyFormName)
    'do stuff with p_form
End Sub

Upvotes: 1

Related Questions