尝试使用Visual Basic将Word中的comboBox连接到Access中的数据库 [英] Trying to connect a comboBox in word to a database in Access using Visual Basic

查看:166
本文介绍了尝试使用Visual Basic将Word中的comboBox连接到Access中的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去6个小时,我一直在为此工作,但我遇到了一个令人沮丧的错误.现在,我想在我的空白Word文档中添加一个组合框,并用在Access中创建的表中的字段中的数据填充它.我正在使用Visual Studio 2015作为我的IDE和Word 2013文档模板.

I've been working on this for the past 6 hours and I'm getting a frustrating error. For now, I want to add a combo box into my blank word document and populate it with a data from a field in a table I created in Access. I'm using Visual Studio 2015 as my IDE and the Word 2013 Document Template.

错误

ERROR

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.

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

My Database Table 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收到了此错误.

From some advice I read, I changed by 'Target CPU': from any PC to x86. However, this time I received this error from Word upon starting up.

推荐答案

已经有一段时间没有实际应用了,但是,如果我的记忆正确的话,这应该可以解决:

Haven't been praticing for a while but, if my memories are correct, this should work :

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的最佳方法.

I'm not able to test it, this may require a bit of tweaking... but RowSource have always been the best way (as far as I know) to feed ListBox/ComboBox with DB rows.

这支持许多来源,例如ADO& amp; DAO记录集,数组...

This supports a lot of sources like ADO & DAO Recordsets, Arrays...

这篇关于尝试使用Visual Basic将Word中的comboBox连接到Access中的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆