自己寫給自己看的mysql查詢,也留作紀錄。
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 |
4.怎在工作表中直接導入查詢結果
先貼上查詢sub的code,以及設定好後。
作一個activex命令按鈕,貼上以下內容
Private Sub CommandButton1_Click()
a = 查詢
Sheets("工作表5").Range("A1:H" & UBound(a, 2) + 1) = WorksheetFunction.Transpose(a)
End Sub
沒有留言:
張貼留言