open form sending value?

  • Thread starter Thread starter Co
  • Start date Start date
C

Co

Hi All,

I need to open a form and send a value with it.
I think it is something with Property but I don't know how to do it.
I click a Listview and open my form. The ID (int) from the Listview
needs
to go to the form so I can run this code:

Dim sql As String = "SELECT * FROM Bestanden WHERE Id=" & DocID

Can anyone help me.

Marco
 
Co said:
Hi All,

I need to open a form and send a value with it.
I think it is something with Property but I don't know how to do it.
I click a Listview and open my form. The ID (int) from the Listview
needs
to go to the form so I can run this code:

Dim sql As String = "SELECT * FROM Bestanden WHERE Id=" & DocID

Can anyone help me.

Marco


You can create a new constructor that takes a parameter:

Public Sub New(ByVal id As String)
InitializeComponent()
' do somthing with ID...
End Sub
 
You can create a new constructor that takes a parameter:

    Public Sub New(ByVal id As String)
        InitializeComponent()
        ' do somthing with ID...
    End Sub

Thanks Mike,

where should I put this code?
In the basic form or the form I want to open?

Could you help me a bit more?

Marco
 
You can create a new constructor that takes a parameter:

Public Sub New(ByVal id As String)
InitializeComponent()
' do somthing with ID...
End Sub

-> Thanks Mike,
->
-> where should I put this code?
-> In the basic form or the form I want to open?
->
-> Could you help me a bit more?
->
-> Marco


This code goes in the form that you want to open. In that form you also
then need to save the value passed in so that it can be used in your SQL
statement. You didn't specify how you want to execute that statement, so
how you save it is open to discussion.

So, if you have Form1, which is the main form, and Form2 which is the form
doing the SQL call, your code might look something like this:

Public Class Form1
Private Sub Button1_Click _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Dim f As New Form2("123XYZ")
f.ShowDialog()
End Sub
End Class

Public Class Form2
Public Sub New(ByVal id As String)
InitializeComponent()
' do somthing with ID...
Dim sql As String = "SELECT * FROM Bestanden WHERE Id=" & id
' execute select...
End Sub
End Class


Hopefully this helps explain my thoughts.
 
Family Tree Mike said:
-> Thanks Mike,
->
-> where should I put this code?
-> In the basic form or the form I want to open?
->
-> Could you help me a bit more?
->
-> Marco


This code goes in the form that you want to open. In that form you also
then need to save the value passed in so that it can be used in your SQL
statement. You didn't specify how you want to execute that statement, so
how you save it is open to discussion.

So, if you have Form1, which is the main form, and Form2 which is the form
doing the SQL call, your code might look something like this:

Public Class Form1
Private Sub Button1_Click _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Dim f As New Form2("123XYZ")
f.ShowDialog()
End Sub
End Class

Public Class Form2
Public Sub New(ByVal id As String)
InitializeComponent()
' do somthing with ID...
Dim sql As String = "SELECT * FROM Bestanden WHERE Id=" & id
' execute select...
End Sub
End Class


Hopefully this helps explain my thoughts.

One thing to add. If you add a new constructor with a parameter make sure
you create another with no parameter or you will loose the ability to edit
the form in the designer. Adding a new constructor will remove the default
constructor.

So add as well.
Public Sub New()
InitializeComponent()
End Sub

LS
 
Lloyd said:
One thing to add. If you add a new constructor with a parameter make
sure you create another with no parameter or you will loose the
ability to edit the form in the designer.

Sure? :-) Only if you want to inherit it and open the inherited Form in the
designer.


Armin
 
Armin Zingler said:
Sure? :-) Only if you want to inherit it and open the inherited Form in
the designer.


Armin

Sorry I had it wrong. The designer works but when I try to execute it has
an error.

When I attempt a new constructor without a default (I am using the
application framework), I get a compile error in the OnCreateMainForm sub
created when I create the vb project. Adding the default New removes this
error.

LS
 
Hi All,

I need to open a form and send a value with it.
I think it is something with Property but I don't know how to do it.
I click a Listview and open my form. The ID (int) from the Listview
needs
to go to the form so I can run this code:

Dim sql As String = "SELECT * FROM Bestanden WHERE Id=" & DocID

Can anyone help me.

Marco

Hi Marco,

just create a property inside the form class code like so:

Public Class Form1
Dim _DocID As Integer
Public WriteOnly Property DocID() As Integer
Set(ByVal value As Integer)
_DocID = value
End Set
End Property

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim sql As String = "SELECT * FROM Bestanden WHERE Id=" &
_DocID
End Sub
End Class


from your calling module

dim frm as new form1
frm.docid = 10
frm.show()

hth
diego
 
Back
Top