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

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

759 512MB
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#post3701560
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 3416
NeoPa
32,556 Expert Mod 16PB
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 512MB
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 Expert Mod 2GB
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,556 Expert Mod 16PB
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 Expert Mod 2GB
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,556 Expert Mod 16PB
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 512MB
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,556 Expert Mod 16PB
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 512MB
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
TheSmileyCoder
2,322 Expert Mod 2GB
Im afraid that part of this may become hard to understand since neither you nor me, are native english speakers.

I really use variation of techniques. Lets say I need to empty out a temporary table, I would simply do:
Expand|Select|Wrap|Line Numbers
  1. Currentdb.Excecute "DELETE * FROM TableName",dbFailOnError
Such a query for example I dont think I would ever bother to write out as a stored querydef. I think it would just add unnecessary clutter to the query window.

I think one of the more advanced procedures I have involves:
Creating a document.
First a temporary table is created, and loaded with all of the paragrahs (records) to go into the document.

Find references
Now each paragrach may contain one or more references to other paragrahs in the same or in other documents. I first do a query to identify all paragrahs with references in them, and them to a another temporary table.

Locate documents
Based on this temporary table I identify to which document the reference points.

Build secondary Documents
I then create X temporary tables containing each of the secondary documents, in order to identify the paragraph number of each refence.

Search n replace
I then do a search n replace of the original document paragraph in which the reference was found, replacing the reference with the target documents Name and Paragraph number.

Now I doubt I could do all this without using codes, since I dont know the tables names beforehand. I couldn't possibly define the queries beforehand. (At least I dont think so).


I dont really know if that answered your question, feel free to ask again.
Feb 8 '12 #11

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

Similar topics

2
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...
7
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. ...
5
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:...
0
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...
4
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...
0
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...
4
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" ...
5
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...
1
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...
4
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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.