Pass String variable from Popup form to Main form in vba (1 Viewer)

JT Kent

Registered User.
Local time
Today, 10:38
Joined
Aug 26, 2010
Messages
50
Hello All,
I need an urgent response to this issue.

My Ms Access application has 2 forms viz:
a) The Main form
b) A popup form

The Main form calls the popup form via a button click. The popup form has a combo box whose value is stored in a global string variable.

I want to reference this string variable from the Main form (with the popup form still running i.e popup.visible = false)

Any help will be greatly appreciated.
 

JT Kent

Registered User.
Local time
Today, 10:38
Joined
Aug 26, 2010
Messages
50
Pass String variable's value from Popup form to Main form

Hello All,I have 2 forms in my Ms Access application viz:1) Popup form2) The Main formThe Main form calls the popup form. The popup form has a combo box whose value is stored in a global variable after triggering an event.How can i use vba to reference this string variable (in the popup form) from the Main form.Help!!!!
 

Steve R.

Retired
Local time
Today, 13:38
Joined
Jul 5, 2006
Messages
4,674
Re: Pass String variable's value from Popup form to Main form

Two options. One is to declare a public variable in a module. The other is to refer to the control in the pop-up to the underlying form.
Code:
Private Sub List1_Click()
    Forms(FormNamestr).facility = Me.List1
    Forms(FormNamestr).Controls(TextNamestr) = Me.List1.Column(1)
End Sub
The code above is based on the use of a drop-down list.
 

jzwp22

Access Hobbyist
Local time
Today, 13:38
Joined
Mar 15, 2008
Messages
2,629
As long as the popup form is open (visible or not doesn't matter), you can get the value in the control whenever you need it, so you should not need to migrate it to the main form nor would you need a global variable. Just reference the form control when you need it

forms!YourPopUpFormNameHere!YourComboBoxControlNameHere
 

JT Kent

Registered User.
Local time
Today, 10:38
Joined
Aug 26, 2010
Messages
50
Thanks for your prompt reply but it appears i still have issues with the code.

The code below gets the value of the combobox in the popup form and stores it in a string variable in the Main form.

[Forms]![Popupform]![Popupcombobox].ItemData([Forms]![Popupform]![Popupcombobox].ListIndex) = StrMainform

Whenever i run this it displays run-time error 2186.
Urgent help please!!!!
 

boblarson

Smeghead
Local time
Today, 10:38
Joined
Jan 12, 2001
Messages
32,059
You can't set a variable on another form directly. You would need to have a function that you can call on that main form (set to public) in order to do it.

Main form:
Code:
Public Function SetVal(strVar As String)
   strMainForm = strVar
End Function

From the popup form:
Code:
Forms!YourMainFormNameHere.SetVal(Me.PopupComboBox)

Don't know why you are using ItemData and all to go that route. The combo box should have the value of the selected item. And, if you are referring to the popup form in its own code, you use ME and not the fully qualified form reference. Much easier that way.
 

JT Kent

Registered User.
Local time
Today, 10:38
Joined
Aug 26, 2010
Messages
50
Re: Pass String variable's value from Popup form to Main form

Thanks for your prompt reply but am very new to Vba programming so i hardly understand your code. But i have now explained what i want to do in a clearer way.

The code below gets the value of the combobox in the popup form and stores it in a string variable in the Main form.

[Forms]![Popupform]![Popupcombobox].ItemData([Forms]![Popupform]![Popupcombobox].ListIndex) = StrMainform

Whenever i run this it displays run-time error 2186.
Urgent help please!!!!
 

boblarson

Smeghead
Local time
Today, 10:38
Joined
Jan 12, 2001
Messages
32,059
Re: Pass String variable's value from Popup form to Main form

This is a duplicate thread. I'm going to merge the two.
 

boblarson

Smeghead
Local time
Today, 10:38
Joined
Jan 12, 2001
Messages
32,059
Oh, and please do not post the same question multiple times.
 

JT Kent

Registered User.
Local time
Today, 10:38
Joined
Aug 26, 2010
Messages
50
Thanks everyone.
I owe you one Bob. It works perfectly.

I would however like to know how to pass multiple string variables from the Popup form to the Main form.

The function you (Bob) posted to me works just with one variable. I need to pass the value of 8 variables to the Main form.

Pardon me, I am a stranger to VBA.
Finally, thanks for the advice.
 

boblarson

Smeghead
Local time
Today, 10:38
Joined
Jan 12, 2001
Messages
32,059
You could do something like this:
Code:
Public Function SetVal(strVar As String)
Dim varSplit As Variant
 
varSplit = Split(strVar, "|")

   strMainForm = strVar(0)
   strVariable2 = strVar(1)
   strVariable3 = strVar(2)
   strVariable4 = strVar(3)
   strVariable5 = strVar(4)
   strVariable6 = strVar(5)
   strVariable7 = strVar(6)
End Function

And then you would call it like:
Code:
Forms!YourMainFormNameHere.SetVal(Me.PopupComboBox & "|" & Me.Control2Name & _
"|" & Me.Control3Name & "|" & Me.Control4Name & "|" & Me.Control5Name & _
"|" & Me.Control6Name & "|" & Me.Control7Name & "|" & Me.Control8Name)
 

JT Kent

Registered User.
Local time
Today, 10:38
Joined
Aug 26, 2010
Messages
50
Thanks Bob for your swift response.

I however have a compile error using the latest set of codes you sent me.
The system says "Expected Array" and the issue starts from the line of code below:

StrMainform = strVar(0)

Am i supposed to declare an array (where and how?)
Thanks
 

boblarson

Smeghead
Local time
Today, 10:38
Joined
Jan 12, 2001
Messages
32,059
What is the code you are trying to use? I gave you what you needed. Did you leave the :
Code:
Dim varSplit As Variant
 
varSplit = Split(strVar, "|")
part in there, EXACTLY as shown?
 

JT Kent

Registered User.
Local time
Today, 10:38
Joined
Aug 26, 2010
Messages
50
Hello Bob,
I did not leave any part out.
I just copied the code as you sent it into my application.

But it keeps telling me Expected Array.
Thanks
 

MarkK

bit cruncher
Local time
Today, 10:38
Joined
Mar 17, 2004
Messages
8,179
I'm pretty sure Bob means ...
Code:
Public Function SetVal(strVar As String)
   Dim varSplit As Variant
 
   varSplit = Split(strVar, "|")

   strMainForm = varSplit(0)
   strVariable2 = varSplit(1)
   strVariable3 = varSplit(2)
   strVariable4 = varSplit(3)
   strVariable5 = varSplit(4)
   strVariable6 = varSplit(5)
   strVariable7 = varSplit(6)
End Function

You can't set a variable on another form directly.
I don't understand this. You can directly set a public variable on another form. If Form1 has a declaration like ...
Code:
Public MyTestVariable as string
Then in a standard module I can run code like ...
Code:
Sub Test1
  DoCmd.OpenForm "Form1"
  Forms("Form1").MyTestVariable = "This is a test!!!"
  MsgBox Forms("Form1").MyTestVariable
End Sub
That public variable is even visible to Intellisense.
 

boblarson

Smeghead
Local time
Today, 10:38
Joined
Jan 12, 2001
Messages
32,059
Thanks Bob for your swift response.

I however have a compile error using the latest set of codes you sent me.
The system says "Expected Array" and the issue starts from the line of code below:

StrMainform = strVar(0)

Am i supposed to declare an array (where and how?)
Thanks

Umm, you have

StrMainform = strVar(0)

but it should say

strMainform = varSplit(0)

So, I would say you did change from what I said.
 

JT Kent

Registered User.
Local time
Today, 10:38
Joined
Aug 26, 2010
Messages
50
Thanks a lot everybody,
It works perfectly. I would also try out your latest suggestion with respect to referencing variables in another form.

Cheers and thanks again.
 

Users who are viewing this thread

Top Bottom