0
因此,我對編碼非常陌生,我的工作讓我深入到一個excel項目中,希望能夠得到一些幫助。在Excel中使用輸入字段在Access中查找和檢索數據
我們目前擁有訪問數據庫,其中包含特定交易所上市證券的歷史價格。我想知道是否可以使用VBA從Excel中提取選擇輸入的歷史價格。到目前爲止,我有這個代碼 - 子getDataFromAccess()
Dim DBFullName As String
Dim Connect As String, Source As String
Dim Connection As ADODB.Connection
Dim Recordset As ADODB.Recordset
Dim Col As Integer
Dim Symbol As String
' Database Path Info
DBFullName = "O:\ProjectX\ProjectX.accdb"
' Open the Connection
Set Connection = New ADODB.Connection
Connect = "Provider=Microsoft.ACE.OLEDB.12.0;"
Connect = Connect & "Data Source=" & DBFullName & ";"
Connection.Open ConnectionString:=Connect
' pull first symbol input from worksheet
Symbol = ActiveSheet.Range("A1").Value
' Create RecordSet
Set Recordset = New ADODB.Recordset
With Recordset
' Filter Data
Source = "SELECT * FROM HistoricalData WHERE [SYMBOL] = 'HYD'"
' Source = "SELECT * FROM Customers WHERE [Job Title] = 'Owner' "
.Open Source:=Source, ActiveConnection:=Connection
' MsgBox "The Query:" & vbNewLine & vbNewLine & Source
' Write field names
For Col = 0 To Recordset.Fields.Count - 1
Range("B1").Offset(0, Col).Value = Recordset.Fields(Col).Name
Next
' Write recordset
Range("B1").Offset(1, 0).CopyFromRecordset Recordset
End With
ActiveSheet.Columns.AutoFit
Set Recordset = Nothing
Connection.Close
Set Connection = Nothing
End Sub
正如你所看到的,它拉的HYD的數據,但我無法弄清楚如何把它取值,無論是從形式還是細胞。我曾嘗試
Source = "SELECT * FROM HistoricalData WHERE [SYMBOL] = SYMBOL"
和
Source = "SELECT * FROM HistoricalData WHERE [SYMBOL] = ActiveSheet.Range("A1)"