473,659 Members | 2,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Querying and VB

9 New Member
Hi,
I have a MS Access DB with a query that I need to display in vb but am not too sure on how to go about it. The query has some criteria - i.e you have to input a date to run the query. I think that's a parameter in vb......

Well I've been playing around with it for some time now but amn't really getting anywhere. Could ye recommend any good tutorial or link as to how to do it?

Here's the code that I've been working on:

Expand|Select|Wrap|Line Numbers
  1.  Imports System
  2. Imports System.Data
  3. Imports System.Data.OleDb
  4. Imports System.Data.SqlClient
  5. Public Class frmTodaysAbsentees
  6.  
  7.     Inherits System.Windows.Forms.Form
  8.  
  9.     Dim objDataSet As New DataSet
  10.     Dim dateToday = Now.Date
  11.  
  12.     Dim objConnection As New OleDb.OleDbConnection( _
  13.     "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= FYP.mdb")
  14.     Dim objAbsenteesDA As New OleDb.OleDbDataAdapter("Select * from TodaysAbsentees", objConnection)
  15.     Dim objAbsenteesCB As New OleDb.OleDbCommandBuilder(objAbsenteesDA)
  16.     Dim objRow As DataRow
  17.  
  18.     Private Sub frmTodaysAbsentees_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  19.         'TODO: This line of code loads data into the 'DataSet2.TodaysAbsentees' table. You can move, or remove it, as needed.
  20.         'Me.TodaysAbsenteesTableAdapter.Fill(Me.DataSet2.TodaysAbsentees)
  21.  
  22.         'clear data set of any existing data
  23.         objDataSet.Clear()
  24.  
  25.         'fill the data set with infro from the 'TodaysAbsentees' query
  26.         objAbsenteesDA.FillSchema(objDataSet, SchemaType.Source, "TodaysAbsentees")
  27.         objAbsenteesDA.Fill(objDataSet, "TodaysAbsentees")
  28.  
  29.  
  30.         'setup the relationship
  31.         objDataSet.Relations.Clear()
  32.         objDataSet.Relations.Add("Class2Student", _
  33.                                  objDataSet.Tables("tblClass").Columns("ClassID"), _
  34.                                  objDataSet.Tables("tblStudent").Columns("ClassID"))
  35.         objDataSet.Relations.Add("Student2Attendance", _
  36.                                  objDataSet.Tables("tblStudent").Columns("StudentID"), _
  37.                                  objDataSet.Tables("tblAttendance").Columns("StudentID"))
  38.  
  39.         lstAbsentStudents.Items.Clear()
  40.  
  41.         Dim I As Integer
  42.         Dim strCurrent As String
  43.         Dim strCurrent1 As String
  44.         For I = 1 To objDataSet.Tables("TodaysAbsentees").Rows.Count
  45.             strCurrent = objDataSet.Tables("TodaysAbsentees").Rows(I - 1).Item("SName")
  46.             strCurrent1 = objDataSet.Tables("TodaysAbsentees").Rows(I - 1).Item("FName")
  47.             lstAbsentStudents.Items.Add(strCurrent + ", " + strCurrent1)
  48.         Next
  49.  
  50.  
  51.     End Sub
  52.  
I've been trying to reference the query as a dataset. But should I write the SQL code someplace??

Also, when I run it I get the error: "No value given for one or more required parameters"

Help would be greatly appreciated.
Feb 6 '09 #1
4 1371
mcmahon
9 New Member
I've been working on it since posting this question earlier.

New Code::
Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.Data
  3. Imports System.Data.OleDb
  4. Imports System.Data.SqlClient
  5. Public Class frmTodaysAbsentees
  6.  
  7.     Inherits System.Windows.Forms.Form
  8.  
  9.     Dim objDataSet As New DataSet
  10.  
  11.     Dim objConnection As New OleDb.OleDbConnection( _
  12.     "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= FYP.mdb")
  13.     Dim objAbsenteesDA As New OleDb.OleDbDataAdapter("Select * from TodaysAbsentees", objConnection)
  14.     Dim objAbsenteesCB As New OleDb.OleDbCommandBuilder(objAbsenteesDA)
  15.     Dim objRow As DataRow
  16.     Dim queryString As String = "SELECT * FROM TodaysAbsentees Where AttendanceDate LIKE ?"
  17.     Dim AbsenteesCmd As OleDbCommand = New OleDbCommand(queryString, objConnection)
  18.     Dim AbsenteesParam As New OleDb.OleDbParameter
  19.  
  20.  
  21.     Private Sub frmTodaysAbsentees_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  22.         'TODO: This line of code loads data into the 'DataSet2.TodaysAbsentees' table. You can move, or remove it, as needed.
  23.         'Me.TodaysAbsenteesTableAdapter.Fill(Me.DataSet2.TodaysAbsentees)
  24.         AbsenteesCmd.Parameters.Add("AttendanceDate", OleDbType.Date).Value = Now.Date
  25.         'Dim reader As OleDbDataReader = AbsenteesCmd.ExecuteReader()
  26.         'clear data set of any existing data
  27.         objDataSet.Clear()
  28.  
  29.         'fill the data set with infro from the 'TodaysAbsentees' query
  30.         objAbsenteesDA.FillSchema(objDataSet, SchemaType.Source, "TodaysAbsentees")
  31.         objAbsenteesDA.Fill(objDataSet, "TodaysAbsentees")
And the same error is occuring. How can I get this working? Also, I have a datagridview on the windows form for displaying the results
Feb 6 '09 #2
Frinavale
9,735 Recognized Expert Moderator Expert
It could have something to do with not recognizing your table name.
Do you have a table "TodaysAbsentee s"? Is this spelled correctly?

It could have something to do with your Select statement also.
Try using
Expand|Select|Wrap|Line Numbers
  1. Dim queryString As String = "SELECT * FROM TodaysAbsentees Where AttendanceDate LIKE '@todaysDate'"
And change your code from:
Expand|Select|Wrap|Line Numbers
  1. AbsenteesCmd.Parameters.Add("AttendanceDate", OleDbType.Date).Value = Now.Date
To use the "todaysDate " parameter:
Expand|Select|Wrap|Line Numbers
  1. AbsenteesCmd.Parameters.Add("todaysDate", OleDbType.Date).Value = Now.Date

Let me know if this works out for you.
Feb 6 '09 #3
mcmahon
9 New Member
"TodaysAbsentee s" is the name of the query in my database and it is spelt correctly.

I made the changes to my code that you recommended but when I run it I still get that same error: "no value given for one or more required parameters".

I should mention, I have never used queries with vb before so don't really know what I'm doing. I've just been googling stuf to try and piece it together. But from looking at my code it doesnt look like much is going on

Expand|Select|Wrap|Line Numbers
  1.  Public Class frmTodaysAbsentees
  2.  
  3.     Inherits System.Windows.Forms.Form
  4.  
  5.     Dim objDataSet As New DataSet
  6.     Dim dateToday = Now.Date
  7.  
  8.     Dim objConnection As New OleDb.OleDbConnection( _
  9.     "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= FYP.mdb")
  10.     Dim objAbsenteesDA As New OleDb.OleDbDataAdapter("Select * from TodaysAbsentees", objConnection)
  11.     Dim objAbsenteesCB As New OleDb.OleDbCommandBuilder(objAbsenteesDA)
  12.     Dim objRow As DataRow
  13.     Dim queryString As String = "SELECT * FROM TodaysAbsentees Where AttendanceDate LIKE '@todaysDate'"
  14.     Dim AbsenteesCmd As OleDbCommand = New OleDbCommand(queryString, objConnection)
  15.     Dim AbsenteesParam As New OleDb.OleDbParameter
  16.  
  17.  
  18.     Private Sub frmTodaysAbsentees_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  19.         'TODO: This line of code loads data into the 'DataSet2.TodaysAbsentees' table. You can move, or remove it, as needed.
  20.         'Me.TodaysAbsenteesTableAdapter.Fill(Me.DataSet2.TodaysAbsentees)
  21.  
  22.         AbsenteesCmd.Parameters.Add("todaysDate", OleDbType.Date).Value = Now.Date
  23.  
  24.         objDataSet.Clear()
  25.  
  26.         'fill the data set with infro from the 'TodaysAbsentees' query
  27.         objAbsenteesDA.FillSchema(objDataSet, SchemaType.Source, "TodaysAbsentees")
  28.         objAbsenteesDA.Fill(objDataSet, "TodaysAbsentees")
  29.  
  30.         'DataGridView1.ClearSelection()
  31.  
  32.  
  33.     End Sub
First off I need to sort out that parameters error. But I've got a dataset there, how will I actually get to show my query results in the datagridview that is on my form??

Thanks
Feb 7 '09 #4
jg007
283 Contributor
for the datagrid try -

Expand|Select|Wrap|Line Numbers
  1.  
  2. DataGridView1.DataSource = objDataSet.Tables("TodaysAbsentees")
  3.  
  4.  
You do not seem to be setting the dataadaptor to use the command that you are creating.

I just created a quick database but the query for me was -

Expand|Select|Wrap|Line Numbers
  1.  
  2.   Dim queryString As String = "SELECT * FROM TodaysAbsentees Where AttendanceDate LIKE @todaysDate"
  3.  
  4.  
sorry, my code is a bit sloppy and I did not want to just post the whole code but hopefully you can work it out from there!
Feb 8 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2249
by: Michael Whittaker | last post by:
Hello! I have a problem with my php script. The script's task is to search an IP-Database with ranges as entries and find, in which range the entered IP is. OK, I've queried the MySQL-Results of the ranges into an array, containing the "IP ==> internalid" where internalid is the Primarykey of the table from which I can get
1
1686
by: Ravi Shankar | last post by:
Hi all, I have a calendar application( like Microsoft Outlook) writtn in Java.Whenever an event is created, we can set SMS/EMAIL notification. Hence when an event is created, I am storing that event info and notification time into a database.Now the notification can be ranging from 5 minutes to say one day.Hence I need to query the databse every 5 minutes and fetch the data and do send the notification directly. I understand that such a...
3
1825
by: Keith | last post by:
I am fairly new to SQL so sorry if this is a really dumb question. I have a small (still) SQL database, which I am trying to query from an ASP page. The field I am querying is of DATETIME data type, and is populated automatically using the GetDate() function as a default value. When I try and search on this field, using a date/time in the format dd/mm/yyyy hh:mm:ss as the search criteria, it fails with the following
1
2095
by: valexena | last post by:
After a few minutes of querying the database my session disconnects abruptly. Can somebody help me and tell why can be happening? -- Posted via http://dbforums.com
1
1412
by: Ron L. | last post by:
Hi, Any suggestions for a good intorudction to OLAP querying with MDX? Thanks, Ron. -- Performance Intelligence, Inc. Spy 4 DB2 - http://www.pireporting.com/spy4db2.html
6
2648
by: Greg | last post by:
I am working on a project that will have about 500,000 records in an XML document. This document will need to be queried with XPath, and records will need to be updated. I was thinking about splitting up the XML into several XML documents (perhaps 50,000 per document) to be more efficient but this will make things a lot more complex because the searching needs to go accross all 500,000 records. Can anyone point me to some best practices...
5
2367
by: Shane | last post by:
I wonder if someone has any ideas about the following. I am currently producing some reports for a manufacturing company who work with metal. A finished part can contain multiple sub-parts to make up the finished part. The sub-parts can also be made up of sub-parts and those sub-parts can also be made up of sub-parts etc etc. All parts are contained within the same table and I have a seperate table
3
1577
by: MDB | last post by:
I'd normally Google for a question like this, and hope to snag a few examples along with the answer, but this time I can't see to get the keywords specific enough. Or I'd ask coworkers, but they're just as new to ASP.NET as I am. Is it possible to have a dataset filled with all the records in an SQL table (on the small side, maybe three hundred records total), and then query that table for subsets of data, e.q. a simple WHERE clause,...
2
1736
by: RajSharma | last post by:
Hi, I am facing a problem regarding querying thru a large table having millions of rows....... Its hanging in between while querying for all those rows Can anybody suggest me a query regarding : Querying the database everytime for next 100 records ( that means i need to set up a cursor) till the count of the table rows ends up(take 1 million rows e.g.) The database is DB2
4
1522
by: =?Utf-8?B?U3VoYXMgVmVuZ2lsYXQ=?= | last post by:
Hello, I am facing an issue while querying Active directory using C# code with system.DirectoryServices namespace. Here is the path for my LDAP - "LDAP://CN=XY - C++/Unix and other, OU=Automatic,OU=DLs,DC=domain,DC=com"; I have replaced the / character with\/- but still is giving error while querying. Please help me to resolve this.
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8332
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2750
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.