473,750 Members | 2,596 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling a query in VB

20 New Member
Im currently using a string to store my query in VB.
It seems to work perfectly fine for short queries.
But as my queries become longer and longer, this method just doesnt seem to work.
Im using an access database and when i run my SQL queries in that they work fine its just when i transfer them to the vb code they seem to cause errors.

So is there a way to call saved access queries in vb without actually putting the query string in the code?

This is the code im currently using it would be nice if a solution followed this general pattern of interacting with a database:

Note: the strSportSQL string is actually all on one line, this forum displays it differently.

Expand|Select|Wrap|Line Numbers
  1.  'Read Sports
  2.             Dim strSportSQL As String
  3.             strSportSQL = "SELECT DISTINCT TblSport.sportName FROM TblSport INNER JOIN ((TblLeague INNER JOIN TblLeagueSeason ON TblLeague.leagueID = TblLeagueSeason.leagueID) INNER JOIN TblLeagueSeasonTeam ON TblLeagueSeason.leagueSeasonID = TblLeagueSeasonTeam.leagueSeasonID) ON TblSport.sportID = TblLeague.sportID GROUP BY TblSport.sportName, TblLeague.leagueID, TblLeague.size, TblLeague.leagueName HAVING (((Count(TblLeagueSeasonTeam.teamID))<[size]));"
  4.  
  5.             Dim DataConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & Server.MapPath("App_Data/Database.mdb"))
  6.  
  7.             Dim SportCommand As New OleDbCommand(strSportSQL, DataConn)
  8.  
  9.             DataConn.Open()
  10.  
Mar 31 '08 #1
6 1439
Ashlewis
20 New Member
Sorry to double post I cant seem to edit my original one.
Ive further narrowed down the problem to it causes this error, when ever i use a query with GROUP BY in it.

(in this case im using GROUP BY and HAVING)
Expand|Select|Wrap|Line Numbers
  1. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
  2.  
  3. Exception Details: System.Data.OleDb.OleDbException: IErrorInfo.GetDescription failed with E_FAIL(0x80004005).
  4.  
Mar 31 '08 #2
balabaster
797 Recognized Expert Contributor
To run presaved queries in Access, you treat them as you would a stored procedure:

Expand|Select|Wrap|Line Numbers
  1. Dim oCmd As New OledbCommand("qryMySavedQuery")
  2. oCmd.CommandType = CommandType.StoredProcedure
  3. ''If you need to add parameters, use the following syntax
  4. 'oCmd.Parameters.Add(New OledbParameter("InputParam1", "Id"))
  5. Dim oRdr As OledbDataReader = oCmd.ExecuteReader()
Expand|Select|Wrap|Line Numbers
  1. OledbCommand oCmd = new OledbCommand("qryMySavedQuery");
  2. oCmd.CommandType = CommandType.StoredProcedure;
  3. // //If you need to add parameters, use the following syntax
  4. //oCmd.Parameters.Add(new OledbParameter("InputParam1", "Id"));
  5. OledbDataReader oRdr = oCmd.ExecuteReader();
That should simplify your life from all those hideous stringbuilder queries.
Mar 31 '08 #3
Ashlewis
20 New Member
Thank you.
(Im coding in VB)

Now I have another problem,
What if i want to do somthing like this

strSQL = "SELECT userName FROM tblUser WHERE userName = ' " & txtUsername.Tex t & " ' "

but obviously doing it the tidy way you showed me.

I mean how do would I use the parameter code, how does it correspond to an SQL statement.
Mar 31 '08 #4
Ashlewis
20 New Member
Please ignore that above post, i was having a moment of madness

Im now using the parameter code to run a similar query but this time the query obviously takes a parameter. This parameter is called selectedUser

This is how ive used it
Expand|Select|Wrap|Line Numbers
  1. Dim DataConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & Server.MapPath("App_Data/Database.mdb"))
  2.         Dim LeagueCommand As New OleDbCommand("QryOpenLeagues")
  3.         oCommand.CommandType = Data.CommandType.StoredProcedure
  4.         oCommand.Connection = DataConn
  5.         oCommand.Parameters.Add(New OleDbParameter(ddlUser.SelectedValue, "selectedUser"))
  6.  
I get a "Data type mismatch in criteria expression. " error for the reader i run after.
Mar 31 '08 #5
balabaster
797 Recognized Expert Contributor
In answer to your question with regards to using regular query strings:

Expand|Select|Wrap|Line Numbers
  1. Dim strQuery = "Select * From MyTable Where Field1 = @Param1"
  2. Dim oCmd As New OledbCommand(strQuery, oCon)
  3. oCmd.Parameters.Add(New OledbParameter("Param1", SelectedValue)
  4. Dim oRdr As OledbDataReader = oCmd.ExecuteReader()
In answer to your second question - the run-time error - you have your parameter and your value the wrong way round in line 5.

You've got New OledbParameter( Value, Parameter)
it should be New OledbParameter( Parameter, Value)
Apr 1 '08 #6
Ashlewis
20 New Member
Thank you very very much!
As helpful as ever.
Apr 1 '08 #7

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

Similar topics

2
4751
by: Shailan | last post by:
Hi Im having trouble with the following code that seems to be behave differently when called from the browser as opposed to the command line. The calling script is a cgi that forks, with the child trying to call another cgi script and pass arguments to it. It works fine from the command line, and calls the required script and passes the arguments correctly. However, when it is run on the browser, it calls the script but does not pass...
2
1773
by: Andy | last post by:
Hello, I have a question regarding how to format a date in VB so that I can call it from a query and get results. I'm calling functions in the query because that was the only way I found I could set up a query with multiple parameters to return data the way I wanted. (I'm reading up on a filter recordset command that will probably do this much better, but I haven't got it figured out yet.) The query calls all the functions and pulls up...
6
1773
by: NOSPAMrclark | last post by:
I'm wondering . . . . What is the generally accepted method for calling reports from multiple locations? Example: REPORT-A is called by selecting a record in a listbox and clicking a button. The queries behind the record use the listbox value (selected) as criteria to limit/build the report data.
7
60533
by: Regnab | last post by:
I know this has been covered before but being a beginner at SQL, and only marginally better at VBA, I'm struggling. All I wish to do is call the following function which is in "Microsoft Office Access Class Objects": Function GetPropertyCode() As String GetPropertyCode = PropertyCode End Function
15
11788
by: Bryan | last post by:
I have a multi-threaded C# console application that uses WMI (System.Management namespace) to make RPC calls to several servers (600+ ) and returns ScheduledJobs. The section of my code that performs the query is contained in a delegate function that I execute via a second thread. On 1 or 2 of the 600+ servers the query hangs. I've tried to use Thread.Join() coupled with a Thread.Abort() but this does not kill the thread. Based on...
1
4666
by: | last post by:
Hey all, Just starting to play with calling DLL's outside the .NET sandbox and I'm hoping for some help with the following: The function prototypes: public static extern long GMW_DS_Query( string szSQL,
3
2514
by: thecoolone | last post by:
I am trying to call a function from "a href" inside the same page. this is the code: echo "<br><br><a href=\"isearch($query)\">More results from Mysite</a>"; // calling the function isearch function isearch($query) {$query=urlencode($query);
5
7852
by: Stinky Pete | last post by:
Hi (again) ;-) I'm still very much at the bottom of a steep learning curve with VB, so any and all help is always appreciated. I've found some code to generate the user names who have logged onto the file in question and from what I've been reading it will do what I need. However, what is eluding me is the "calling" it bit. If anyone can put into simple terms what is needed & general principles I would be really grateful.
4
1520
by: MLH | last post by:
I have the following saved UNION query named qryPeople2NameInNPaperAd: SELECT & " " & & " " & & " " & & ", " & & " " & AS Item, tblVehicleJobs.VehicleJobID FROM tblVehicleJobs INNER JOIN tblAddnlOwnrs ON tblVehicleJobs.VehicleJobID = tblAddnlOwnrs.VehicleJobID Where tblVehicleJobs.VehicleJobID = GetCurrentVehicleJobID(); UNION SELECT & " " & & " " &
4
3588
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And Each Category Name has its corresponding Category order, Input field and a submit button. The Category name is being fetched from the oracle db along with the corresponding Category order. In the corresponding input field (text box) the user enters a new category order which gets stored in the...
0
9001
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
9583
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
9396
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
9342
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6808
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
6081
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();...
2
2807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.