Avagut
Avagut

Reputation: 994

How to open another form within a vba function and return to the function

I have a MS Access form with a button that calls a function that starts by checking a date and asks the user if he needs to manually change the date. If this is the case, another form is opened where the date is entered and validated. Is there a possible way to load the selected date into a variable and return to the first form into the function that is running to use it further down the procedure?

Upvotes: 2

Views: 4821

Answers (1)

Graffl
Graffl

Reputation: 380

The following way is working for me although I assume there is a better one. First of all I inserted a module and declared a global variable:

Global globalText As String

Then you need to make sure the other form (here frmEntry) is called "modal", so that your code will wait until the form is closed again. You can achieve this by:

DoCmd.OpenForm "frmEntry", WindowMode:=acDialog

In frmEntry you need to write whatever value was selected then to the global variable and then close the form, which would look something like this:

Private Sub btnClose_Click()

    globalText = Me.txtEntry.Value
    DoCmd.Close acForm, "frmEntry"

End Sub 

And then it is basically done and your code in your basic form will go on running and you can use the value from globalText anywhere or can write it to some hidden textBox to use it later in other functions.

Upvotes: 2

Related Questions