Solved

VB6: How to call SQLServer 2000 system procedure sp_fkeys

Posted on 2004-09-01
4
221 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
[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
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

697 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