Solved

Excel Add-In Connecting to a SQL database

Posted on 2011-09-12
3
226 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

762 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

16 Experts available now in Live!

Get 1:1 Help Now