473,549 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Discussion: Using SQL string in VBA VS passing parameters to query

759 Contributor
Hello !
I started using Access about one year ago.
Thank you ALL for help me all this time.
I wish to thank especially to Smiley, for his patience at least, who help me with the very basic steps at the beginning.

Reading this thread,
http://bytes.com/topic/access/answers/931314-auto-fill-query-increment-number#post3701 560
I see that you, Smiley, use a copy-paste solution to insert a query SQL into VBA code in order to set (by VBA) a parameter.
More other experts from here use SQL strings in VBA. So I suspect there are good reasons to do that.

On the other hand, in my opinion, for the maintenance reasons, is better to use the query itself and pass to the query the parameters you need. If at a latter time is needed to modify the query is enough to use the design view for that query and no need to change anything in VBA.
Of course this is available for "nice" SQL which can be generated using the design view interface for queries.

So I ask you, all experts, to enlightened me when a solution is better than the other one, where (when) should use parametric queries and where (when) should use SQL in VBA and, of course, WHY to chose one way or another one.

Thank you very much !
Feb 5 '12 #1
10 3429
NeoPa
32,564 Recognized Expert Moderator MVP
This is an interesting question, that often confuses newer database developers when they come into contact with the work of more experienced developers. I remember bumping into this myself in my early days when we had an Access consultant in to handle a particular project.

In many cases a QueryDef (Saved Access Query) can be used in projects wherever a SQL string can. In fact, there are some places (within Domain Aggragate function calls for instance - DLookup(), DMax(), DAvg, etc) where SQL strings are not applicable at all.

More experienced db developers though, being more conversant with SQL and tending to think in SQL, would often prefer to think of a query in SQL and simply create it in the VBA code there and then. The balance of where and when this occurs is generally down to the developer themself, but issues to consider are :
  1. How frequently it's used.
  2. How complex it is.
If it's called from more than one place - that is the same query - then a QueryDef has more weight. If it's a very short string with little to it then a SQL string has. QueryDefs are pre-optimised, so a large complex query that is called from multiple places (essentially a fundamental part of the project) may even be designed as a QueryDef by very experienced developers as they will see the benefit of avoiding redoing the optimisation phase. It's a judgement call.

Where a SQL string really becomes necessary rather than just preferred, is where the SQL of the query depends on values available only at run-time. Consider a form that allows you to select the table and fields of what you want to see in a report. The SELECT and FROM clauses are both dependent an what is selected, so designing them into a QueryDef (at design-time) would be impossible. Handling that with a SQL string from VBA however, is perfectly reasonable. I'll say at this time that making changes to a QueryDef in code is actually possible too, certainly with an MDB or ACCDB file. It's highly dubious though as a developer typically wants to know what they're working with and the designs of their main objects are an important part of that. Possible then, but to be avoided in almost all situations.

Filtering is another matter entirely of course. Reports and forms certainly, have a WhereCondition (filtering) parameter, which can be passed on opening, that allows a filter to be specified even for a QueryDef. This means that the RecordSource parameter (The source that the form or report is bound to) can easily be a QueryDef, even if filtering is required.

I hope this gives you something to consider. Others may offer their own unique perspective to the discussion.
Feb 5 '12 #2
Mihail
759 Contributor
Thank you, NeoPa.
I am happy you understand my English and you point the discussion to the right way.
I fully understand what you say. Also I understand that my skill is not enough to see as far as you can.
Of course I expect other posts in order to make (for myself) as good idea as is possible.
Thank you again !
Feb 5 '12 #3
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
I have never learnt how to combine parameter queries with VBA. So thats one reason for me not using them. I

f a query does not require parameters, I might design and save it as a query object, and then run it from VBA, but often I also like that the SQL for the code is stored with the code. It makes it easier when reading and reviewing my code that I don't have to open a second object.

Thats really my 2 cents on the subject.
Feb 5 '12 #4
NeoPa
32,564 Recognized Expert Moderator MVP
FYI Smiley - A QueryDef with parameters, when given an object reference in VBA, presents a Parameters() collection such that parameters may be set prior to invoking the query itself. If you ever feel the need to do something requiring that then we here can help you with it I'm sure :-)
Feb 5 '12 #5
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
Hi Neo. Thanks for the information. I have no doubt you guys could help me figure it out, but so far I haven't really had the need for it (or realised that I should have done it, so thats another reason for me to follow this thread.)

One thing I do remember reading somewhere is that access can more quickly process stored queries then queries with SQL syntax generated through VBA. I haven't confirmed nor denied it myself yet though.
Feb 5 '12 #6
NeoPa
32,564 Recognized Expert Moderator MVP
That's covered in post #2 Smiley. QueryDefs have the optimisation stored when they're first actually run. This is generally helpful as it saves the delay of Jet preparing the process order/optimisation on the fly. It's only a negative issue when the data is so changed from the first run that an alternative approach would be more efficient on this occasion. The order/optimisation for a SQL string is always assessed whenever it's run.
Feb 5 '12 #7
Mihail
759 Contributor
I see that my "discussion " is a very short one.
I think that NeoPa fully covered the subject in post #2.
I wish to thank all of you.
Feb 7 '12 #8
NeoPa
32,564 Recognized Expert Moderator MVP
Thanks for the question Mihail. If you think you're the only person who was ever curious about these issues then you're very much mistaken. I suspect this thread can be a real benefit to many others needing to understand exactly what you asked about. Good for you :-)
Feb 7 '12 #9
Mihail
759 Contributor
Help me to understand your technique, Smiley.
You encapsulate the SQL into procedures (subroutines) ? Then you pass values for variables via procedure interface ? Or you use another approach ?
Feb 8 '12 #10

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

Similar topics

2
17346
by: zlatko | last post by:
There is a form in an Access Project (.adp, Access front end with SQL Server) for entering data into a table for temporary storing. Then, by clicking a botton, several action stored procedures (update, append) should be activated in order to transfer data to other tables. I tried to avoid any coding in VB, as I am not a professional, but I...
7
49561
by: Pavils Jurjans | last post by:
Hallo, I have been programming for restricted environments where Internet Explorer is a standard, so I haven't stumbled upon this problem until now, when I need to write a DOM-compatible code. The question is about best practices for passing parameters to an event function. I have, say, the following HTML:
5
11101
by: Bill | last post by:
I need to pass a static parameter to an included ASP file. I'm thinking about using this with a query string, but upon reflection I'm not sure how I can do this. For example: ---file start: FIRST.ASP ------ <% Server.Execute("second.asp?a=1") %> <!--- ***** or ***** --->
0
3308
by: Zlatko Matić | last post by:
Hi everybody! Recently I was struggling with client/server issues in MS Access/PostgreSQL combination. Although Access is intuitive and easy to use desktop database solution, many problems appear when someone is trying to use it as front-end for real server database systems such as PostgreSQL or MySQL. One of these problems is regarding...
4
2978
by: Mike Dinnis | last post by:
Hi, I've been working through a number of turorials to try to learn more about retrieving data from a SQL database. I think i've mastered techniques where i create a sql string in the page and pass it to the Db and retrieveing data from a stored procedure, but I can't get the hang of parameters. I have a method where I can get the...
0
2236
by: Neelima Godugu | last post by:
Hi All, I have developed a windows forms user control, which I am going to host in Internet Explorer.. I am familiar with the security settings requirement inorder to do the above. I have successfully gotten it working. The only problem I have is with passing parameters to the Windows Forms User control from IE I am using the Object tag...
4
7151
by: David Freeman | last post by:
Hi There! I'm just wondering if there's a way to pass parameters (as if you were passing parameters to a ASCX web control) when calling an ASPX page? e.g. MyDetailsPage.UserName = "david" OR... the only way to do it is to use the QueryString or Session object?
5
2354
by: =?Utf-8?B?SnVsaWEgQg==?= | last post by:
Hi, I posted last week about a problem I've got and didn't get any responses, but I've managed to come up with a workaround and a possible cause (guessing). I was wondering if someone could confirm my theories and give me some information on the proper way to handle this. I have a search results web page that is opened with info in the...
1
3463
by: Gert Wurzer | last post by:
Hi! I have an asp.net 2.0 web application that uses an iframe to access another asp.bet 2.0 webapp via https. I've already found a working prototype solution to create a dynamic iframe taking URL parameters with an asp.net 2.0 literal. My problem is, that i can't use the URL parameters due to security reasons. Are there any other...
4
2808
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi, just wondering if anyone can provide a brief example of passing parameters from one webpage to another, C# VS2005? I need to pass several selected values of dropdown list boxes to a secondary page. I have a response.redirect("secondpage") in a button click event. Thanks. -- Paul G Software engineer.
0
7532
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...
0
7461
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...
0
7730
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. ...
1
7491
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...
0
6055
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...
0
5101
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...
0
3509
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...
1
1956
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
1
1068
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.