Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old June 15th, 2005, 05:32 AM
Registered User
 
Join Date: Jun 2005
Location: Spalding, Lincolnshire, United Kingdom.
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
__________________
Abaxt
Reply With Quote
  #2 (permalink)  
Old June 15th, 2005, 08:15 AM
Friend of Wrox
 
Join Date: Nov 2004
Location: Seattle, WA, .
Posts: 248
Thanks: 0
Thanked 0 Times in 0 Posts
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
Reply With Quote
  #3 (permalink)  
Old July 7th, 2005, 11:05 AM
Registered User
 
Join Date: Jul 2005
Location: , , USA.
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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 04: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



All times are GMT -4. The time now is 02:56 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.