Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access- Transferring values across forms

Status
Not open for further replies.

qureshi

Programmer
Mar 17, 2002
104
AU
Hi,
Looks like a pretty basic thing but I cant get it right.
I am working on a access vba project. This project has two forms. Currently in the vba editor the forms show up as:

Form_Bonus Calculation Main Form and
Form_Form1

I have declared a public variable in the Main Form called Current_Week.

At some stage in the processing, Form_Form1 is displayed. This form only has one textbox and the user is supposed to type a value into it.
I want to transfer this value into the Current_Week variable.

I thought something like this should work:

Form_Bonus Calculation Main Form.current_week

or

Current_Week = form_Form1.text0.value

The first option generates an error while the second option just has a Null in the Current_Week variable.

Is there any help available.
Thanks

 
Try something like this from a relevant event in Form1:

[tt]forms("Bonus Calculation Main Form").Current_Week = me("text0").value[/tt]

- using the form names for referencing - note also that the "Bonus Calculation Main Form" need to be open to address the variable

Roy-Vidar
 
Thanks RoyVidar for that. I have another question.
The code is something like this:

On form 1
Private Sub Command2_Click() ------------------ (1)

Forms(0).Current_Week = Me.Text0.Value
If IsNull(Me.Text0) Or Me.Text0 = 0 Then
MsgBox "Week Number Not selected", vbInformation
End
End If

Forms(1).Visible = False
End Sub

On form 2:
Private Sub Command36_Click()
Open_Form

Debug.Print Current_Week
DoCmd.Close acForm, "Form_Form1", acSaveNo

End
End sub

Sub Open_Form()
DoCmd.OpenForm "Form1", acNormal
End Sub

The problem is:
The value of Current_Week is one step behind. i.e If I enter 23 it will be displayed on the second run. It appears that the code does not go to the point marked as ------(1) above on its first run.

I hope I have explained the problem.

Thanks

 
Sorry qureshi, but I'm a little confused.
When you say 1st run, you mean, On Form1, you click command button, which populates, public variable(Current_Week) from MainForm.
2nd step, you click cmdButton on MainForm to evaluate(debug.print) the variable.

As a side, I don't believe, you need to hide, re-open, then close Form1, after the variable has been populated.
As RoyVidar said, when working from Form1, MainForm needs to be open to reference its variable.

...so, if I have your steps correctly, is MainForm open during the initial population of Current_Week?

It is perplexing (at least to me), that the variables value, is a step behind.

Again, if your first step is to open Form1, then MainForm...
it stands to reason, that the first run does not populate the variable.

I'm speculating here, qureshi, bear with me if I'm stating the obvious.

...sorry, one more question to make sure I understand.
The variable doesn't always stay a step behind does it, even after 4 consecutive runs?

Either way, good luck! I'll try & stay tuned!
 
Thanks for the reply
Actually, form2 is the main form. its open at all times. When the user clicks on its command button, it is supposed to open up form1. The user then enters a value in the textbox. Presses OK. The form1 closes and the value in the textbox is transferred to the current_week variable.

Atleast this is what I am planning to do.

Yes, the variable stays behind one step even after 10 consective runs.

1 run textbox value entered 2
current_week value is Null

2nd run textbox value entered 3
current_week is 2

3rd run textbox value 10
current_week is 3

Hope that makes things a bit clear.
 
Ok, again, a little confusing, I think you are using the wrong sequence of events, because , all in the same procedure, you open Form1 from MainForm, but before anything gets entered, you Debug.Print the variable, then Close the Form1? How does Form1 stay opened at all, unless from another source.
but, obviously, somehow it does, I don't see a what point, you populate the variable.
Unless I am missing something very obvious, MainForm is calling sub Procedure to Open_Form1, then debug.Print a variable which hasn't been populated yet, then you close the form.

If I'm just completely lost qureshi, don't worry, I'll wait to read further replies, but if I'm on to something(one way or another), then feel free to respond!

thx & good Luck!
 
I don't quite understand what you're doing. Just having a go again:

Let's say you have a button on your main form opening "form1", use the acdialog option to halt code execution until "form1" is closed

[tt]docmd.openform "form1",,,,,acdialog
debug.print me.current_week[/tt]

In form1 you have a close/ok button with the following:

[tt]if isnull(me("Text0").value then
msgbox "please enter a value"
else
forms(0).current_week=me("Text0").value
docmd.close acform, me.name
end if[/tt]

I think those two routines should be sufficient for what you've described. One of the challenges with your click routine, is that when not using the acdialog option, the code within the click event runs, providing the value currently in the week_number form public, it doesn't wait until "form1" is closed.

Roy-Vidar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top