[英]Accessing Rows from Access Database in Visual Basic 2010 based on Combobox Selection
[英]Trying to connect a comboBox in word to a database in Access using Visual Basic
在過去的6個小時中,我一直在進行此工作,但遇到了令人沮喪的錯誤。 現在,我想在我的空白Word文檔中添加一個組合框,並用我在Access中創建的表中的字段中的數據填充它。 我將Visual Studio 2015用作IDE和Word 2013文檔模板。
錯誤
An exception of type 'System.Runtime.InteropServices.COMException' occurred in WordInvoice.dll but was not handled in user code Additional information: Provider cannot be found. It may not be properly installed.
我的數據庫表 ID Employee Amount 1 Danny $100.00 2 Andy $200.00 3 Dixon $50.00 4 James $250.00
我的密碼
Imports System.Diagnostics
Imports Microsoft.Office.Interop.Word
Public Class ThisDocument
Private Sub ThisDocument_Startup() Handles Me.Startup
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
cnn.Open("Provider=Microsoft.Jet.OLEDB.4.0;" &
"Data Source=C:\Users\Danny\Documents\Employee Records1.accdb")
rst.Open("SELECT Employee FROM Payroll;", cnn, ADODB.CursorTypeEnum.adOpenStatic)
rst.MoveFirst()
Dim ccList As ContentControl
ccList = ContentControls.Add(Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlComboBox)
ccList.Title = "Select a Employee"
ccList.SetPlaceholderText(,, "Please Select a Employee")
Do While Not rst.EOF
ccList.DropdownListEntries.Add(rst.Fields(0).ToString)
rst.MoveNext()
Loop
rst.Close()
End Sub
Private Sub ThisDocument_Shutdown() Handles Me.Shutdown
End Sub
End Class
我嘗試過的
根據我閱讀的一些建議,我將“目標CPU”更改為:從any PC
更改為x86
。 但是,這次我在啟動時從Word收到了此錯誤。
一段時間沒做過,但是,如果我的記憶正確的話,這應該可以工作:
Public Class ThisDocument
Private Sub ThisDocument_Startup() Handles Me.Startup
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
cnn.Open("Provider=Microsoft.Jet.OLEDB.4.0;" &
"Data Source=C:\Users\Danny\Documents\Employee Records1.accdb")
rst.Open("SELECT Employee FROM Payroll;", cnn, ADODB.CursorTypeEnum.adOpenStatic)
rst.MoveFirst()
Dim ccList As ContentControl
ccList = ContentControls.Add(Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlComboBox)
ccList.Title = "Select a Employee"
ccList.SetPlaceholderText(,, "Please Select a Employee")
Set ccList.RowSource=rst
End Sub
...
我無法對其進行測試,這可能需要進行一些調整...但是,就我所知,RowSource一直是用DB行填充ListBox / ComboBox的最佳方法。
這支持許多源,例如ADO&DAO記錄集,數組...
您需要安裝x64 Microsoft Access數據庫引擎2010 Redistributable,並將連接字符串更改為Provider = Microsoft.ACE.OLEDB.12.0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.