Linked Forms

Arg81

Well-known member
Joined
Mar 11, 2005
Messages
949
Location
Midlands, UK
Programming Experience
1-3
Linked Forms - RESOLVED

Is it possible?

I.E. select a record in datagrid, and then click a button to open the form to display relevant information from another table with the same ID?

I can do it via a DataGrid and then setting it to the relationship, but I'd prefer to do it via a form.

Cheers!
Luke
 
Last edited:
Simply open a new form and pass it whatever information it needs to display the data, or set its properties directly from the opening form. Isn't this what you do when open any form?
 
So if I understand you correctly, you're saying that Form 2 should be created using it's own data-adapters etc, and then from Form 1, pass the parameter?

I.E. I have a form which lists Computer Hardware. The key of the Computer Table is the computer name. In a Software Table, the key is the computer name. On the Hardware form I want a button that can be clicked, and the software for that computer is displayed on a new form.

However, the computers are listed by user, and the computers are displayed in a DataGrid. I want to select a row in the grid, press a button, and view the software installed on that PC.

Does that make sense???

Cheers!
Luke
 
If you already have the data required then you can simply pass a DataSet or DataTable to the new form. If you don't already have the data then yes, you should pass the required parameters to the new form and let it retrieve the necessary data from the database itself.

You can use either method. The trade-off is whether you have one big data access at the start, which will take longer and may get data that is never used, or do smaller data access operations as needed, which gets you started quicker but may make the app seem more sluggish as as each new form is opened.
 
Cool.

I'll go for the parameter version. Only thing is, how do I take out the parameter (ComputerName) from a selected row in the datagrid? I understand how to do it with textboxes, but the computers are listed in a grid instead.

Ta!
Luke
 
VB.NET:
Dim columnIndex As Integer 'Put the index of the ComputerName column here.
Dim selectedComputerName As String = CStr(Me.DataGrid1.Item(Me.DataGrid1.CurrentRowIndex, columnIndex))
 
You are a legend.

Thanks for the help.

Maybe I've still done it wrong but it works. Basically I have added a variable to my global list called ComputerName.

Then on form1 with the datagrid I use a button with:

VB.NET:
 Dim columnindex As Integer = 0 

Dim SelectedComputerName As String = CStr(Me.grdComputers.Item(Me.grdComputers.CurrentRowIndex, columnindex))

SelectedComputer = SelectedComputerName

Dim frmSoft As New frmCompSoftware

frmSoft.Show()

And on form2 which loads the software I changed the dataAdapter to have WHERE NetBIOS = @NetBIOS
and loaded the adapter with the value of @NetBIOS = SelectedComputer


thanks!
 
Error Trapping

JM,

I'm just trying to put some error trapping in, and can't get my head round this - hopefully you'll have an idea of what I mean.

I've taken what you helped me with and created another form for the support that comes with a PC / Laptop. However, this support contract is not on the ComputerName, but instead it's SerialNumber.

Because some of our PC's are built by us, they don't have serial numbers. Therefore when I click the button, I get the error message:

"Cast from DBNull to type String is not Valid".

I have tried If IsDBNull(SerialNumber) Then
Messagebox.show("No Serial Number!")

...but that doesn't work. I've also tried If SelectedComputer = "" Then

(as shown below)

The coding for the button is:

VB.NET:
 Dim columnindex As Integer = 1 
 
Dim SelectedComputerName As String = CStr(Me.grdComputers.Item(Me.grdComputers.CurrentRowIndex, columnindex))
 
Serial = SelectedComputerName
 
If SelectedComputerName = "" Then
 
MessageBox.Show("No Serial Number!")
 
Else
 
Dim frmSupport As New frmSupportDetails
 
frmSupport.Show()
 
End If

Thanks!
Luke
 
My guess is that your exception is thrown on this line:
VB.NET:
Dim SelectedComputerName As String = CStr(Me.grdComputers.Item(Me.grdComputers.CurrentRowIndex, columnindex))
I'd actually forgotten this fact, but you cannot convert a null value to a string using CStr. You can, however, use ToString, so you can change it to:
VB.NET:
Dim SelectedComputerName As String = Me.grdComputers.Item(Me.grdComputers.CurrentRowIndex, columnindex).ToString()
Alternatively, you could test the item value against DBNull.Value before assigning it to a variable.
 
Back
Top