Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel Add-In Connecting to a SQL database

Posted on 2011-09-12
3
Medium Priority
?
236 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:Murray Brown
  • 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 2000 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:Murray Brown
ID: 36524384
thanks very much
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

972 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