<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Connect to SQL Database in Inventor Programming Forum</title>
    <link>https://forums.autodesk.com/t5/inventor-programming-forum/connect-to-sql-database/m-p/2133479#M152665</link>
    <description>Thanks for the reply Joe.  I have the Microsoft ActiveX Data Objects 2.8 Library check box selected under Tools--&amp;gt;References.  Do I somehow need to reference it in the code?&lt;BR /&gt;
&lt;BR /&gt;
I have the following reference check boxes selected:&lt;BR /&gt;
Visual Basic For Applications&lt;BR /&gt;
Autodesk Inventor Object Library&lt;BR /&gt;
OLE Automation&lt;BR /&gt;
Microsoft Forms 2.0 Object Library&lt;BR /&gt;
Microsoft ActiveX Data Objects 2.8 Library&lt;BR /&gt;
&lt;BR /&gt;
Do I need anything else? Thanks.  I have cleaned up my code a bit but it still won't connect:&lt;BR /&gt;
&lt;BR /&gt;
Public Sub GetData_Click()&lt;BR /&gt;
&lt;BR /&gt;
Dim oConn As New ADODB.Connection&lt;BR /&gt;
Set oConn = CreateObject("ADODB.Connection")&lt;BR /&gt;
&lt;BR /&gt;
Dim oRecord As New ADODB.Recordset&lt;BR /&gt;
Set oRecord = CreateObject("ADODB.Recordset")&lt;BR /&gt;
&lt;BR /&gt;
With oConn&lt;BR /&gt;
    .Provider = "SQLOLEDB.1;"&lt;BR /&gt;
    .ConnectionString = "Data Source=ServerName; Initial Catalog=DatabaseName; Integrated Security=SSPI"&lt;BR /&gt;
    .Open&lt;BR /&gt;
End With&lt;BR /&gt;
&lt;BR /&gt;
If oConn.State = adStateOpen Then&lt;BR /&gt;
    Debug.Print "Connection successfully opened."&lt;BR /&gt;
Else&lt;BR /&gt;
    Debug.Print "Connection failed."&lt;BR /&gt;
End If&lt;BR /&gt;
&lt;BR /&gt;
oRecord.ActiveConnection = oConn&lt;BR /&gt;
oRecord.Open "Select * FROM dbo.TableName", oConn, adOpenForwardOnly, adLockOptimistic&lt;BR /&gt;
&lt;BR /&gt;
oRecord.Close&lt;BR /&gt;
oConn.Close&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
End Sub</description>
    <pubDate>Fri, 07 Dec 2007 20:20:24 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2007-12-07T20:20:24Z</dc:date>
    <item>
      <title>Connect to SQL Database</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/connect-to-sql-database/m-p/2133477#M152663</link>
      <description>I would like to be able to connect to an SQL database via code in VBA.  I have been struggling the code to open the connection.  Does anyone have experience doing this or some sample code on how to establish the connection?  Here is what I have so far:&lt;BR /&gt;
&lt;BR /&gt;
Public Sub GetData&lt;BR /&gt;
&lt;BR /&gt;
Dim oConn As New ADODB.Connection&lt;BR /&gt;
&lt;BR /&gt;
Dim strConn As String&lt;BR /&gt;
strConn = "PROVIDER=SQLOLEDB;"&lt;BR /&gt;
strConn = strConn &amp;amp; "Data Source=ServerName;Initial Catalog = XYZDatabase;"&lt;BR /&gt;
strConn = strConn &amp;amp; " Integrated Security = SSPI;"&lt;BR /&gt;
&lt;BR /&gt;
oConn.Open strConn&lt;BR /&gt;
&lt;BR /&gt;
'Open recordset and manipulate data here&lt;BR /&gt;
&lt;BR /&gt;
oConn.Close&lt;BR /&gt;
&lt;BR /&gt;
End Sub&lt;BR /&gt;
&lt;BR /&gt;
I get a User Type not defined error on Dim oConn As New ADODB.Connection.  Thanks for any help. -Rob</description>
      <pubDate>Fri, 07 Dec 2007 16:47:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/connect-to-sql-database/m-p/2133477#M152663</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-12-07T16:47:50Z</dc:date>
    </item>
    <item>
      <title>Re: Connect to SQL Database</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/connect-to-sql-database/m-p/2133478#M152664</link>
      <description>You first need to set a reference to the "Microsoft ActiveX Data Objects 2.x &lt;BR /&gt;
Library" using &lt;PROJECT&gt;.&lt;BR /&gt;
&lt;BR /&gt;
Joe ...&lt;BR /&gt;
&lt;BR /&gt;
&lt;KOREDOVA&gt; wrote in message news:5795555@discussion.autodesk.com...&lt;BR /&gt;
I would like to be able to connect to an SQL database via code in VBA.  I &lt;BR /&gt;
have been struggling the code to open the connection.  Does anyone have &lt;BR /&gt;
experience doing this or some sample code on how to establish the &lt;BR /&gt;
connection?  Here is what I have so far:&lt;BR /&gt;
&lt;BR /&gt;
Public Sub GetData&lt;BR /&gt;
&lt;BR /&gt;
Dim oConn As New ADODB.Connection&lt;BR /&gt;
&lt;BR /&gt;
Dim strConn As String&lt;BR /&gt;
strConn = "PROVIDER=SQLOLEDB;"&lt;BR /&gt;
strConn = strConn &amp;amp; "Data Source=ServerName;Initial Catalog = XYZDatabase;"&lt;BR /&gt;
strConn = strConn &amp;amp; " Integrated Security = SSPI;"&lt;BR /&gt;
&lt;BR /&gt;
oConn.Open strConn&lt;BR /&gt;
&lt;BR /&gt;
'Open recordset and manipulate data here&lt;BR /&gt;
&lt;BR /&gt;
oConn.Close&lt;BR /&gt;
&lt;BR /&gt;
End Sub&lt;BR /&gt;
&lt;BR /&gt;
I get a User Type not defined error on Dim oConn As New ADODB.Connection. &lt;BR /&gt;
Thanks for any help. -Rob&lt;/KOREDOVA&gt;&lt;/PROJECT&gt;</description>
      <pubDate>Fri, 07 Dec 2007 20:06:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/connect-to-sql-database/m-p/2133478#M152664</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-12-07T20:06:06Z</dc:date>
    </item>
    <item>
      <title>Re: Connect to SQL Database</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/connect-to-sql-database/m-p/2133479#M152665</link>
      <description>Thanks for the reply Joe.  I have the Microsoft ActiveX Data Objects 2.8 Library check box selected under Tools--&amp;gt;References.  Do I somehow need to reference it in the code?&lt;BR /&gt;
&lt;BR /&gt;
I have the following reference check boxes selected:&lt;BR /&gt;
Visual Basic For Applications&lt;BR /&gt;
Autodesk Inventor Object Library&lt;BR /&gt;
OLE Automation&lt;BR /&gt;
Microsoft Forms 2.0 Object Library&lt;BR /&gt;
Microsoft ActiveX Data Objects 2.8 Library&lt;BR /&gt;
&lt;BR /&gt;
Do I need anything else? Thanks.  I have cleaned up my code a bit but it still won't connect:&lt;BR /&gt;
&lt;BR /&gt;
Public Sub GetData_Click()&lt;BR /&gt;
&lt;BR /&gt;
Dim oConn As New ADODB.Connection&lt;BR /&gt;
Set oConn = CreateObject("ADODB.Connection")&lt;BR /&gt;
&lt;BR /&gt;
Dim oRecord As New ADODB.Recordset&lt;BR /&gt;
Set oRecord = CreateObject("ADODB.Recordset")&lt;BR /&gt;
&lt;BR /&gt;
With oConn&lt;BR /&gt;
    .Provider = "SQLOLEDB.1;"&lt;BR /&gt;
    .ConnectionString = "Data Source=ServerName; Initial Catalog=DatabaseName; Integrated Security=SSPI"&lt;BR /&gt;
    .Open&lt;BR /&gt;
End With&lt;BR /&gt;
&lt;BR /&gt;
If oConn.State = adStateOpen Then&lt;BR /&gt;
    Debug.Print "Connection successfully opened."&lt;BR /&gt;
Else&lt;BR /&gt;
    Debug.Print "Connection failed."&lt;BR /&gt;
End If&lt;BR /&gt;
&lt;BR /&gt;
oRecord.ActiveConnection = oConn&lt;BR /&gt;
oRecord.Open "Select * FROM dbo.TableName", oConn, adOpenForwardOnly, adLockOptimistic&lt;BR /&gt;
&lt;BR /&gt;
oRecord.Close&lt;BR /&gt;
oConn.Close&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
End Sub</description>
      <pubDate>Fri, 07 Dec 2007 20:20:24 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/connect-to-sql-database/m-p/2133479#M152665</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-12-07T20:20:24Z</dc:date>
    </item>
    <item>
      <title>Re: Connect to SQL Database</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/connect-to-sql-database/m-p/2133480#M152666</link>
      <description>Ok, I got it to work.  I needed to connect to a specific SQL server instance instead of just the server.&lt;BR /&gt;
&lt;BR /&gt;
...Data Source=ServerName\InstanceName;...&lt;BR /&gt;
&lt;BR /&gt;
I was missing the InstanceName.  Thanks.</description>
      <pubDate>Fri, 07 Dec 2007 21:07:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/connect-to-sql-database/m-p/2133480#M152666</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-12-07T21:07:53Z</dc:date>
    </item>
    <item>
      <title>Re: Connect to SQL Database</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/connect-to-sql-database/m-p/2133481#M152667</link>
      <description>Here is the function I use to create the connection, you'll need to customize for your server.  Also note that you may or may not want to use the INTEGRATED SECURITY.  We have a single user account that all users use to access the SQL database.  However, if INTEGRATED SECURITY=sspi, then it will try to log in as your windows login name.  However, if each user has their own account, then definitely use it so you don't have to embed the password (a security risk).  You could also prompt for password, but our users don't actually know who they are logging in as, they just press a button on the toolbar &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;
&lt;BR /&gt;
-----------------------------------------------------------------------&lt;BR /&gt;
&lt;BR /&gt;
Function CreateSQLConnection() As ADODB.Connection&lt;BR /&gt;
&lt;BR /&gt;
    Dim sProvider As String&lt;BR /&gt;
    Dim sServer As String&lt;BR /&gt;
    Dim sCatalog As String&lt;BR /&gt;
    Dim sUser As String&lt;BR /&gt;
    Dim sPassword As String&lt;BR /&gt;
    &lt;BR /&gt;
    sProvider = "SQLOLEDB"&lt;BR /&gt;
    sServer = 'your server name here&lt;BR /&gt;
    sCatalog = 'your catalog here&lt;BR /&gt;
    sUser = 'your user account here&lt;BR /&gt;
    sPassword = ' your password here&lt;BR /&gt;
    &lt;BR /&gt;
    ' Create a connection object.&lt;BR /&gt;
    Dim cnDB As ADODB.Connection&lt;BR /&gt;
    Set cnDB = New ADODB.Connection&lt;BR /&gt;
    &lt;BR /&gt;
    ' Provide the connection string.&lt;BR /&gt;
    Dim strConn As String&lt;BR /&gt;
    &lt;BR /&gt;
    'Use the SQL Server OLE DB Provider.&lt;BR /&gt;
    strConn = strConn &amp;amp; "PROVIDER=" &amp;amp; sProvider &amp;amp; ";"&lt;BR /&gt;
    &lt;BR /&gt;
    'Connect to the database on the server.&lt;BR /&gt;
    strConn = strConn &amp;amp; "DATA SOURCE=" &amp;amp; sServer &amp;amp; ";" &amp;amp; "INITIAL CATALOG=" &amp;amp; sCatalog &amp;amp; ";"&lt;BR /&gt;
    &lt;BR /&gt;
    'Set user and password&lt;BR /&gt;
    strConn = strConn &amp;amp; "User Id=" &amp;amp; sUser &amp;amp; ";" &amp;amp; "Password=" &amp;amp; sPassword &amp;amp; ";"&lt;BR /&gt;
    &lt;BR /&gt;
    'Use an integrated login (NOTE: DON'T USE WHEN LOGGING IN FROM OTHER USER ACCOUNT)&lt;BR /&gt;
    'strConn = strConn &amp;amp; "INTEGRATED SECURITY=sspi;"&lt;BR /&gt;
    &lt;BR /&gt;
    'Open the connection&lt;BR /&gt;
    On Error Resume Next&lt;BR /&gt;
    &lt;BR /&gt;
        cnDB.Open strConn&lt;BR /&gt;
        If Err Then&lt;BR /&gt;
            Debug.Print Err.Description&lt;BR /&gt;
            Debug.Print Err.Number&lt;BR /&gt;
            Debug.Print Err.Source&lt;BR /&gt;
            Err.Clear&lt;BR /&gt;
            Return&lt;BR /&gt;
        End If&lt;BR /&gt;
                    &lt;BR /&gt;
    On Error GoTo 0&lt;BR /&gt;
    &lt;BR /&gt;
    Set CreateSQLConnection = cnDB&lt;BR /&gt;
&lt;BR /&gt;
End Function&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
-----------------------------------------------------------------------</description>
      <pubDate>Mon, 10 Dec 2007 18:48:47 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/connect-to-sql-database/m-p/2133481#M152667</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-12-10T18:48:47Z</dc:date>
    </item>
    <item>
      <title>Re: Connect to SQL Database</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/connect-to-sql-database/m-p/2133482#M152668</link>
      <description>Josh, thanks for the info and the code.  The integrated security will work in our case since each user has their own account.  Half of the program data comes from our SQL based order tracking system and half is entered by the user on a form.  A magic button on the form combines the two and prevents any potentially painful thinking. &lt;span class="lia-unicode-emoji" title=":face_with_tongue:"&gt;😛&lt;/span&gt;</description>
      <pubDate>Mon, 10 Dec 2007 19:28:44 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/connect-to-sql-database/m-p/2133482#M152668</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-12-10T19:28:44Z</dc:date>
    </item>
  </channel>
</rss>

