1. Read the local network card address
DimMymacSetMymac =GetObject("winmgmts:"). InstancesOf ("Win32_NetworkAdapterConfiguration") for eachMymacaddressinchMymacIfmymacaddress.ipenabled =True Then MsgBox "the MAC address of the native NIC is:"&mymacaddress.macaddressExit for End If Next
2. Reading data from a data set to sheet
DimStrdate as String, strentity as Stringstrdate=Format(Sheets ("Sheet1"). [B2],"YYYY-MM-DD") strentity= Sheets ("Sheet1"). [B3]DimstrSQL as String 'query statements,strSQL ="SELECT * from xxxx where ..."Sheets ("Sheet1"). Range ("a7:j65535"). ClearContents ' Erase contentDimDs1 asADODB. RecordsetSetDS1 =runSql (strSQL)'get data based on query statements and populate to A7Worksheets ("sheet10"). Range ("A7"). CopyFromRecordset DS1SetDS1 = Nothing
3. Database connection and execution
Option Explicit PublicConn asADODB. Connection Public Conststrconn ="Provider=SQLOLEDB.1; Persist Security info=true; User Id=sa; Password=ts123;initial Catalog=sqldb;data source=.; Connect timeout=720; " Public FunctionRUNSQL (strSQL) asADODB. RecordsetDimDs asADODB. RecordsetSetDS =NewADODB. Recordset openconn ds. Open strSQL, ConnSetRUNSQL =DS CloseconnEnd Function Public FunctionOpenconn ()IfConn is Nothing Then Setconn =NewADODB. ConnectionEnd If IfConn. State <>1 ThenConn. Open strconnEnd IfEnd Function
Public FunctionCloseconn ()IfConn. State =1 Then Setconn = Nothing End IfEnd Function
VBA Code Set