I am hoping that someone could please hep me with this problem.
I am using Access as a front end system with linked tables to SQL Server Management Studio, using ODBC. This has made the database run slow, so I am trying to convert the Access queries in to Stored procedures in SSMS, and then run them as pass through queries in Access. This has worked great so far, and the queries that I have converted are running very fast.
However I have only converted the queries that do not require user chosen variables within Access. These are the ones that I am now having difficulty with.
For example, let's say I have a simple table in SSMS called [Names] consisting of [First Name], [Surname]
I can run the following stored procedure, named [Search], to return all names:
SELECT [First name], [Surname]
FROM [Names]
I can execute this stored procedure in the front end Access as a pass through query, and it does return all the records.
However, within the front end Access database, I have a form called [Search], with a textbox named "SearchFirstName". When the user manually types in 'Anthony' into the textbox, I would like it to to take the value from the textbox, and use it as part of the stored procedure to limit the results, as follows:
SELECT [First name], [Surname]
FROM [Names]
WHERE [First Name] = [Forms]![Search]![SearchFirstName]
I have searched for help, but I am becoming stuck. I think I need Access to set the textbox value as a parameter, which can then be used in SSMS.
The avenue that I have found which might work from another article is below, however I am not sure how/where I would need to implement it:
With CurrentDb.QueryDefs("qPass") .SQL = "exec SalesGeneralSearch " & strSearch Set Me.MyListBox.RowSource = .OpenRecordset End If
If anyone could help, I would be so grateful
Thank you
3条答案
按热度按时间njthzxwz1#
This is how I execute SQL stored procedures in MS Access (VBA):
5lwkijsr2#
You can use this:
' now open report based on above.
Or for a reocrdset, go:
ny6fqffe3#
A bit of an update to my original question. By using the help from here, I have managed to get the query to work for one variable. It works as follows:
SQL Stored procedure:
Then in Access, I have a pass through query which is:
Then Finally, on my form, I have a button which runs the following VB on click:
As I say this works really well. It gets the results that I want it too. However, I would also like to add extra search variables, for example SURNAME. I would also need it to work, if any of the text boxes in Access were null, or somehow set their default value to a wildcard.
I have tried the following
SQL Stored Procedure
Then in Access, I have a pass through query which is:
Then Finally, on my form, I have a button which runs the following VB on click:
However, when I try adding the second variable it does not work, and the vba debugger says 'Compile Error'
I am hoping that it is something simple which I am doing wrong