码迷,mamicode.com
首页 > 数据库 > 详细

excel VBA动态链接数据库

时间:2019-04-29 09:29:04      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:class   des   rom   exce   hda   bsp   VID   sql   OLE   

最近车间制造部有需求,需要通过excel填写的ID获取数据库中的某个取值。研究了一下VBA,简要记录一下代码,仅供参考。

Function GETPMAX(SID As String) As String

    Dim result As String
    
    链接数据库
    Set Cnn = CreateObject("ADODB.Connection")
    Set rt = CreateObject("ADODB.Recordset")
    
    Cnn.ConnectionString = "Provider = SQLOLEDB;User ID= ;password=;Data Source=;Initial Catalog = "
    Cnn.Open
    Sql = "SELECT TOP 1 Pmax FROM dbo where ID=‘" & SID & "‘ order by hDateTime desc"
    
     获取记录集
    Set rt = Cnn.Execute(Sql)
   
     根据字段获取值
    result = CStr(rt("Pmax"))
    
    GETPMAX = result
    
    关闭记录集
    rt.Close
    
    关闭数据库链接,释放资源
    Cnn.Close

End Function

 

excel VBA动态链接数据库

标签:class   des   rom   exce   hda   bsp   VID   sql   OLE   

原文地址:https://www.cnblogs.com/youmeng/p/10788413.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!