David Carter

David Carter

  • NA
  • 86
  • 22.5k

Database Query in VB

Aug 5 2010 9:55 PM

I have an Access Database which I'll call David.mdb, behind my Visual Studio 2008 front end. It has Just one Table called "Product and Uses". The table has a number of fields. Product Name, Under 12 Months, Over 12 Months, etc. [The Products are suitable for different ages]. On Form1, I have a text box with 4 buttons (First,Last,Next,Previous).  I have OleDbDataAdapter1, OleDbConnection1 and DsProducts1 associated with the form. The Textbox named txtProducts is databound to DsProducts1-Products and Uses-Product Name. The following code works fine, it loads the form with the first product and cycles through the product names.
Public
Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DsProducts1.Clear()
OleDbDataAdapter1.Fill(DsProducts1)
End Sub
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
txtProduct.BindingContext(DsProducts1,
"Product and Uses").Position = 0
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
txtProduct.BindingContext(DsProducts1,
"Product and Uses").Position = Me.BindingContext(DsProducts1, "Product and Uses").Count - 1
End Sub
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
txtProduct.BindingContext(DsProducts1,
"Product and Uses").Position -= 1
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
txtProduct.BindingContext(DsProducts1,
"Product and Uses").Position += 1
End Sub
 
My question is that I want to write Queries so that only those product Names that are suitable for a particular age appear.
I can write them using the Query builder
SELECT     [Product Name]
FROM        [Product and Uses]
WHERE     ([Over 12 Months] = true)

From the Query Builder I can execute SQL and it works fine but I don't know how to write that Query as workable code in the form.
Hope this makes sense.
Regards,
David
 

Answers (6)