473,387 Members | 1,486 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,387 software developers and data experts.

Help with executing access query

I have written 6 Queries in an Access DB, which are executed in a For
Each…Next loop to populate DataSet object. A new DataTable object is created
in DataSet for each query, as follows

Private Sub GetRawLiRatios()
Dim cnn As OleDbConnection
Dim cmd As New OleDbCommand
Dim dad As OleDbDataAdapter
Dim dstRawFigures As New DataSet

Dim arrRatios As String() = [Enum].GetNames(GetType(LiRatios))
Dim obj As New DataStructureConverter

Try
'! Initialize OleDbConnection object.
cnn = CreateConnection()

'! Set connection property for OleDbCommand object.
cmd.Connection = cnn

'! Add parameters to the paramter collection.
cmd.Parameters.Add("IndustryName", cboIndustry.Text)
cmd.Parameters.Add("Year", nudYear.Value)

'! Set the command type property of the command object.
cmd.CommandType = CommandType.StoredProcedure

For Each ratio As String In arrRatios
'! Set the command text property of the command object
'! to the name of the query.
cmd.CommandText = ratio

'! Initialize data adapter object with command object.
dad = New OleDbDataAdapter(cmd)

'! Fill the dataset.
dad.Fill(dstRawFigures, ratio)

Next ratio

Catch ioExcep As IO.IOException
MessageBox.Show(ioExcep.Message, Application.ProductName,
MessageBoxButtons.OK, MessageBoxIcon.Error)

Catch ex As Exception
Call HandleException(Me.Name, ex, "GetRawLiRatios")
End Try

End Sub

Where arrRatios is an Enumeration which contains name of the queries in
access db. If any of the queries get deleted from the access db, For
Each...Next loop is exited and exception is thrown.

Now I want my For Each...Next loop to continue processing for reaming
queries in the db, rather than throwing an exception. How can I check whether
queries exists in the db before using Fill method?

Thanks

Jul 21 '05 #1
1 1633
On Mon, 10 Jan 2005 23:11:01 -0800, "Job Lot" <Jo****@discussions.microsoft.com> wrote:

¤ I have written 6 Queries in an Access DB, which are executed in a For
¤ Each…Next loop to populate DataSet object. A new DataTable object is created
¤ in DataSet for each query, as follows
¤
¤ Private Sub GetRawLiRatios()
¤ Dim cnn As OleDbConnection
¤ Dim cmd As New OleDbCommand
¤ Dim dad As OleDbDataAdapter
¤ Dim dstRawFigures As New DataSet
¤
¤ Dim arrRatios As String() = [Enum].GetNames(GetType(LiRatios))
¤ Dim obj As New DataStructureConverter
¤
¤ Try
¤ '! Initialize OleDbConnection object.
¤ cnn = CreateConnection()
¤
¤ '! Set connection property for OleDbCommand object.
¤ cmd.Connection = cnn
¤
¤ '! Add parameters to the paramter collection.
¤ cmd.Parameters.Add("IndustryName", cboIndustry.Text)
¤ cmd.Parameters.Add("Year", nudYear.Value)
¤
¤ '! Set the command type property of the command object.
¤ cmd.CommandType = CommandType.StoredProcedure
¤
¤ For Each ratio As String In arrRatios
¤ '! Set the command text property of the command object
¤ '! to the name of the query.
¤ cmd.CommandText = ratio
¤
¤ '! Initialize data adapter object with command object.
¤ dad = New OleDbDataAdapter(cmd)
¤
¤ '! Fill the dataset.
¤ dad.Fill(dstRawFigures, ratio)
¤
¤ Next ratio
¤
¤ Catch ioExcep As IO.IOException
¤ MessageBox.Show(ioExcep.Message, Application.ProductName,
¤ MessageBoxButtons.OK, MessageBoxIcon.Error)
¤
¤ Catch ex As Exception
¤ Call HandleException(Me.Name, ex, "GetRawLiRatios")
¤ End Try
¤
¤ End Sub
¤
¤ Where arrRatios is an Enumeration which contains name of the queries in
¤ access db. If any of the queries get deleted from the access db, For
¤ Each...Next loop is exited and exception is thrown.
¤
¤ Now I want my For Each...Next loop to continue processing for reaming
¤ queries in the db, rather than throwing an exception. How can I check whether
¤ queries exists in the db before using Fill method?
¤
¤ Thanks

You should be able to use the GetOleDbSchemaTable method of the OleDbConnection object. Can't recall
exactly what the difference is but check for Procedures and Views:

Dim DatabaseConnection As New System.Data.OleDb.OleDbConnection

Dim SchemaTable As DataTable

DatabaseConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=E:\My Documents\db1.mdb"

DatabaseConnection.Open()

'Retrieve schema information about Catalog.

SchemaTable =
DatabaseConnection.GetOleDbSchemaTable(System.Data .OleDb.OleDbSchemaGuid.Procedures, _
New Object() {Nothing, Nothing, "ValidateUser"})

'SchemaTable =
DatabaseConnection.GetOleDbSchemaTable(System.Data .OleDb.OleDbSchemaGuid.Views, _
' New Object() {Nothing, Nothing, "Query2"})

If SchemaTable.Rows.Count <> 0 Then
Console.WriteLine("QueryDef " & SchemaTable.Rows(0)!PROCEDURE_NAME.ToString & " does not
exist")
Else
Console.WriteLine("QueryDef does not exist")
End If

'displays the properties
DataGrid1.DataSource = SchemaTable

DatabaseConnection.Close()
Paul ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Jul 21 '05 #2

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

Similar topics

3
by: Neil Hindry | last post by:
I wonder if you can help me. I have setup an address-book database in Access XP. I have the first name & surname as separate fields. As I wanted to sort my database by surname and then by first...
5
by: Art | last post by:
Hi, Can anyone point me to an example of how I would execute a query I've created in an Access DB. I've copied the SQL down to my VB.net application and that works fine, but it's ugly. I'd like...
1
by: Job Lot | last post by:
I have written 6 Queries in an Access DB, which are executed in a For Each…Next loop to populate DataSet object. A new DataTable object is created in DataSet for each query, as follows Private...
7
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason...
8
by: daD | last post by:
I'm trying to write a small database that tracks people coming and going from a small campground. I need to have the current guests in the "current" table" and then have the ability to check them...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
8
by: SAL | last post by:
Hello, Using the designer in my project, I created a DataTable for one of the tables in an Access database. I created a TableAdapter for that DataTable by adding queries. For the most part, the...
1
by: jesmi | last post by:
my code is: U]employee.cfm <html> <head> <title>Employee List</title> </head> <body> <h1>Employee List</h1>
1
by: contactrajib | last post by:
I have a situation where one of the app server thread is executing an update statement on A record and at the same time another thread is trying to read it by executing a select query. Now we are...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.