Solved

VB6: How to call SQLServer 2000 system procedure sp_fkeys

Posted on 2004-09-01
4
216 Views
Last Modified: 2013-12-25
This is a frustrating one.  If I use "sp_fkeys 'tblControl'" in Query Analzyer, I get the information I'm after (relationships the table is participating in).  HOW do I call this stored procedure from VB6?? It's driving me nuts!

I've tried the code below (which I got off the web somewhere) and passed in "sp_fkeys" and "tblControl".  I don't get any errors, but the recordset has no records in it.  When I run it through Query Analyser, 3 records return (which is correct, since the table is participating in three relationships):

Public Function CallStoredProc(sProcName As String, sParameter As String) As Integer
    'TH 22/01/01 call a stored procedure from vb
    Dim cmd As New Command
    Dim prm As New ADODB.Parameter
    Dim rs As New ADODB.Recordset
    'create new stored proc object
    Set cmd = New ADODB.Command
    'add a paramter with value of "sParameter"
    Set prm = cmd.CreateParameter("param1", adBSTR, adParamInput, 1, sParameter)

    With cmd
        .Parameters.Append prm
        .ActiveConnection = gcn
        .CommandType = adCmdStoredProc
        .CommandText = sProcName
    End With
   
    Set rs = cmd.Execute
   
    If Not rs.State = adStateOpen Then
        rs.Open
    End If
    If gcn.Errors.Count > 0 Then
        MsgBox gcn.Errors.Item(0)
    End If
    'Set frmMain.list1.DataSource = rs
    MsgBox rs.RecordCount
End Function
0
Comment
Question by:MushyPeas
4 Comments
 
LVL 29

Accepted Solution

by:
leonstryker earned 250 total points
ID: 11963339
Hmm, try this:

Public Function CallStoredProc(sProcName As String, sParameter As String) As Integer
Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset
    rs.Open "Exec " & sProcName & "'" & sParameter & "'"
    MsgBox rs.RecordCount
    CallStoredProc = rs.RecordCount
End Function


Leon
0
 
LVL 1

Author Comment

by:MushyPeas
ID: 11969268
Thank you!!!  Can you explain this to me - why "Exec".  Is this not the same as saying cmd.execute (which I also tried)

Is this an acceptable/efficient/safe method to use?

Thanks

M
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now