2022年8月10日 星期三

VBA+MYSEL SELECT 應用:查詢建立以及查詢結果導入工作表中(快速語法教學)

 自己寫給自己看的mysql查詢,也留作紀錄。

1.先設定引用項目:
圖1.
microsoft activex data object 6.1 library
microsoft ado ex.2.8 for ddl and security
(不小心全打成小寫xd👀)

2.mysql環境掌握:

圖2.mysql環境
keyword:root、localhost、3306
圖3.schemas 
說明:查詢要連線哪一個資料庫,以及查那一頁。
筆者要查資料庫"twi_securities_lending",跟工作表的"securities_lending_borrowing" 這一頁。

3.開始寫查詢sub

MYSEL 查詢主要是透過SELECT *FROM 語法查詢

關鍵語法:

   strSql = "Select  " & "*" & " FROM " & "securities_lending_borrowing" & " where" & " date='2021/08/09'

記得查詢的條件要前後加上"'" 的部分:
主要語法: "Select  " & "*" & " FROM " & "securities_lending_borrowing" & " where" 
前後都要有"符號歐,因為是文字一定要加。

Function 查詢()

On Error GoTo line1                                                                                        

   Dim Conn As New ADODB.Connection

    Dim rs As New Recordset


'設定查詢資料庫、ip、port、使用者跟密碼

  Conn.Open "Driver=_

{MySQL ODBC 8.0 UNICODE Driver};Server=127.0.0.1:3306;_

Database=twi_securities_lending;User=root;Password=_

xxxx;Option=3;"  

 Conn.CursorLocation = adUseClient

  Dim Catalog As New ADOX.Catalog

  Set Catalog.ActiveConnection = Conn

  Dim Table As ADOX.Table, Column As ADOX.Column

   strSql = "Select  " & "*" & " FROM " & "securities_lending_borrowing" & " where" & " date='2021/08/09'"

rs.Open strSql, Conn, adOpenStatic                                    

                                      s1 = rs.RecordCount                                      

                                      If s1 = 0 Then

                                          查詢= 0                                       

                                           Exit Function

                                      End If

                                         RS_ARRAY = rs.GetRows(s1)     

                   rs.Close                   

                   查詢 = RS_ARRAY

  Exit Function

line1:

   Debug.Print Err.Description

  Debug.Print Err.NUMBER

       MsgBox Err.Description  

End Function

Function 查詢()
On Error GoTo line1
Dim Conn As New ADODB.Connection
Dim rs As New Recordset
'設定查詢資料庫、ip、port、使用者跟密碼
Conn.Open "Driver={MySQL ODBC 8.0 UNICODE Driver};Server=127.0.0.1:3306;Database=twi_securities_lending;User=root;Password=XXXX;Option=3;"
Conn.CursorLocation = adUseClient
Dim Catalog As New ADOX.Catalog
Set Catalog.ActiveConnection = Conn
Dim Table As ADOX.Table, Column As ADOX.Column
strSql = "Select " & "*" & " FROM " & "securities_lending_borrowing" & " where" & " date='2021/08/09'"
rs.Open strSql, Conn, adOpenStatic
s1 = rs.RecordCount
If s1 = 0 Then
查詢 = 0
Exit Function
End If
RS_ARRAY = rs.GetRows(s1)
rs.Close
查詢 = RS_ARRAY
Exit Function
line1:
Debug.Print Err.Description
Debug.Print Err.NUMBER
MsgBox Err.Description
End Function
view raw gistfile1.txt hosted with ❤ by GitHub

4.怎在工作表中直接導入查詢結果

先貼上查詢sub的code,以及設定好後。

作一個activex命令按鈕,貼上以下內容

Private Sub CommandButton1_Click()

        a = 查詢    

        Sheets("工作表5").Range("A1:H" & UBound(a, 2) + 1) = WorksheetFunction.Transpose(a)        

End Sub

圖5.大功告成

常有的問題:如忘了"'"符號 XD、MYSQL PORT、TABLE NAME設定錯誤等等。


沒有留言:

張貼留言

指數變化(2025.03.28)

 指數變化(2025.03.28) 上周焦點: 美國消費者信心指數 3/25 92.9 美國耐久財訂單月增率 3/26 1.4 PCE 月增 0.4 年增 2.8 (不多阿,最高還有5.5ㄝ)   本周愛看: 美國非農業就業人數變化 美國ISM製造業採購經理人指數 美國芝加哥FE...