473,326 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Displaying records on a form...

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
Nov 20 '05 #1
15 1492
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 Diddly wrote:
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
Nov 20 '05 #2
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
Nov 20 '05 #3
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
Nov 20 '05 #4
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
Nov 20 '05 #5
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").Valu e =
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
Nov 20 '05 #6
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").Valu e =
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
Nov 20 '05 #7
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").Valu e =
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
Nov 20 '05 #8
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").Valu e =
cmbChapter.SelectedValue
daVerse.SelectCommand.Parameters("BookID").Value = cmbBook.SelectedValue

Thanks,

Gary
Nov 20 '05 #9
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("@Categ oryName",
OleDbType.VarChar, 80).Value = "toasters"
myDataAdapter.SelectCommand.Parameters.Add("@Seria lNum",
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

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").Valu e =
cmbChapter.SelectedValue
daVerse.SelectCommand.Parameters("BookID").Value = cmbBook.SelectedValue

Nov 20 '05 #10
-----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+2RaJ8FtQOSDNEsSnhusIAn jvg
Br2nwwik5MqTWqCZwfOV+RpE
=Sjmw
-----END PGP SIGNATURE-----
Nov 20 '05 #11
-----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+/Ihz8KnwrkurVSEQI3ewCgz47ZHl4CCwKppkHA6iLLIJkdLLEAo JMq
7YPjGhKTg/ecLseEooK0GHZq
=SH7q
-----END PGP SIGNATURE-----
Nov 20 '05 #12
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,

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

Nov 20 '05 #13
-----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/DWxj8KnwrkurVSEQIDtACgkVFiJOWMs4NYg0TB8BXCs1oGi5IA n3I4
DuzkrqSNIGwldFQ4Ld564qZ0
=5vKK
-----END PGP SIGNATURE-----
Nov 20 '05 #14
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
Nov 20 '05 #15
Cor,
daText.SelectCommand.Parameters("VerseID)").Value =
cmbVerse.SelectedValue
There is too many parentheses in the line above ;)
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.


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

Thanks again,

Gary
Nov 20 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tim Graichen | last post by:
Good morning, I have a sub-form that displays records from a table as a continuous form. The table has several hundred records, but the subform only displays five or six records. The records do...
6
by: Matt K. | last post by:
Hi there, I have a form in an Access project that contains a subform which displays the results of a query of the style "select * from where = #a certain date#". In the main part of the form...
3
by: Robin S. | last post by:
I tried to ask this question several days ago, but I didn't explain my application correctly. Basically I want to have one record from table "A" and I want to display, say, 5 records from table...
5
by: Robert | last post by:
Hello Accessors I have some reports created in Access that are very good for what they do. However, it seems to me that when you are displaying information you don't need to print out that a...
5
by: tdmailbox | last post by:
I have a form with a child form. In the child form there is a list of names that can grow quite large. On the parent form I want to display the first name from the child form. I set up a test...
9
by: Susan Bricker | last post by:
Greetings. I am having trouble populating text data that represents data in my table. Here's the setup: There is a People Table (name, address, phone, ...) peopleID = autonumber key There...
3
by: Lyn | last post by:
I need some guidance for a technique that will allow me to accomplish the following... I have a table in which each record contains a photograph. I would like to display in a form a thumbnail...
0
by: darrel | last post by:
Hi there, good day! i need some help in displaying my records,,, my assignment is i have to display a database records in a labels. Its like this in my database i have a table called "SUBJECTS"...
1
by: dheroan | last post by:
Hi there, I'm fairly new to using databases with VB .NET. I'm currently working on an application using a Microsoft Access database as a data source. I have created a form to display the fields...
0
by: John Kirkpatrick | last post by:
Hi all, I am having difficulty displaying records on a frontend MS Access 2000 form using a MySQL backend. The following code works well with the Jet database engine but doesn't work properly...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.