Solved

Correct Syntax for SQL statement

Posted on 2011-03-09
9
556 Views
Last Modified: 2012-05-11
I am trying to put a stored procedure in my code in VBA , but for some reason I can't figure the proper syntax to return the value.
I am able to get the result when I run in SQL but I can' t run it in my VBA code.

this is what I have in my sql server query:
DECLARE @I_sCompanyID smallint, @O_mNoteIndex numeric(19,5), @O_iErrorState int;
select @I_sCompanyID = CMPANYID from DYNAMICS..SY01500 where INTERID = 'TEST4';
exec DYNAMICS.[dbo].[smGetNextNoteIndex] @I_sCompanyID, 1, @O_mNoteIndex output,
@O_iErrorState output; SELECT @O_mNoteIndex

and my vba code is below , but when I try to get my value , it always return 1 instead of
711.00000

what am I missing?

thanks
David
Private Sub ButSave_Click()
Dim intReturn As Integer

    'Retrieve an ADO Connection for the current user
    Set cn = UserInfoGet.CreateADOConnection
    'Set the current Database company
    cn.DefaultDatabase = UserInfoGet.IntercompanyID
    cmd.ActiveConnection = cn

'Get next note id and then insert batch record
strsql = "DECLARE @I_sCompanyID smallint, @O_mNoteIndex numeric(19,5), @O_iErrorState int; " & _
         "select @I_sCompanyID = CMPANYID from DYNAMICS..SY01500 where INTERID = 'TEST4'; " & _
         "exec DYNAMICS.[dbo].[smGetNextNoteIndex] @I_sCompanyID, 1, @O_mNoteIndex output, " & _
         "@O_iErrorState output; SELECT @O_mNoteIndex"

cmd.CommandText = strsql
cmd.Execute intReturn
MsgBox (intReturn)





End Sub

Open in new window

SQLScript.JPG
0
Comment
Question by:taverny
  • 5
  • 3
9 Comments
 
LVL 17

Expert Comment

by:Daniel Reynolds
ID: 35089068
Why not just call the stored procedure?

strsql = "exec myStoredProcedure 'param1','param2'"
 
0
 

Author Comment

by:taverny
ID: 35090190
i thought that was what I was doing ,
I am actually trying to follow the info from an other question that I have open , and one expert told me to do it this way.
it seems to work in SQL but I guess I don't know how to store the value returned from this SQL statment
here is the original post:
http://www.experts-exchange.com/Microsoft/Applications/Microsoft_Dynamics/Q_26843202.html
0
 

Author Comment

by:taverny
ID: 35118761
can someone help me on getting the value back from the sql statment?
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 6

Expert Comment

by:judgeking
ID: 35140422
I've attached an example of how I call a SP and get the output.  Basically, you have to define the SP parms, and whether it's an input or output.

But using your code above, you may be able to get the result by changing the code to this:

'(change the last bit of your SQL to SELECT @O_mNoteIndex AS NoteIndex
Dim oRs 'returned recordset

Set oRs = cmd.Execute(intReturn) 'intReturn only tells us how many rows were affected
On error resume next
MsgBox oRs(0) 'Not sure if index starts at 0 or 1
MsgBox oRs(1) 'Not sure if index starts at 0 or 1
MsgBox oRs("NoteIndex") 'May be able to use the output name if you make the change above


Const adCmdStoredProc   = 4
  Const adParamInput      = 1
  Const adParamOutput     = 2
  Const adInteger         = 3
  Const adDate            = 7
  Const adVarChar         = 200

  Set oCommand = CreateObject("ADODB.Command")
  
  oCommand.ActiveConnection = Conn
  oCommand.CommandTimeout = 600
  oCommand.CommandType = adCmdStoredProc
  oCommand.CommandText = "spAddReprintTransaction"
  oCommand.Parameters.Append oCommand.CreateParameter("@EntryID", adInteger, adParamInput, , EntryID)
  oCommand.Parameters.Append oCommand.CreateParameter("@DistribType", adVarChar, adParamInput, 50, DistribType)
  oCommand.Parameters.Append oCommand.CreateParameter("@FaxNumber", adVarChar, adParamInput, 50, FaxNumber)
  oCommand.Parameters.Append oCommand.CreateParameter("@UserName", adVarChar, adParamInput, 50, UserName)
  oCommand.Parameters.Append oCommand.CreateParameter("@FailureID", adInteger, adParamInput, , FailureID)
  oCommand.Parameters.Append oCommand.CreateParameter("@TransID", adInteger, adParamOutput)
  oCommand.Execute
  lTransID = oCommand.Parameters("@TransID")

Open in new window

0
 

Author Comment

by:taverny
ID: 35142666
Thank you for your reponse . I am gonna try that now.
0
 

Author Comment

by:taverny
ID: 35150727
at the same time that you reponded another expert also responded to my other questions. I will post his answer in here.
but since you seem to understand very well stored procedure , may be you can explain me a little more your code so I can try to do it on my own the next time it happens again.
this is what I understand so far if I look at your code:
line 1 thru 6 create constant with the value specified on the right
line 8 thru 11  we create a connection to the server

then that's where I am not following , line 12 13 , I am not sure if it is a defined name or it is a stored procedure.
then the following append the new constant to the stored procedure, how do you know what the stored procedure expect from the code.

thanks for your helpl, but I am very new to store procedure
0
 
LVL 6

Accepted Solution

by:
judgeking earned 500 total points
ID: 35172205
Yes, spAddReprintTransaction is the name of a SP in my DB.  The next lines set up the parms for the SP (with values if input parms), with TransID being an output parm, that's how I retrieve the value on line 21.  When calling any SP, you have to know what parms it expects and what types of values they are (int, char, datetime, etc) or else the call will fail.

The code below might work for you, but you need to find out the name of the second parm, I've just put the name @Flag for now:
Const adCmdStoredProc   = 4
  Const adParamInput      = 1
  Const adParamOutput     = 2
  Const adSmallInt        = 2
  Const adInteger         = 3
  Const adDate            = 7
  Const adVarChar         = 200
  Const adNumeric         = 131

  Dim oCommand
  Dim iCompanyID, iNoteIndex
  
  Set oCommand = CreateObject("ADODB.Command")  
  Set oRs = cn.Execute("select CMPANYID from DYNAMICS..SY01500 where INTERID = 'TEST4'")
  iCompanyID = oRs("CMPANYID")
  oCommand.ActiveConnection = cn
  oCommand.CommandTimeout = 600
  oCommand.CommandType = adCmdStoredProc
  oCommand.CommandText = "smGetNextNoteIndex"
  oCommand.Parameters.Append oCommand.CreateParameter("@I_sCompanyID", adSmallInt, adParamInput, , iCompanyID)
  oCommand.Parameters.Append oCommand.CreateParameter("@Flag", adInteger, adParamInput, , 1)
  oCommand.Parameters.Append oCommand.CreateParameter("@O_mNoteIndex", adNumeric, adParamOutput)
  oCommand.Parameters.Append oCommand.CreateParameter("@O_iErrorState", adInteger, adParamOutput)
  oCommand.Execute
  iNoteIndex = oCommand.Parameters("@O_mNoteIndex")

Open in new window

0
 

Author Comment

by:taverny
ID: 35234816
Thanks for the response.
I will probably need your help in the near future regarding stored procedure , but thanks for your help already.
0
 
LVL 6

Expert Comment

by:judgeking
ID: 35235265
No problem, thanks for the 'accept'.  
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
For cloud, the “train has left the station” and in the Microsoft ERP & CRM world, that means the next generation of enterprise software from Microsoft is here: Dynamics 365 is Microsoft’s new integrated business solution that unifies CRM and ERP fun…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

813 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

10 Experts available now in Live!

Get 1:1 Help Now