Solved

Excel Add-In Connecting to a SQL database

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vlookup for in-between dates 4 39
Excel Conditional Statements 11 39
ASP/VB email question 4 35
Clear Filter 8 40
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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 on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

777 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