Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ShowDialog - open a form that is built on a table - using correct ID 4

Status
Not open for further replies.

surfside1

Programmer
Feb 12, 2006
209
US
I need to code a ShowDialog statement for a form. That form is built using fields from a table. The form needs to open displaying data for a record in that table. I'm curious how you identify the key within the ShowDialog statement or is that handled some other way using a where clause? Does this make sense? (real new at this!) Thanks!
 
Check out this FAQ:

faq796-5773

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
From another post, you mention this is a datagrid on the page. You want to click on the datagrid and have a showdialog method of a form bring up the details for that record. I would recommend the following:

Make sure the ID field for the record is included in the datasource for the datagrid. It does not necessarily need to be displayed on the datagrid but the field need to be contained. Then, using an appropriate click (cell or row probably) for the dratagrid, you grab the cell containing the ID and pass that to your where statment.

So, as an example if I had a datagrid with the following:

Code:
ID(Hidden)         FirstName       LastName
1                  Robert          Johnson
[code]

When I click on the row, I pass DataGrid.CurrentRow.Cells(0).Value (maybe not exact syntax...from the top of my head - but you get the idea) to the SQL statement or whatever method you are using the get the details for the record and display them through the ShowDialog form....

Does that make any sense??

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
It does make sense and this is the code that I used in Access, but not sure how in VB:

THIS IS A PUBLIC SUB ROUTINE THAT CAN BE USED THROUGHOUT THE FORM WITH THE GRID FOR EACH FIELD IN THE GRID IN THEIR CLICK EVENT:

Public Sub openDetailForm()
Dim intID As Integer
intID = Me.ID
DoCmd.OpenForm "frm_Listings", , , "ID = " & intID, , acDialog
End Sub

EACH FIELD ON DATAGRID WHEN CLICKED WILL CALL OPENDETAILFORM:

Private Sub End_Date_Click()
Call openDetailForm
End Sub

What I want to do is the following but I'm not sure where I would put the ID for that row in the ShowDialog statement.

My.Forms.frmListingEditForm.ShowDialog()

Thanks!
 
I just thought of something. Would I code:

Me.Tbl_ListingsBindingSource.Filter = "ID = intID"
My.Forms.frmListingEditForm.ShowDialog()
for each click event for the fields in the grid?

I'll give it a try.



 
Well, I tried the following and it didn't work:
TOP OF FORM:
Public Class frmAuctionListings
Dim intGridID As Integer

CLICK OF GRID EVENT:
Private Sub Tbl_ListingsDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Tbl_ListingsDataGridView.CellContentClick
intGridID = DataGridViewTextBoxID
Me.Tbl_ListingsBindingSource.Filter = "ID = intGridID"
My.Forms.frmListingEditForm.ShowDialog()
End Sub

I'm sure I'm not using the correct click event in the grid or I don't know how to reference the actual cell that is clicked or has focus. Ideas?
 
The error I get is on the intGridID = DataGridViewTextBoxID statement. It says System.Windows.Forms.DataGridViewTextBoxColumn cannot be converted to Integer. How do you get the current value of the cell that has focus? Or am I totally off?
 
You need to use code like this. I can't make sense of what your gridview or column names even are from what you have posted.

Code:
'where e.RowIndex is the clicked row
'and 0 is the index of the cell whos value you want to retrieve
intGridID = myDataGridView.Rows(e.RowIndex).Cells(0).Value

You may need to cast the cell's value to an integer.

Hope this helps,

Alex

[small]----signature below----[/small]
I don't do any programming whatsoever

Ignorance of certain subjects is a great part of wisdom
 
Also, what you have there is not really all that similar to any of the methods discussed in the FAQ's posted by jebenson. Which method are you trying to use?

[small]----signature below----[/small]
I don't do any programming whatsoever

Ignorance of certain subjects is a great part of wisdom
 
What I am coding is when the mouse clicks on any cell in the grid Tbl_ListingsDataGridView or if they click in any cell in the grid, I need to open another form that contains the information for the row in the table that contains the ID column in the grid which is the key. Below is the sub:

Private Sub Tbl_ListingsDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Tbl_ListingsDataGridView.CellContentClick

My confusion is how the cells are identified in the grid and how to access them in code. If I should be doing this another way, I could use the help. Thanks!
 
The cells per row can be referenced using the index....this index is 0-based, so the first cell is 0, the second is 1 and so on. That includes hidden cells as well. So if your first cell is hidden, the first displayed cell is 1, etc.

Alex gave you the right string:

Code:
myDataGridView.Rows(e.RowIndex).Cells(0).Value

In the example, myDataGridView refers to the datagridview object, Rows(e.RowIndex) is providing you with the current row (i.e. the row that was clicked) and Cells(0) referes to the first cell, hidden or otherwise.

You should only need to change the 0 in Alex's example to the column that holds your ID value...


=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Sorry Alex that it didn't sink in you were giving me the exact code. Now I'm not sure how to make sure the table id is set when the ShowDialog is used. This is what I coded:

(the click event when the grid is clicked)
Private Sub Tbl_ListingsDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Tbl_ListingsDataGridView.CellContentClick
Dim intGridID As String
intGridID = Tbl_ListingsDataGridView.Rows(e.RowIndex).Cells(0).Value

(this msgbox displays the correct id)
MsgBox("intGridID = " & intGridID)

(then I wasn't sure how to set the key of the table that should be used when opening the form)
Me.Tbl_ListingsBindingSource.Filter = "ID = intGridID"?
(OR)
Me.Tbl_ListingsBindingSource.Filter = "ID = Tbl_ListingsDataGridView"?
(OR)???
(Then I'm thinking the ShowDialog would pick up the correct key?)
My.Forms.frmListingEditForm.ShowDialog()

Sorry for my confusion....
 
Have you read the FAQ's jebenson posted yet?

You need to first decide how you want to pass the intGridID first. Once your form has this piece of information, it is pretty easy (I would use it as a parameter at the database level if possible, to minimize size of result set actually being passed over). But first you need to settle on how to get the value to the form.

Which strategy you choose will drive how you decide to set the filter or datasource on your second form.

[small]----signature below----[/small]
I don't do any programming whatsoever

Ignorance of certain subjects is a great part of wisdom
 
I have read the post but I'm confused as to how it fits my scenario. The strategy of how I need to get the id from the grid I have done with your code example. Now what is still confusing is that if my second form is based on one of my tables and I now have the key of the table that should be used on the second form, how do I pass that value before the ShowDialog() statement runs? Would I be setting up one of those SetReceive values that will passed to the ID field on the form that is opened when the ShowDialog statement runs? I'm beginning to think I should do as your "Signature" states! Thanks for your help!
 
This is why you need to come up with a strategy. In the simplest way I can think of to explain it, you will need to either give your second form a way to retrieve the value from the first, or you will need to pass it from the first form to the second BEFORE it opens (because when you use ShowDialog, all activity on the first form is suspended until the dialog is closed).

I don't really know enough about the relationship between these two forms to make any recommendations on which route you should take. But they can all work.

The easiest thing you could do (IMO) is set up a variable called 'myID' on your second form and set this from Form1 (either explicitly or through the constructor). You can then set this before showing the form, and the variable can be used in the second form to limit what is displayed. It would go something like this:

Code:
dim myF2 as new Form2()
myF2.myID = myDataGridView.Rows(e.RowIndex).Cells(0).Value
'actual filtering will happen when myF2 is shown
myF2.ShowDialog()

Hope this helps,

Alex


[small]----signature below----[/small]
I don't do any programming whatsoever

Ignorance of certain subjects is a great part of wisdom
 
Ok, Please bear with me....
What confuses me about the FAQ is this. If I already have a form1 (frmAuctionListings.vb) that is the form with the datagrid which contains the ID that I want to pass to the form2 (frmListingEditForm) that is already created and formatted with fields that are bound to a table containing that ID, are these the Form1 class and Form2 class that you are talking about? Then I'm confused about the
Private Sub OpenForm2
dim frmReceiver as new Form2
frmReceiver.SetReceive(txtSend.text)
frmReceiver.ShowDialog
End Sub
I'm confused about the "as new Form2". Am I to replace your Form1 and Form2 with my form names or are these the instantiations that you talked about? And can you help me with that or point me to the right direction of where I learn about that? Thanks so much!
 
You code would look something like this:

Private Sub Open_frmListingEditForm
dim fListingEditForm as new frmListingEditForm
fListingEditForm.SetReceive("Your ID value here")
fListingEditForm.ShowDialog
End Sub

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top