Displaying records on a form...

B

Bo Diddly

Hi everyone,

I am relatively new to VB.Net, and database programming.
I have a database with four tables, set up as follows:

tblBook
BookID... key
Book... All the book titles of the Bible
tblChapter
ChapterID... key
Chapter... chapter numbers (1 - 150)
tblVerse
VerseID... key
Verse... verse number (1 - 100)
tblVerseText
ID... key
BookID
ChapterID
VerseID
VerseText... Each verse from the Bible, each in its own record

In VB.Net, I have a form with three ComboBoxes and one Label:

cmbBook... to display all the Book titles
cmbChapter... to display all the Chapter numbers
cmbVerse... to display all the verse numbers
lblVerseText... to display the verse corresponding to the selections made in
the three ComboBoxes

I can get each ComboBox to display the information from the corresponding
tables, however, I want to be able to filter each subsequent ComboBox, and
the label, to display only the information that is common to the preceding
selection.

For example:

You select 'Genesis' in cmbBook, and cmbChapter will only display the
numbers 1 - 22, (the number of chapters in Genesis).
Then you select '1' in cmbChapter, and cmbVerse will only display the
numbers 1 - 31, (the number of verses in Genesis-1).
Then you select '1' in cmbVerse, and lblVerseText will only display the
verse Genesis,1,1.

As I stated earlier, I can get each ComboBox to display ALL of the
corresponding data from their respective tables, but I would like to filter
the data according to the data currently selected in any of the COmboBoxes.

Any help would be gretly appreciated,

Gary
 
O

One Handed Man [ OHM# ]

One way . . .

For each dataTable, set up a DataView. In the selectedIndexChanged event
change the filter parameters of cascading combo boxes to correspond with the
key found in the relation above. Bind the comboboxes to the DataView not the
table, except the master table.

Another way,

Is to set up relations for each table relation, and set up the binding so
the secondary boxes get the data from the relation. Ive done this with grids
but not with combo boxes but it should work.


OHM


Bo said:
Hi everyone,

I am relatively new to VB.Net, and database programming.
I have a database with four tables, set up as follows:

tblBook
BookID... key
Book... All the book titles of the Bible
tblChapter
ChapterID... key
Chapter... chapter numbers (1 - 150)
tblVerse
VerseID... key
Verse... verse number (1 - 100)
tblVerseText
ID... key
BookID
ChapterID
VerseID
VerseText... Each verse from the Bible, each in its own record

In VB.Net, I have a form with three ComboBoxes and one Label:

cmbBook... to display all the Book titles
cmbChapter... to display all the Chapter numbers
cmbVerse... to display all the verse numbers
lblVerseText... to display the verse corresponding to the selections
made in the three ComboBoxes

I can get each ComboBox to display the information from the
corresponding tables, however, I want to be able to filter each
subsequent ComboBox, and the label, to display only the information
that is common to the preceding selection.

For example:

You select 'Genesis' in cmbBook, and cmbChapter will only display the
numbers 1 - 22, (the number of chapters in Genesis).
Then you select '1' in cmbChapter, and cmbVerse will only display the
numbers 1 - 31, (the number of verses in Genesis-1).
Then you select '1' in cmbVerse, and lblVerseText will only display
the verse Genesis,1,1.

As I stated earlier, I can get each ComboBox to display ALL of the
corresponding data from their respective tables, but I would like to
filter the data according to the data currently selected in any of
the COmboBoxes.

Any help would be gretly appreciated,

Gary

Regards - OHM# OneHandedMan{at}BTInternet{dot}com
 
C

Cor

Hi Bo Diddy,

In addition to OHM about the comboboxes,
have a look at datasource, datamembers, and value members from the combobox,
using that it is not that difficult I thought,

Cor
 
B

Bo Diddly

Thank you to OHM and Cor,

I will try it out as soon as I get a chance, maybe before tomorrow
(Saturday), and I will let you know.'

Gary
 
B

Bo Diddly

This is what I've done so far:

I have a DataBase with:

tblBook
BookID... key
Book... All the book titles of the Bible
tblChapter
ChapterID... key
Chapter... chapter numbers (1 - 150)
tblVerse
VerseID... key
Verse... verse number (1 - 100)
tblVerseText
ID... key
BookID
ChapterID
VerseID
VerseText... Each verse from the Bible, each in its own record

Created a DataAdapter for each table, (when I run the SQL statements within the configuration wizard, they do what I want them to do):

daBook... SELECT Book,BookID FROM tblBook ORDER BY BookID

daChapter... SELECT DISTINCT tblChapter.Chapter, tblChapter.ChapterID, tblVerseText.ChapterID AS Expr1, tblVerseText.BookID FROM tblChapter INNER JOIN tblVerseText ON tblChapter.ChapterID = tblVerseText.ChapterID WHERE (tblVerseText.BookID = ?) ORDER BY tblChapter.ChapterID

daVerse... SELECT DISTINCT tblVerse.Verse, tblVerse.VerseID, tblVerseText.ChapterID, tblVerseText.VerseID AS Expr1 FROM tblVerse INNER JOIN tblVerseText ON tblVerse.VerseID = tblVerseText.VerseID WHERE (tblVerseText.ChapterID = ?) ORDER BY tblVerse.VerseID

daText... SELECT VerseText, ID, VerseID FROM tblVerseText WHERE (VerseID = ?) ORDER BY ID

Created a DataView for each DataSet

dvBook
dvChapter
dvVerse
dvText

The Controls on the form are bound to the DataViews:

cmbBook > dvBook
cmbChapter > dvChapter
cmbVerse > dvVerse
lblText > dvText

I need the item that is selected in each ComboBox to filter the choices in the subsequent ComboBoxes.

How do I write the statement in the SelectedItemChanged event to take the value displayed and pass it's corresponding key to filter the next ComboBox?

Thanks in advance,

Gary
 
B

Bo Diddly

Cor and OHM,

Thanks for the push in the right direction.
I have been able to get the first ComboBox to work properly, (it wasn't a
problem though). I can retrieve the .value from the first ComboBox and pass
it into the SelectCommand for the second ComboBox. However, this is where I
am having problems now. My SelectCommand statement for the third ComboBox
works fine when I run it in the Query Builder or when I run Preview Data.
But when I run the program, it doesn't work properly.

-----------------------------------------
SELECT DISTINCT tblVerse.Verse, tblVerse.VerseID, tblVerseText.VerseID AS
VerseKey, tblVerseText.ChapterID AS ChapterKey, tblVerseText.BookID AS
BookKey
FROM tblVerse INNER JOIN tblVerseText ON
tblVerse.VerseID = tblVerseText.VerseID
WHERE (tblVerseText.ChapterID = ?) AND (tblVerseText.BookID = ?)
ORDER BY tblVerse.VerseID
-----------------------------------------

-----------------------------------------
Private Sub cmbChapter_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cmbChapter.SelectedIndexChanged

daVerse.SelectCommand.Parameters("ChapterID").Value =
cmbChapter.SelectedValue

daVerse.SelectCommand.Parameters("BookID").Value = cmbBook.SelectedValue

dsVerse.Clear()

daVerse.Fill(dsVerse)

End Sub

-----------------------------------------
Can I somehow combine the first two lines in the cmbChapter_SelectedIndex
Changed event?
I am a little confused as to why the SQL statement works perfectly in the
Query Builder or Preview Data, but does not filter properly when the program
is run.
As I said , the first two ComboBoxes filter and display perfectly, but the
third ComboBox does not filter.

Thanks again for any nudges in the right direction,

Gary
 
B

Bo Diddly

Cor and OHM,

Thanks for the push in the right direction... everything is working fine
except...

The third ComboBox does not filter correctly. The SQL statement works
perfectly in the Query Builder or in Data Preview. But I cannot get the
ComboBox to filter correctly.

-------------------------------------
SELECT DISTINCT
tblVerse.Verse, tblVerse.VerseID, tblVerseText.VerseID
AS VerseKey, tblVerseText.ChapterID AS ChapterKey, tblVerseText.BookID AS
BookKey
FROM tblVerse INNER JOIN
tblVerseText ON tblVerse.VerseID =
tblVerseText.VerseID
WHERE (tblVerseText.ChapterID = ?) AND (tblVerseText.BookID = ?)
ORDER BY tblVerse.VerseID
-------------------------------------
Private Sub cmbChapter_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cmbChapter.SelectedIndexChanged

daVerse.SelectCommand.Parameters("ChapterID").Value =
cmbChapter.SelectedValue

daVerse.SelectCommand.Parameters("BookID").Value = cmbBook.SelectedValue

dsVerse.Clear()

daVerse.Fill(dsVerse)

End Sub

-------------------------------------
In the code above, am I retrieving the two variables correctly to pass them
to the daVerse.SelectCommand.Parameters? I know it is correct if only one
parameter is needed, but the only way I can get the proper filtering is to
use the two variables, BookID and ChapterID, together in the one SQL
statement.

I know I am missing something silly in the Sub procedure, but I can't figure
it out.

Thanks in advance for any nudges in the right direction,

Gary
 
C

Cor

Hi Garry,

I did look a long time and did not see it, the strange thing is of course
that it works with combobox 1 and not with combobox 2.

While I asume they are completly the same with the only difference the extra
parameter in the second.
(I never wrote it in the way you did, but looking at the documentation I see
no errors).

Did you debug it?

I myself add always while debugging just something as

dim myfirsttest as string = cmbChapter.SelectedValue
daVerse.SelectCommand.Parameters("ChapterID").Value =
cmbChapter.SelectedValue

dim mysecondtest as string = cmbBook.SelectedValue
daVerse.SelectCommand.Parameters("BookID").Value =
cmbBook.SelectedValue

I hope this helps, and message back if the values are correct OK?

Cor
 
B

Bo Diddly

Cor,

I don't get any 'ERRORS' when I run it in Debug mode.

A little more information:

cmbBook displays 73 items, (the names of the Books in the Douay-Rheims
Bible)

cmbChapter displays up to 150 items, (the number of Chapters according to
which Book is selected in cmbBook), this works fine.

cmbVerse should display up to 176 items, (the number of Verses according to
which Chapter AND Book are selected in the previous ComboBoxes), this part
is not filtering correctly. It does not generate any errors in the program.

When I run the SQL statement in the Query Builder, (I have to enter the two
parameters), the resulting table is correctly filtered and displays the
number of verses according to the Book AND Chapter selected. This is also
the result when I run Preview Data, (and have to enter the parameters
manually).

I am stuck at trying to pass the TWO parameters to
dsVerse.SelectCommand.CommandText...

Can it be done with one line, not two like this:

daVerse.SelectCommand.Parameters("ChapterID").Value =
cmbChapter.SelectedValue
daVerse.SelectCommand.Parameters("BookID").Value = cmbBook.SelectedValue

Thanks,

Gary
 
C

Cor

Hi Bo,

Normaly I only find this in the documentation (read SQL where OLeDB when it
is in your case SQL)
' ...
' create myDataSet and myDataAdapter
' ...
myDataAdapter.SelectCommand.Parameters.Add("@CategoryName",
OleDbType.VarChar, 80).Value = "toasters"
myDataAdapter.SelectCommand.Parameters.Add("@SerialNum",
OleDbType.Integer).Value = 239
myDataAdapter.Fill(myDataSet)

But I found your methode also, but if you realy stuck, maybe you can try
this to test
(debugs very easy)

mystring as string = "SELECT DISTINCT tblVerse.Verse, tblVerse.VerseID,
tblVerseText.VerseID AS
VerseKey, tblVerseText.ChapterID AS ChapterKey, tblVerseText.BookID AS
BookKey
FROM tblVerse INNER JOIN tblVerseText ON
tblVerse.VerseID = tblVerseText.VerseID
WHERE tblVerseText.ChapterID ='" & cmbChapter.SelectedValue & "' AND
(tblVerseText.BookID = '"
& cmbBook.SelectedValue & "' ORDER BY tblVerse.VerseID"

dim daverse as new dataadapter(mystring, myconnection)
daverse.fill(mydataset)

And then when it works make it nice code.

Maybe it helps,

Cor
 
B

Bo Diddly

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Cor,

My method works like a charm!

I had the wrong item selected in cmbChapter.ValueMember.

It just didn't make sense that the SQL statements would work in the
Builder but not when I ran the program.

So, I will try not to make too many changes at a time... LOL

Thanks for all the help so far!

You don't GIVE the answer... you DIRECT to the answer... A much
better way as far as I'm concerned.

Happy Holidays and Happy New Year!

Gary

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 6.5.8 for non-commercial use <http://www.pgp.com>

iQA/AwUBP+/Akj8KnwrkurVSEQI3bwCgq+Mcqd+2RaJ8FtQOSDNEsSnhusIAnjvg
Br2nwwik5MqTWqCZwfOV+RpE
=Sjmw
-----END PGP SIGNATURE-----
 
B

Bo Diddly

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

OOPS,

Now I get a 'System.IndexOutOfRangeException' error at the following
point:

Private Sub cmbVerse_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cmbVerse.SelectedIndexChanged

'Code to populate lblText

HERE-> daText.SelectCommand.Parameters("VerseID)").Value =
cmbVerse.SelectedValue

daText.SelectCommand.Parameters("ChapterID").Value
= cmbChapter.SelectedValue

daText.SelectCommand.Parameters("BookID").Value =
cmbBook.SelectedValue

'Clear and populate lblText

dsText.Clear()

daText.Fill(dsText)

End Sub

It's late, I'll look into it tomorrow,



Gray

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 6.5.8 for non-commercial use <http://www.pgp.com>

iQA/AwUBP+/Ihz8KnwrkurVSEQI3ewCgz47ZHl4CCwKppkHA6iLLIJkdLLEAoJMq
7YPjGhKTg/ecLseEooK0GHZq
=SH7q
-----END PGP SIGNATURE-----
 
C

Cor

Hi Gray,

The Selected Index changed fires everytime that there is something changed
in that combobox.
Therefore you have to stop it while initializing using a switch/bool/flag.

I use the Selected Index change commited, but I think it has a bug.

I thought it works fine for me if i am using it with a dataset binded to the
datasource of the combobox

(I thought that the bug is that without a datasource it gives the previous
selectedvalue).

You can try that also, but check it well if it is working correct.

I hope this goes again?

Cor


"> OOPS,
 
B

Bo Diddly

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Cor,

Do you see anything wrong with this line?

daText.SelectCommand.Parameters("VerseID)").Value =
cmbVerse.SelectedValue

When I made the correction everything works great.
Now all that is left is to embelish the UI for the program.

I am looking for people to try it out, it is very basic at this
point, but with 35,845 records in tblVerseText, (all the verses from
the Douay-Rheims Bible), there is still some editing to do. (Mostly
typing errors ) :)

Is there a place I can post my little program for others to see and
use?

Thanks for all the help,

Gary

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 6.5.8 for non-commercial use <http://www.pgp.com>

iQA/AwUBP/DWxj8KnwrkurVSEQIDtACgkVFiJOWMs4NYg0TB8BXCs1oGi5IAn3I4
DuzkrqSNIGwldFQ4Ld564qZ0
=5vKK
-----END PGP SIGNATURE-----
 
C

Cor

Hi Gray,

daText.SelectCommand.Parameters("VerseID)").Value =
cmbVerse.SelectedValue

I really do not know.
I do not use it, I also did look to the documentation for this, because I
thought this was giving the error, but why I do not know. (It is also very
slightly in the documentation)
Is there a place I can post my little program for others to see and
use?

I would not do that Gray, but when you see messages here with what you can
help others, you can cut out snippets and past them in as examples.

Cor
 
B

Bo Diddly

Cor,

There is too many parentheses in the line above ;)
I would not do that Gray, but when you see messages here with what you can
help others, you can cut out snippets and past them in as examples.

Absolutely, I will do that... Small price to pay for the help I have
received!

Thanks again,

Gary
 

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

Top