Microsoft Dynamics GP Developing for Dynamics GP
A blog dedicated to the Microsoft Dynamics GP Developer & Consultant community
 
Welcome to MSDN Blogs Sign in | Join | Help

Developing for Dynamics GP

by David Musgrave (Australia) and the Microsoft Dynamics GP Developer Support Team (USA)

News

  • Please use the Blog Feedback? - Contact Us link at the top of the page to email questions relating to the blog itself.

    If you wish to ask a technical question, please use the links below to ask on the Newsgroups. If you ask on the Newsgroups, others in the community can respond and the answers are available for everyone in the future.

    Please do not use comments on pages and posts to ask questions unrelated to the topic on that page or post.



    Dates of Interest:

    11-Jul-2008: Blog Created by David Musgrave.
    10-Oct-2008: First Post by Scott Stephenson.
    04-Nov-2008: First Post by Dave Dusek.
    11-Nov-2008: First Post by Beth Gardner.
    28-Nov-2008: First Post by Chris Roehrich.
    30-Dec-2008: First Post by Patrick Roth.
    24-Feb-2009: First Post by Greg Willson.
    22-Apr-2009: First Post by David Clauson.
    04-May-2009: First Post by Ryan Wigestrand.
    19-Jun-2009: First Post by Dawn Langlie.
    03-Jul-2009: First Post by Emily Halvorson.



    WorldMaps Statistics since
    24-Feb-2009:






    Translator Tool:




    Disclaimer

    This blog is provided "AS IS" with no warranties, and confers no rights.

    The links in this blog may lead to third-party Web sites. Microsoft provides third-party resources to help you find customer service and/or technical support resources. Information at these sites may change without notice. Microsoft is not responsible for the content at any third-party Web sites and does not guarantee the accuracy of third-party information.

Contents

Favourite Posts

Blog Links

Newsgroups Links

Resources Links

Using ADO with VBA with Report Writer

David MeegoAs a follow on to the Using VBA with Report Writer post, I would like to discuss using ActiveX Data Objects to access data from SQL Server while using the Report Writer.

Sometimes it is not possible to use Report Writer to create a table relationship to access the data that you want to add to a report. In these situations, you can use Visual Basic for Applications (VBA) to access field in any Microsoft SQL Server table by using ActiveX Data Object (ADO) to connect.

The best practice is as follows:

  • Open the connection when the report starts.
  • Access the desired data using the before section events.
  • Close the connection when the report ends.

When you follow this practice, the connection is opened once when the report is opened, and is not continuously opened and closed as the report prints each section.

Assuming that the report fields (or fields in tables already linked to the report) can be used to uniquely identify the desired row in the SQL table, you can create a SQL Select statement with a where clause based on the values from the report.  You can then create a blank calculated field to return the data from VBA to the report.

Note: Placeholder names have been used in the example code.  They are surrounded by braces {}.

Sample Code 

The methods of opening the connection to SQL Server are different depending on the version of Microsoft Dynamics GP being used. Example scripts follow:

Method 1: Microsoft Dynamics GP 10.0

Dim cn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim cmd As New ADODB.Command
Dim sqlstring As String

Private Sub Report_Start()
    ' ADO Connection
    Set cn = UserInfoGet.CreateADOConnection
    'Use a client-side cursor so that a recordset count can be obtained later.
    cn.CursorLocation = 3
    'set the database to the currently logged in db
    cn.DefaultDatabase = UserInfoGet.IntercompanyID
End Sub

Private Sub Report_BeforeBody(SuppressBand As Boolean)
    sqlstring = "SELECT {SQLFIELD} FROM {SQLTABLE} WHERE {KEYFIELD} = '" & RTrim({ReportKeyField}) & "'"
   
    ' ADO Command
    cmd.ActiveConnection = cn
    ' adCmdText
    cmd.CommandType = 1
    ' Command
    cmd.CommandText = sqlstring
       
   ' Pass through SQL
    Set rst = cmd.Execute
    If Not (rst.EOF And rst.BOF) Then
        {ReportCalcField}.Value = RTrim(rst!{SQLFIELD})
    End If
    rst.Close
End Sub

Private Sub Report_End()
    ' Close ADO Connection
    If rst.State = adStateOpen Then rst.Close
    If cn.State = adStateOpen Then cn.Close
    Set cn = Nothing
    Set rst = Nothing
    Set cmd = Nothing
End Sub

 

Method 2: Microsoft Dynamics GP 9.0

Use the RetrieveGlobals9.dll file to return an ADO connection object that lets you connect to Microsoft Dynamics GP data. To download the RetrieveGlobals9.dll file together with its documentation, visit one of the following Microsoft Web sites, depending on whether you are a customer or a partner:

Customer: Modifier/VBA Samples for Microsoft Dynamics GP 9.0 Secure Link

Partner: Modifier/VBA Samples for Microsoft Dynamics GP 9.0 Secure Link

Dim cn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim cmd As New ADODB.Command
Dim sqlstring As String

Private Sub Report_Start()
    Dim userinfo As New RetrieveGlobals9.retrieveuserinfo
    Dim luserid As String
    Dim lintercompanyid As String
    Dim lsqldatasourcename As String
    Dim ldate As Date

    ' RetrieveGlobals
    lsqldatasourcename = userinfo.sql_datasourcename()
    luserid = userinfo.retrieve_user()
    lintercompanyid = userinfo.intercompany_id()
    ldate = CStr(userinfo.user_date())

    ' ADO Connection
    Set cn = userinfo.Connection
    'Use a client-side cursor so that a recordset count can be obtained later.
    cn.CursorLocation = 3
    'set the database to the currently logged in db
    cn.DefaultDatabase = lintercompanyid
End Sub

Private Sub Report_BeforeBody(SuppressBand As Boolean)
    sqlstring = "SELECT {SQLFIELD} FROM {SQLTABLE} WHERE {KEYFIELD} = '" & RTrim({ReportKeyField}) & "'"
   
    ' ADO Command
    cmd.ActiveConnection = cn
    ' adCmdText
    cmd.CommandType = 1
    ' Command
    cmd.CommandText = sqlstring
       
   ' Pass through SQL
    Set rst = cmd.Execute
    If Not (rst.EOF And rst.BOF) Then
        {ReportCalcField}.Value = RTrim(rst!{SQLFIELD})
    End If
    rst.Close
End Sub

Private Sub Report_End()
    ' Close ADO Connection
    If rst.State = adStateOpen Then rst.Close
    If cn.State = adStateOpen Then cn.Close
    Set cn = Nothing
    Set rst = Nothing
    Set cmd = Nothing
End Sub

 

Method 3: Microsoft Business Solutions - Great Plains 8.0

Use the RetrieveGlobals.dll file to return an ADO connection object that lets you connect to Microsoft Dynamics GP data. To download the RetrieveGlobals.dll file together with its documentation, visit one of the following Microsoft Web sites, depending on whether you are a customer or a partner.

Customer: Modifier/VBA Samples for Great Plains 8.0 Secure Link

Partner: Modifier/VBA Samples for Great Plains 8.0 Secure Link

Dim cn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim cmd As New ADODB.Command
Dim sqlstring As String

Private Sub Report_Start()
    Dim userinfo As New RetrieveGlobals.retrieveuserinfo
    Dim luserid As String
    Dim lintercompanyid As String
    Dim lsqldatasourcename As String
    Dim lsqlpassword As String
    Dim constring As String

    ' RetrieveGlobals
    lsqldatasourcename = userinfo.sql_datasourcename()
    luserid = userinfo.retrieve_user()
    lsqlpassword = userinfo.sql_password()
    lintercompanyid = userinfo.intercompany_id()
    'MsgBox (luserid & " " & lsqlpassword & " " & lintercompanyid & " " & lsqldatasourcename)

    ' Create Connection String
    constring = "Provider=MSDASQL" & _
                ";Data Source=" & lsqldatasourcename & _
                ";User ID=" & luserid & _
                ";Password=" & lsqlpassword & _
                ";Initial Catalog=" & lintercompanyid
    'MsgBox constring

    ' ADO Connection
    With cn
        .ConnectionString = constring
        .CursorLocation = 3 ' adClient
        .Open
    End With
End Sub

Private Sub Report_BeforeBody(SuppressBand As Boolean)
    sqlstring = "SELECT {SQLFIELD} FROM {SQLTABLE} WHERE {KEYFIELD} = '" & RTrim({ReportKeyField}) & "'"
   
    ' ADO Command
    cmd.ActiveConnection = cn
    ' adCmdText
    cmd.CommandType = 1
    ' Command
    cmd.CommandText = sqlstring
       
   ' Pass through SQL
    Set rst = cmd.Execute
    If Not (rst.EOF And rst.BOF) Then
        {ReportCalcField}.Value = RTrim(rst!{SQLFIELD})
    End If
    rst.Close
End Sub

Private Sub Report_End()
    ' Close ADO Connection
    If rst.State = adStateOpen Then rst.Close
    If cn.State = adStateOpen Then cn.Close
    Set cn = Nothing
    Set rst = Nothing
    Set cmd = Nothing
End Sub

 

For more information on how to use ADO on reports and in a window, these Knowledge Base (KB) articles are a good reference:

How to use an ActiveX Data Object (ADO) with VBA on a report in Microsoft Dynamics GP (KB 954619) Secure Link

How to use ActiveX Data Object (ADO) with VBA on a window with Microsoft Dynamics GP and with Microsoft Business Solutions - Great Plains 8.0 (KB 942327) Secure Link

You can also look at the example code in the postsbelow:

Modifier - Adding a field to a scrolling window using ADO Example

RW - Accessing any SQL data from a Report Example 

 

Hope you find this information useful. 

David

23-Feb-2009: Add Link to KB 954619.

Posted: Thursday, October 30, 2008 11:10 AM by David Musgrave
Filed under: , ,

Comments

DynamicAccounting.net said:

Developing for Dynamics GP is full of new stuff today including Using VBA with Report Writer , Using

# October 30, 2008 9:51 AM

David Musgrave said:

# October 30, 2008 9:17 PM

Developing for Dynamics GP said:

The question in more detail is whether it is possible to print "Page: Y/X" or "Page: Y

# May 12, 2009 9:18 PM

Developing for Dynamics GP said:

The question in more detail is whether it is possible to print "Page: Y/X" or "Page: Y of X" on a Report

# May 12, 2009 10:44 PM

Developing for Dynamics GP said:

When the inventory module of Microsoft Dynamics GP was first written the Item Description field was 60

# May 25, 2009 3:06 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker