Click here to Skip to main content
16,004,602 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I need some help in completing one of my activity which requires Excel vba and SQL server tables...


SQL server Table ->

1) Maintain_Table-->Which consists of only one values that is 0 or 1.

Action
1) On Excel sheet will have a button "click here".
2) When user clicks on the button should first check if Maintain_Table has a value 0 or 1 and show popup message accordingly.

What I have tried:

i have googled but could not get any help.
Posted
Updated 28-Mar-17 3:50am
v2

1 solution

You can call this method by click on button.
This is just an example how to connect SQL Server over ADO:

Sub ConnectSqlServer()

    Dim conn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim sConnString As String
 
    ' Create the connection string.
    sConnString = "Provider=SQLOLEDB;Data Source=INSTANCE\SQLEXPRESS;" & _
                  "Initial Catalog=MyDatabaseName;" & _
                  "Integrated Security=SSPI;"
    
    ' Create the Connection and Recordset objects.
    Set conn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    
    ' Open the connection and execute.
    conn.Open sConnString
    Set rs = conn.Execute("SELECT * FROM Table1;")
    
    ' Check we have data.
    If Not rs.EOF Then
        ' Transfer result.
        Sheets(1).Range("A1").CopyFromRecordset rs
    ' Close the recordset
        rs.Close
    Else
        MsgBox "Error: No records returned.", vbCritical
    End If

    ' Clean up
    If CBool(conn.State And adStateOpen) Then conn.Close
    Set conn = Nothing
    Set rs = Nothing
    
End Sub
 
Share this answer
 
Comments
Leo Chapiro 28-Mar-17 10:32am    
If this task is too hard for you consider to purchase a programming service instead of trying to do it by yourself. I marked your post as "Not a question" .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900