passing parameters between forms

  • Thread starter Thread starter Jan Warning
  • Start date Start date
J

Jan Warning

How do I pass an argument from one form to another?
I do the following:
FormA:
dim strFilename as string ="Filename"
dim frmLayout1 as New frmLayout(strFilename)
frmLayout1.Show()

frmLayout:
Private strFilename as string
..
..
Sub New(Filename as string)
..
..strFilename = Filename
..
..
Private Sub frmLayout_Load(...)
dim strLayout as new Filestream(strFilename, FileMode.Open)

At this point I get an error because strFilename is empty (Nothing)

When I step thru the code, I can see that the parm is correctly passed to
and stored by the Sub New
What am I doing wrong?
 
You could either use a property (in a Module) and access the value there
(from any form) or

In Form A:

Private sub OpenNewForm()
Dim strValue as string
Dim frm as FormB
frm.GetDetails(strValue)
frm = nothing
End Sub


In Form B

Private mstrValue as string

Public Sub GetDetails(byval vstrValue as string)
mstrValue = vstrValue
me.show
end sub

Gerry
 
Correction to my last post

Dim frm as FormB

should have read

Dim frm as New FormB

Gerry
 
I tried it (I noticed your mistake) and it worked. Thanks a lot.
But I am still wondering why my original code did not work.

Regards, Jan Warning
 
Yeah.. Your original would have been better... Was it the real code
(copy/paste) ? If so can you reproduce this behavior in a new project ?
 
It had to replace a defective harddisk, so I could not reply any sooner.

I could reproduce this behavior in a small new project.
I also discobvered wahat I did wrong, although I still don't quite
understand it.

I passed the parameter from FormA to FormB as follows:
Private Parm as string
dim myFormb as New FormB(Parm)
FormB.Show

In FormB:
Private Filename as string
..

Public Sub New(byval Filename as string)
Filename = Filename
etc.

Public Sub Form2_Load .....
xxxx = Filename

Now filename appears to be empty (Nothing)

when I change the code to"
Private myFilename as string
..

Public Sub New(byval Filename as string)
myFilename = Filename
etc.

Public Sub Form2_Load .....
xxxx = myFilename

then everything is fine.

as I said, I don't quite understand, but it works.

Best regards, Jan Warning
 
Jan Warning said:
It had to replace a defective harddisk, so I could not reply any sooner.

I could reproduce this behavior in a small new project.
I also discobvered wahat I did wrong, although I still don't quite
understand it.

I passed the parameter from FormA to FormB as follows:
Private Parm as string
dim myFormb as New FormB(Parm)
FormB.Show

In FormB:
Private Filename as string
.

Public Sub New(byval Filename as string)
Filename = Filename
etc.


Names are resolved inside out. "Filename = Filename" assigns the argument to
the argument, i.e. it changes nothing. To change the field of the Form,
write

me.filename = filename

Public Sub Form2_Load .....
xxxx = Filename

Now filename appears to be empty (Nothing)

when I change the code to"
Private myFilename as string
.

Public Sub New(byval Filename as string)
myFilename = Filename
etc.

Public Sub Form2_Load .....
xxxx = myFilename

then everything is fine.

as I said, I don't quite understand, but it works.


"myfilename = " accesses the field of the Form because there is no local
variable with the name "myfilename"

Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

System icons in Listview 6
passing variable to function 3
Trying to connect to a Lotus .WK1 file 1
VBA Save 3
Writing structure data to binary file 1
BeforeSave 7
BeforeSave Event 5
ADO and Delimited Text 2

Back
Top