Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 15th, 2005, 05:32 AM
Registered User
 
Join Date: Jun 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Passing Variable MS Proj to SQL Stored Procedure

Hi

Please bear with me as I'm really new to programming and I'm self taught basic SQL and very little VBA due to being in at the deep end at work.

I have a SQL database with an MS Access Project front end to it.
I have written a simple SQL stored procedure which looks up all instances of a word using the variable @find_customer appearing in a customer name field in a table.

I would like to key a keyword into a form within the MS Access Project and it to return all instances of customers with this key word appearing in its CUST_NAME field. Can anyone point me in the right direction on how to do this please. I have just purchased Sams Teach yourself VB in 21 days but can't find what I'm looking for.

Apologies to experienced programmers as I'm sure this is a really basic question.
Regards
Angie


Abaxt
 
Old June 15th, 2005, 08:15 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 248
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Angie,

I'm not well versed in stored procedures. But I think you can do what you want with a simple query. Something like:

Select [field list] From [customer table]
Where [CUST_NAME] like """*" & form_filter_field & "*"""

The route you take to do this depends on how you're going to build the form. Overall, you will probably use the AfterUpdate event of form_filter_field.

For example, if your form is bound to the table and the filter field is not, you could have:
Code:
Private Sub form_filter_field_AfterUpdate ()
   If IsNull(Me.form_filter_field) then
      Me.Filter = ""
      Me.FilterOn = False
   Else
      Me.Filter = "[CUST_NAME] LIKE ""*" & Me.form_filter_field & "*""
      Me.FilterOn = True
      Me.Requery
   End If
End Sub
Or, you could change the RecordSource of the form. For example:
Code:
Private Sub form_filter_field_AfterUpdate ()
   Me.RecordSource = "Select [fields] From [table]"
   If Not IsNull(Me.form_filter_field) then
      Me.RecordSource = Me.RecordSource & _
               " Where [CUST_NAME] LIKE ""*" & Me.form_filter_field & "*"""
   End If
   Me.Requery
End Sub
If the results will be displayed in a subform, you could simply build the query for the subform like this:

Select [fields] from [table] where [CUST_NAME] like ""*" & [Forms]![nameofyourmainform].form_filter_field & "*"""

Using this method, you won't have to write any code. The subform will automatically filter. You will have to manually build the Master and Child Link fields for the subform. Also, your subform won't work unless the form "nameofyourmainform" is open -- which is not really a problem since your intent will be to use the subform on the main form, it's just difficult to test the subform independent of the main form.

Now, when you say you would like to "return all instances of customers...", you could mean that you want to display them. If so, one of the above solutions will handle that. However, since you mentioned that you wrote a stored procedure, your intent could be to be able to manipulate each of the instances. In that case you'll want to use the SQL to open a record set in VBA. If that is the case, you probably want a more advanced book, something that includes VBA code examples. For example our book "Access 2003 VBA Programmer's Reference". The book is available through Wrox. But when I say "our book" I mean me and the other people who wrote chapters for that book.

Good luck.

Randall J Weers
Membership Vice President
Pacific NorthWest Access Developers Group
http://www.pnwadg.org
 
Old July 7th, 2005, 11:05 AM
Registered User
 
Join Date: Jul 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Abaxt,

I just ran into this issue myself. If you are using the ADODB Connection method (which you probably are, if you're using a ODBC database connection), you can pass parameter(s) to a strored procedure using the Execute method as follows:


    Private Sub SomeSubprocedure()
    Dim varProcedureParam1 As Variant
    Dim varProcedureParam2 As Variant
    Dim cn As ADODB.Connection
    Dim cmd As ADODB.Command
    Dim cmd_name As String

    Set cn = New ADODB.Connection
    Set cmd = New ADODB.Command

    cmd_name = "StoredProcedureName"

    'Reference the ADODB help within Visual Basic editor for more on the ADODB.Open() method
    cn.Open "ODBCname", "User", "Password"

    'Note: The ADODB.Connection object must be instantiated before the Command object can be bound
    Set cmd.ActiveConnection = cn
    cmd.CommandText = cmd_name

    varProcedureParam1 = "Some String"
    varProcedureParam2 = 1234

    'The parameters are passed as a variant array
    'Reference the ADODB help within Visual Basic editor for more on the ADODB.Execute() method
    cmd.Execute , Array(varProcedureParam1, varProcedureParam2), adCmdStoredProc
    End Sub





Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing parameters to a sql stored procedure digby_dog VB.NET 2002/2003 Basics 0 July 11th, 2005 08:35 AM
output parameter from MS SQL stored procedure nav1 VB How-To 0 March 14th, 2005 05:00 PM
Passing a parameter value to Stored Procedure mcinar SQL Server 2000 9 October 3rd, 2004 09:42 PM
MS SQL Stored procedure and VBA jesseleon Pro VB Databases 0 July 30th, 2004 01:16 PM
passing stored procedure shoakat SQL Server 2000 1 July 15th, 2004 09:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.