|
-
Mar 9th, 2005, 04:40 PM
#1
Thread Starter
New Member
Passing values between forms. [Resolved]
I created a form that acts as a numeric entry keypad. This same keypad can be called up from multiple forms. I can pass the name of the calling form to the keypad as well as the name of the text box that I want to place the value that is acquired from the keypad. My question is: how can I use these known values of calling form and text box to place the keypad value into.
i.e. "Calling Form"."Text Box".Text = KeypadValue.Text
I can't seem to string together the name of the calling form and the text box without getting some kind of error. If I try to string them together before assigning the value of KeypadValue.Text, I still can't seem to do it.
i.e. KeypadDummyVar.Text = CallingForm & "." & TextBox & ".Text"
KeypadDummyVar.Text = KeypadValue.Text
This merely re-assigns a value for KeypadDummyVar.Text
I'm sure that there is a simple way to do this, but I seem to have hit a mental block on coming up with other options.
I'm trying to avoid placing multiple if-then-else statements in my Keypad form
code to figure out where the Keypad.text goes. (i.e. what form it goes to and what text box on that form should display the value.)
************************
I'll try to clarify:
On multiple forms, I am calling the Keypad form to enter values into the calling form. I've defined 2 textboxes on the keypad (hidden), to hold the name(s) of the calling form and the text Item on the calling form to which the keypad value will be plugged into. The code behind the control on the calling form looks something like this:
Private Sub ChgUpperKO1Offset_Click()
Keypad.CallingForm.Text = "SystemSpecs"
Keypad.CallingText.Text = "UpperKO1Offset"
Keypad.Show
End Sub
The Keypad value is stored in a text box called "NewValue". What I would like to do, is when the enter button on the keypad is pressed, plug NewValue directly in to the CallingText box on the CallingForm.
i.e., for the example above, the code for the enter button on the keypad would be interpreted as follows:
Private Sub EnterButton_Click()
SystemSpecs.UpperKO1Offset.Text = EnterButton.Text
End Sub
But I don't know how to "string together" the destination values without the tedious programming such as this:
Private Sub EnterButton_Click()
If CallingForm.Text = "SystemSpecs" Then
If CallingText.Text = "UpperKO1Offset" Then
SystemSpecs.UpperKO1Offset.Text = EnterButton.Text
Else If CallingText.Text = "UpperKO2Offset" Then
.
.
.
End If
Else If CallingForm.Text = "SystemSettings" Then
If CallingText.Text = "..............
I think you get the jist. Did I mention I am a fairly novice VB programmer? (I bet you couldn't tell!)
Thank you for the suggestions so far. I will continue to plug away.
End Sub
Last edited by mairhead; Mar 11th, 2005 at 08:38 AM.
Reason: close
-
Mar 9th, 2005, 04:50 PM
#2
Re: Passing values between forms.
You can make your keypad form as a custom control, then use that control on other forms that you need it on. Store the data you need to pass in a property.
Similar to what CommonDialog control does. For instance you call ShowOpen, you get a window, select a file and the selected file is stored in the FileName property.
-
Mar 9th, 2005, 05:12 PM
#3
Re: Passing values between forms.
I'm a little confused by your question but it looks to me that for example you want to get a value that's on form2 and use it on form1. If so then in form1 you would do something like
MyTextbox.Text = form2.OtherTextbox.text
If that's not it then please try to explain again.
-
Mar 9th, 2005, 06:32 PM
#4
Addicted Member
Re: Passing values between forms.
i.e. KeypadDummyVar.Text = CallingForm & "." & TextBox & ".Text"
Those names aren't really strings, as when you compiled, they're assigned adresses AFAIK. You would use something like:
Code:
DestinationForm.DestinationTextBox.Text = SourceTextBox.Text
EDIT: On a different note, I'd just suggest making a global variable to hold the source text from your keypad, and calling it modally(is that a word?). IE something like this:
VB Code:
'Dim the var:
Global strKeyPadText
'This is the destinations forms' code:
'Assume frmKeyPad is the keypad form name
frmKeyPad.Show vbModal
'Next line will be called only when the keypad is closed:
txtDestinationBox.Text = strKeyPadText
'This should be put in the unload\terminate event of the keypad, or an Ok button's click for example:
strKeyPadText = txtKeyPad.Text
Last edited by Max_aka_NOBODY; Mar 9th, 2005 at 06:40 PM.
-
Mar 9th, 2005, 09:04 PM
#5
Re: Passing values between forms.
I think he know how to pass data between forms but his problem is that he will be using that Keypad form from many other forms, so he wants to avoid a long IF block (or Select Case) where he will examin the string holding the info on what form to pass the data back to and anothoer block (within previous one) to determine to which control to pass it back to on that form.
That's why I suggested what I did.
-
Mar 10th, 2005, 06:45 AM
#6
Re: Passing values between forms.
I hope I'm getting the issue correct. It sounds like he has no way of knowing what form will require the benefit of his calculator, so how about this suggestion.
Add a module, and a public variable likeIn the form load event of each form, put
VB Code:
Set fForm = ActualFormName
'example: if the form is frmMain then you would have
Private Sub Form_Load()
Set fForm = frmMain
End Sub
On each form, have a textbox name the same thing; something like txtAnswer.
With this set up, to pass an answer, all he would have to say is:
VB Code:
KeypadDummyVar.Text = fForm.txtAnswer
Am I understanding the question correctly?
-
Mar 10th, 2005, 08:00 AM
#7
Addicted Member
Re: Passing values between forms.
Well, maybe I'm wrong, but from what I know, using my approach he can add just one line to each form whenever the keypad is called, and it will work.
-
Mar 10th, 2005, 08:43 AM
#8
Re: Passing values between forms.
I imagine there are reasons not to do this, there usually are to my answers, but sometimes I use this.
from a command button on Form1:
VB Code:
Private Sub Command1_Click()
Dim frmForm2 As New Form2
Call frmForm2.DisplayForm(25) ' 25 is Just a test number
set frmForm2 = nothing
End Sub
in Form2 it will hit this before the Form_Load:
VB Code:
Public Sub DisplayForm(iReceivedValue As Long)
MsgBox iReceivedValue 'Display will be 25
Me.Show vbModal
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|