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

 

posted on 2016-06-10 11:09  jerron  阅读(273)  评论(0编辑  收藏  举报

导航