Solved

Can you send a WHERE clause from access to Microsoft SQL server

Posted on 2014-11-14
6
288 Views
Last Modified: 2014-11-14
I have a screen in access where I am allowing the user to choose what they do and do not want selected from a query. I am building the WHERE clause dynamically based off of what they select. This query takes way to long to run in access, but only takes about eight seconds in Microsoft SQL server. I have created a stored procedure in SQL server and I want to pass the WHERE clause that I have built into it, but it wants the variable to be set equal to something instead of just the statement in order to prevent SQL injection attacks. I have also tried to use exec() and pass everything as a string, but i have strings in my WHERE clause which prevent the entire string from passing. ex. 'This is a example ' strVar ' that I pass'. Is there any way I can pass the information I want into SQL server?

Thanks
0
Comment
Question by:rcimasi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 37

Expert Comment

by:PatHartman
ID: 40443797
You have to build the query in VBA so that all the variables can be expanded first.

strSQL = "Select ...., From ... WHERE ClientID = " & Me.ClientID & " AND " OrderDate > #" & Me.FromDate & "#;"
0
 

Author Comment

by:rcimasi
ID: 40443812
On my form I just have radio buttons, two for each choice, once to include that item, one to not include that item. I do not know what they will choose or if they will choose it. When they select something I simply add it to the WHERE clause.

WhereClause = WhereClause + "tblProject.ProjectStatus = 2"

I then just pass WhereClause into SQL server where it will hopefully run it.
Would I have to create a variable for each item on the page and pass it to SQL server?
0
 
LVL 37

Assisted Solution

by:PatHartman
PatHartman earned 250 total points
ID: 40443828
You need to create the ENTIRE SQL string.  So, create a variable that includes the static part and then concatenate the variable part to make a complete string.  Save the string as a pass-through querydef.  Replace the Form's RecordSource with the name of the new pass-through query.
0
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 
LVL 11

Accepted Solution

by:
John_Vidmar earned 250 total points
ID: 40443839
I too would build the query in VBA:
strSQL = "SELECT ... FROM ... WHERE ..."

if boolean-expression-based-on-what-user-selected then
	strSQL = strSQL + " AND somefield = whatever"
end if

if another-boolean-expression-based-on-a-different-user-selection then
	strSQL = strSQL + " AND someotherfield = somethingelse"
end if

Open in new window

If you are selecting from one table, and there is no initial WHERE-clause then I do this:
strSQL = "SELECT ... FROM ... WHERE 1=1 "

if boolean-expression-based-on-what-user-selected then
	strSQL = strSQL + " AND somefield = whatever"
end if

if another-boolean-expression-based-on-a-different-user-selection then
	strSQL = strSQL + " AND someotherfield = somethingelse"
end if

Open in new window

0
 

Author Comment

by:rcimasi
ID: 40443865
I was able to pass just my WHERE clause into SQL server by creating a variable in SQL server:

@SQL = 'SELECT .... FROM ... WHERE ' + @WhereClause
exec(@SQL)

But building the query in VBA and running the query as exec(@VBA) would work as well.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40443899
Looks like we have a winner here, but in case it helps I have an article called Migrating your Access Queries to SQL Server Transact-SQL that is a big honkin' comparison between Access and SQL Server T-SQL.  If it helps please click the big green 'Was this article helpful?' button at the end.
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question