ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

[µo°Ý] VBA SQL ¨ú±o®É¶¡¶¡¹j ªº¨ä¥LÄæ¦ì¤§¥­§¡­È

¦^´_ 10# GBKEE

ÁÂÁ¶Wª©
¥Î§A«ü¾Éªº¤èªk¤£¥Î ThisworkBook.fullname¡A¶]¤@¦¸¬O¬ù 11¬í§Ö¤F³\¦h

¥t¥~½Ð±Ð¡AExcel ¸ê®Æ®w­n·s¼W¤@Äæ¦ì¡A¥Î¤F Alter Table [CT$] add NewColumn char(10) ±o¨ì¹BºâµL®Äªº«H®§
ADO ¤£¤ä´© Alter TABLE ¶Ü?
¤£¾å±o°ÝÃD¦b­þ¸Ì¡A·q½Ð«ü¾É
  1. Sub ExcelAddColumn()
  2.     Dim SQL As String
  3.     Dim j%, r%
  4.     Dim cnn As ADODB.Connection
  5.     Dim rs As ADODB.Recordset
  6.     Dim ws As Worksheet
  7.    
  8.     Time0 = Timer
  9.     Set ws = ThisWorkbook.Sheets(1)
  10.     Set cnn = New ADODB.Connection
  11.    
  12.     With cnn
  13.             .Provider = "Microsoft.ACE.OLEDB.12.0"
  14.               .ConnectionString = "Extended Properties= Excel 12.0;" _
  15.                 & "Data Source=D:\VBA SQL.xlsm"
  16.         '«Øij¤£­n Source=" & ThisWorkbook.FullName ¦b³o¸ê®Æ®w¬¡­¶Ã¯¤¤°õ¦æ¦¹¥¨¶°,«Ü¯Ó°O¾ÐÅé
  17.         '**********************************************************
  18. '        '2003ª© ¤Þ¥Î¡Gmicrosoft activex data objects 2.x library
  19. '        .Provider = "microsoft.jet.oledb.4.0"
  20. '        .ConnectionString = "Extended Properties= Excel 8.0;" _
  21. '        & "Data Source=D:\VBA SQL.xls"
  22. '        '«Øij¤£­n Source=" & ThisWorkbook.FullName ¦b³o¸ê®Æ®w¬¡­¶Ã¯¤¤°õ¦æ¦¹¥¨¶°,«Ü¯Ó°O¾ÐÅé
  23.         .Open
  24.     End With
  25.    
  26.     SQL = "Alter Table [CT$] ADD NewColumn char(10)"   '­n¼W¥[¤@Äæ¦ì¦WºÙ
  27.    
  28. '' ¥X²{"µL®Äªº¹Bºâ" ¿ù»~
  29.     cnn.Execute SQL
  30.    
  31.     Set rs = New ADODB.Recordset
  32.    
  33.     SQL = "select * from [CT$] "
  34.     rs.Open SQL, cnn
  35.     Debug.Print rs.Fields.Count
  36.    
  37. End Sub
½Æ»s¥N½X

TOP

¦^´_ 8# GBKEE


    ¥H¤@­ÓSQL±Ô­z§¹¦¨»Ý¨D²×©ó¥i¥H¹F¦¨¦p¤U¡A¨Ñ°Ñ¦Ò¡F ÁÂÁÂ
  1. Sub ExcelSQL2()

  2.     Dim i&, j%
  3.     Dim cnn As ADODB.Connection
  4.     Dim rs As ADODB.Recordset
  5.     Dim SQL As String, FileName$
  6.     Dim ws1 As Worksheet
  7.     Dim ws2 As Worksheet
  8.     Dim TimeInterval#
  9.     Dim Time0#
  10.     Dim StartTime, EndTime, TimeStr$
  11.    
  12.     Set ws1 = Sheets("CT")
  13.     Set ws2 = Sheets("Temp")
  14.     ws2.Cells.Clear
  15.    
  16.     FileName = ThisWorkbook.FullName
  17.    
  18.     ws1.Select
  19. '    StartTime = #1/1/2015 12:00:01 AM#
  20. '    EndTime = #12/31/2030#
  21. '    StartTime = CDbl(StartTime)
  22. '    EndTime = CDbl(EndTime)
  23.     StartTime = "2015/01/01 00:00:01"
  24.    
  25.     TimeInterval = 10   '¶¡¹j 10 ¤ÀÄÁ
  26.    
  27. '    Time0 = Timer
  28.     TimeStr = " format( iif(MINUTE([¤é´Á®É¶¡]) mod " & TimeInterval & " = 0,[¤é´Á®É¶¡], " & _
  29.                 "DateAdd(""n"", " & TimeInterval & "-MINUTE([¤é´Á®É¶¡]) mod " & TimeInterval & _
  30.                 " ,[¤é´Á®É¶¡])),""yyyy/mm/dd hh:mm"") "
  31.                
  32. '=== ³B²z¼Æ­ÈÄæ¦ìªº¨C 10¤ÀÄÁ¥­§¡ ===========================
  33.     Set cnn = New ADODB.Connection
  34.         cnn.Provider = "Microsoft.ACE.OLEDB.12.0"
  35.         cnn.ConnectionString = "Data Source=" & FileName & ";Extended Properties=""Excel 12.0 xml;HDR=Yes;"""
  36.         cnn.Open
  37.    
  38.       SQL = "SELECT " + TimeStr + " as [DateTime], CTcode, " & _
  39.                 "AVG([Volts]) as [avgVolt], AVG([Hz]) as [avgHz] " & _
  40.         " From [" & ws1.Name & "$] " & _
  41.         " Where [¤é´Á®É¶¡] >= #" & StartTime & "# " & _
  42.         " Group by " + TimeStr + " ,CTcode " & _
  43.         " Order by " + TimeStr + " ,CTcode"
  44.         
  45.     Set rs = cnn.Execute(SQL)
  46.    
  47.     With ws2
  48.         For i = 0 To rs.Fields.Count - 1
  49.             .Cells(1, i + 1) = rs.Fields(i).Name
  50.         Next
  51.         .Cells(2, 1).CopyFromRecordset rs
  52.     End With
  53.       
  54.     rs.Close
  55.     cnn.Close
  56.     Set rs = Nothing
  57.     Set cnn = Nothing
  58. '    Debug.Print Timer - Time0
  59. End Sub
½Æ»s¥N½X

TOP

        ÀR«ä¦Û¦b : ¦Y­W¤F­W¡B­WºÉ¤Ü¨Ó¡A¨ÉºÖ¤FºÖ¡BºÖºÉ´d¨Ó¡C
ªð¦^¦Cªí ¤W¤@¥DÃD