Pass txtVariables to stub form

I know... [but I'm learning].

How do I pass txtVariables to a 2nd or 3rd stub form?

Say I have these set in a top form;

txtFirst As Single
txtSecond As Single

And I want to pass them to another form, is it just like a regular Function call?

Private Sub frmSecond(txtF, txtS)
' Where txtF is the value passed from txtFirst
' and txtS is the value passed from txtSecond
End

please don't flame me... I have had a really bad day.
LVL 18
mgfranzAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Éric MoreauConnect With a Mentor Senior .Net ConsultantCommented:

This is what you need in frmSecond (this is a public method that will accept parameters from another form and then display - you call this instead of .Show):

Public Sub ShowMe(ByVal p1 As String, ByVal p2 As String)
    Me.Caption = p1 & " - " & p2  'or whatever you want to do with it
    Me.Show
End Sub


And in frmFirst:

Private Sub Command1_Click()
    Call frmSecond.ShowMe(Text1.Text, Text2.Text)
End Sub
0
 
Anthony PerkinsCommented:
This is probably not a good idea (violates about 18 OOP principles), but you can try doing a hack like this:

In Form2

Public txtFirst As Single
Public txtSecond As Single

Then in Form1
Private Sub frmSecond(txtF, txtS)
   Form2.txtFirst = "abc"
   Form2.txtSecond = "xyz"
End Sub

Just don't tell anybody I suggested this <g>
Anthony

0
 
Éric MoreauSenior .Net ConsultantCommented:
It is easier to access controls from sub forms.

for example, from the 3rd form, you can use this syntax (if txtFirst is a control on this form):

msgbox Form1Name.txtFirst.Text

Otherwise, if txtFirst is a PUBLIC variable declared in the first form, you can use:

msgbox Form1Name.txtFirst
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Éric MoreauSenior .Net ConsultantCommented:
>>txtFirst As Single
>>txtSecond As Single

these should be declared as STRING.
0
 
Anthony PerkinsCommented:
Eric's absolutely right my example has a syntax error:
it should be:

In Form2

Public txtFirst As Single
Public txtSecond As Single

Then in Form1
Private Sub frmSecond(txtF, txtS)
  Form2.txtFirst = 12345
  Form2.txtSecond = 67890
End Sub

Anthony
0
 
mgfranzAuthor Commented:
I'm not looking for a hack, all I want to do is pass txt variables to a 'next' form.  In other words, I have 2 forms in my project, frmFirst and frmSecond, in these forms are a number of functions.  In frmFirst I have two text boxes, txtFirst and txtSecond.  I want to pass the input of these txt boxes to frmSecond for manipulation.

This doesn't make sense to me;

Then in Form1
Private Sub frmSecond(txtF, txtS)
 Form2.txtFirst = 12345
 Form2.txtSecond = 67890
End Sub

I need to create a sub named frmSecond in the frmFirst?

(Please keep this to VB101 level...)
0
 
mgfranzAuthor Commented:
OK... here is what I have so far;

<frmFirst>

Private Sub cmdDisplay_Click()
    ' on Click, open the second form
    frmSecond.Show
End Sub

Private Sub frmSecond(txt1, txt2)
   ' pass the variables to the second form
 frmSecond.txt1 = 12345
 frmSecond.txt2 = 67890
End Sub

<frmSecond>

Option Explicit
Public txt1 As Single
Public txt2 As Single

When I click on cmdDisplay_Click, I get an error;

"Compile Error, Argument not Optional"

I really didn't think this was a big deal... pass a global variable to another form in the same project?
0
 
AmitabhCommented:
In the following code you are passing a textbox as a variable from form1 to form2. i don't think it would be difficult for you to use a string variable and not a textbox itself.

In Form1

'===============================================
Private Sub Command1_Click()
Call Form2.abc(Text1)   'Text1 is a texbox
End Sub
'===============================================

In Form2

'==============================================
Public Sub abc(Optional ByRef mytext As TextBox)
MsgBox mytext.Text
End Sub
'===============================================

Hope this is what you want.

Cheers
0
 
priya_pbkCommented:
hi mgfranz,

>>I'm not looking for a hack, all I want to do is pass txt variables to a 'next' form.  In other words, I have 2 forms in my project, frmFirst and frmSecond, in these forms are a number of functions.  In frmFirst I have two text boxes, txtFirst and txtSecond.  I want to pass the input of these txt boxes to frmSecond for manipulation.<<

I dont understand why you need to pass those values thru a procedure at all, when you can access those values of the text boxes directly in different forms, like this

Therefore:
-frmFirst form-> txtfirst and txtSecond text Boxes
-frmSecond form (you need to access the values of txtFirst and txtSecond...is this right?

Let's say you have values 100 in txtfirst and 200 in txtSecond textboxes in form1

Therefore as you want to retreive these values, access them in your second form in aany of the functions or procedures, like this: (I am accessing this in form load event of frmSecond ie second form)

Private Sub Form_Load()
MsgBox frmFirst.frmFirst
MsgBox frmFirst.frmSecond
End Sub

Is'nt this simple enough. you can access the values from the controls(like here the textboxes) of any active form, by giving the formName.theCOntrolName. That's it!

-priya


0
 
mgfranzAuthor Commented:
OK... I think I get it...

So if I need to get any variable regardless of whether it's a txt, pic, etc..., I just use this;

Private Sub Form_Load()
   MsgBox frmFirst.frmFirst
   MsgBox frmFirst.frmSecond
End Sub

Cool.

But what if I need to pass them on a cmd_Click?  Like if a value is entered in txtA and txtB of frmFirst, clicking on the cmd button calls frmSecond.Show, will the entered txt values be available in frmSecond if called like above?
0
 
AmitabhCommented:
Yes you can pass the values to frmsecond and they will be available there.

To add further i have used byref in the example below which means you are directly referring to the values of textboxes in frmfirst and any change to these, would reflect in frmfirst. but if you pass them byval then any change u make in the passed variable would not reflect in the textboxes in frmfirst.

'In frmfirst

'===============================================
Private Sub Command1_Click()
frmsecond.show  ' u can avoid it if u want
Call frmsecond.abc(txtA.text,txtB.text)  
End Sub
'===============================================

'In frmsecond

'==============================================
Public Sub abc(ByRef mytextA As string, ByRef mytextB As string)
MsgBox mytextA
MsgBox mytextB
End Sub
'===============================================

Cheers
0
 
priya_pbkCommented:
>>But what if I need to pass them on a cmd_Click?  Like if a value is entered in txtA and txtB of frmFirst, clicking on the cmd button calls frmSecond.Show, will the entered txt values be available in frmSecond if called like above? <<

yes, absolutely!!! as i said FormName.ControlName. That's it!

-priya

0
 
mgfranzAuthor Commented:
Here is what I ended up using, if there is anything that might make the efficiency better let me know;

<form1>
Private Sub cmdDisplay_Click()
If Not IsNumeric(txtCurrTemp) Or txtCurrTemp = "" Then     ' If input is null output error msg
    MsgBox ("You must enter a Temperature!")
    txtCurrTemp.Text = ""
    txtCurrTemp.SetFocus
ElseIf Not IsNumeric(txtMinWindSpeed) Or txtMinWindSpeed = "" Then     ' If input is null output error msg
    MsgBox ("You must enter a Wind Speed!")
    txtMinWindSpeed.Text = ""
    txtMinWindSpeed.SetFocus
Else
    Call frmWindChill.ShowAll(txtCurrTemp.Text, txtMinWindSpeed.Text)
End If
End Sub

<form2>
Public Sub ShowAll(ByVal txtCurrTemp As Single, txtMinWindSpeed As Single)
    Dim i As Integer, varTemp As Integer, varWindSpeed As Integer, varWindChill As Integer
    frmWindChill.Show
    frmWindSpeed.Hide
    'picCurrentTemp.Print txtCurrTemp; txtMinWindSpeed
    For i = 1 To 10
        varTemp = txtCurrTemp
        varWindSpeed = txtMinWindSpeed
        varWindChill = varTemp - 1.5 * varWindSpeed
        picCurrentTemp.Print " "; varWindSpeed; "mph"; Spc(6); varWindChill; Chr(176); "F"
        txtCurrTemp = varTemp
        txtMinWindSpeed = varWindSpeed + 2
    Next i
End Sub
0
All Courses

From novice to tech pro — start learning today.