• VBA 代码集


    1. 读取本机网卡地址

      Dim MyMac
        Set MyMac = GetObject("Winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
        For Each MyMacAddress In MyMac
          If MyMacAddress.IPEnabled = True Then
          MsgBox "本机网卡MAC地址是:" & MyMacAddress.MacAddress
        Exit For
        End If
        Next

    2.从数据集读取数据至Sheet

        Dim strDate As String, strEntity As String
        strDate = Format(Sheets("sheet1").[b2], "yyyy-MM-dd")  
        strEntity = Sheets("sheet1").[b3]
                
        Dim strSql As String    
        '查询语句,
         strSql = "select * from xxxx where ..."
            
         Sheets("sheet1").Range("A7:J65535").ClearContents '清除内容
            
         Dim ds1 As ADODB.Recordset
         Set ds1 = runSql(strSql)
         '根据查询语句获得数据,并填充至A7
          Worksheets("sheet10").Range("A7").CopyFromRecordset ds1
          Set ds1 = Nothing      

     3. 数据库连接及执行

    Option Explicit
    
    Public conn As ADODB.Connection
    Public Const strConn = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Password=ts123;Initial Catalog=SQLDB;Data Source=.;Connect Timeout=720; "
    
    
    Public Function runSql(strSql) As ADODB.Recordset
        Dim ds As ADODB.Recordset
         Set ds = New ADODB.Recordset
         OpenConn          
         ds.Open strSql, conn     
         Set runSql = ds
         CloseConn     
    End Function
    
    Public Function OpenConn()
        If conn Is Nothing Then
            Set conn = New ADODB.Connection
        End If
        If conn.State <> 1 Then
            conn.Open strConn
        End If
    End Function

    Public Function CloseConn() If conn.State = 1 Then Set conn = Nothing End If End Function

     4. 读取文件内容

    Dim txt As String, Result As String
           
    Result = ""
    Open "c:loc_conn.txt" For Input As #1 '
    Do While Not EOF(1)
      Line Input #1, txt  
      Result = Result & txt & Chr(13) & Chr(10)
    Loop
    Close #1
    MsgBox Result
  • 相关阅读:
    计算机基础 python基础内容 变量以及基础数据类型
    初识函数--生成器
    初识函数--迭代器
    初识函数
    初识小数据池和深浅拷贝
    初识基础数据类型 dict,set
    初识基础数据类型 list,tuple
    初识基础数据类型 int,str,bool
    初识变量
    自定义协议解决粘包问题,阿里云部署,udp协议套接字,socketserver模块的使用
  • 原文地址:https://www.cnblogs.com/jerron/p/5573406.html
Copyright © 2020-2023  润新知