Solved

Excel Add-In Connecting to a SQL database

Posted on 2011-09-12
3
230 Views
Last Modified: 2012-08-13
Hi

I want to connect to my online SQL database  in my Excel 2007 Add-In in VB.net.
I used the code below to connect via VBA.
How do I change it to connect in VB.net?
What Imports do I use
Sub A()
    Call oAppend(Now, "yet more", 45)
End Sub

Public Sub oAppend(ByVal oDate As Date, ByVal oText As String, ByVal oNumber As Single)
 
    Dim oSQL As String
    
    On Error GoTo EH
    
    Set con = New ADODB.Connection
    
    con.Open "Provider=SQLOLEDB;Data Source=196.220.411.247,1235;Network Library=DBMSSOCN;Initial Catalog=test;User ID=nmomashhjjk;Password=nnnmmm222;"

    Set cmd = New ADODB.Command

    'Check last ID ---------------------------
    
    Dim oSQL_lastID As String
    oSQL_lastID = "SELECT MAX(ID) FROM Table1"

    
    Dim Last_ID_Recordset As Variant
    Dim Last_ID_Integer As Integer
    Dim Next_ID_Recordset As Variant
    Dim Next_ID_Integer As Integer
    Dim intID As String
    
    With cmd
        .CommandText = oSQL_lastID
        .CommandType = adCmdText
        .ActiveConnection = con
        '.Execute
        Last_ID_Recordset = .Execute() 'this line returns a recordset
        Last_ID_Integer = Last_ID_Recordset(0)
    End With

Open in new window

0
Comment
Question by:murbro
  • 2
3 Comments
 
LVL 16

Expert Comment

by:carsRST
ID: 36524204
See connection strings example.
Includes the libraries to import.
http://www.connectionstrings.com/sql-server-2008

0
 
LVL 16

Accepted Solution

by:
carsRST earned 500 total points
ID: 36524218
This example is helpful as well

Imports System.Data.SqlClient

connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
        connection = New SqlConnection(connetionString)



http://vb.net-informations.com/dataadapter/dataadapter-dataset-sqlserver.htm
0
 

Author Closing Comment

by:murbro
ID: 36524384
thanks very much
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

861 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