Results 1 to 8 of 8

Thread: Passing values between forms. [Resolved]

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    4

    Resolved 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

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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.

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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.

  4. #4
    Addicted Member Max_aka_NOBODY's Avatar
    Join Date
    Jul 2004
    Location
    Amman, Jordan
    Posts
    179

    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:
    1. 'Dim the var:
    2.  
    3. Global strKeyPadText
    4.  
    5. 'This is the destinations forms' code:
    6. 'Assume frmKeyPad is the keypad form name
    7. frmKeyPad.Show vbModal
    8. 'Next line will be called only when the keypad is closed:
    9. txtDestinationBox.Text = strKeyPadText
    10.  
    11.  
    12.  
    13. 'This should be put in the unload\terminate event of the keypad, or an Ok button's click for example:
    14. strKeyPadText = txtKeyPad.Text
    Last edited by Max_aka_NOBODY; Mar 9th, 2005 at 06:40 PM.

  5. #5
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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.

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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 like
    VB Code:
    1. Public fForm As Form
    In the form load event of each form, put
    VB Code:
    1. Set fForm = ActualFormName
    2. 'example:   if the form is frmMain then you would have
    3. Private Sub Form_Load()
    4. Set fForm = frmMain
    5. 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:
    1. KeypadDummyVar.Text = fForm.txtAnswer
    Am I understanding the question correctly?

  7. #7
    Addicted Member Max_aka_NOBODY's Avatar
    Join Date
    Jul 2004
    Location
    Amman, Jordan
    Posts
    179

    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.

  8. #8
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    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:
    1. Private Sub Command1_Click()
    2.     Dim frmForm2 As New Form2
    3.     Call frmForm2.DisplayForm(25) ' 25 is Just a test number
    4.     set frmForm2 = nothing
    5. End Sub

    in Form2 it will hit this before the Form_Load:

    VB Code:
    1. Public Sub DisplayForm(iReceivedValue As Long)
    2.     MsgBox iReceivedValue  'Display will be 25
    3.     Me.Show vbModal
    4. 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
  •  



Click Here to Expand Forum to Full Width