使用excel VBA验证SQL服务器表 [英] Validation with SQL server tables with excel VBA

查看:75
本文介绍了使用excel VBA验证SQL服务器表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助才能完成我的一项需要Excel vba和SQL服务器表的活动...





SQL server表 - >



1)Maintain_Table - >其中只包含一个0或1的值。



动作

1)在Excel工作表上会有一个按钮点击这里。

2)当用户点击按钮时,应首先检查Maintain_Table的值是0还是1并相应地显示弹出消息。



我尝试过:



i已经谷歌搜索但无法获得任何帮助。

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.

推荐答案

您可以通过点击按钮调用此方法。

这只是一个例子如何通过ADO连接SQL Server:



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


这篇关于使用excel VBA验证SQL服务器表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆