Solved

Pass txtVariables to stub form

Posted on 2002-07-18
13
206 Views
Last Modified: 2010-05-02
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.
0
Comment
Question by:mgfranz
  • 4
  • 3
  • 2
  • +2
13 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7163828
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
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 7163829
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
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 7163832
>>txtFirst As Single
>>txtSecond As Single

these should be declared as STRING.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7163844
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
 
LVL 18

Author Comment

by:mgfranz
ID: 7163882
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
 
LVL 18

Author Comment

by:mgfranz
ID: 7163907
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Expert Comment

by:Amitabh
ID: 7163938
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
 
LVL 2

Expert Comment

by:priya_pbk
ID: 7164055
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
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 50 total points
ID: 7164459

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
 
LVL 18

Author Comment

by:mgfranz
ID: 7165158
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
 

Expert Comment

by:Amitabh
ID: 7166256
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
 
LVL 2

Expert Comment

by:priya_pbk
ID: 7166284
>>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
 
LVL 18

Author Comment

by:mgfranz
ID: 7166895
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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now