热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

允许Access用户选择Excel工作表进行链接-AllowAccessusertoselectExcelworksheetforlinking

SoIamusingVBAinAccesstocreatelinkedtablesbetweenExcelandAccess.Simpleenoughandass

So I am using VBA in Access to create linked tables between Excel and Access. Simple enough and as some online resources guided me I decided to utilize the TransferSpreadsheet command. So I ran some code to test out if I had the syntax correct and got this

所以我在Access中使用VBA在Excel和Access之间创建链接表。很简单,当一些在线资源引导我时,我决定使用TransferSpreadsheet命令。所以我运行了一些代码来测试我的语法是否正确并得到了这个

DoCmd.TransferSpreadsheet acLink, acSpreadsheetTypeExcel12, _
        "Link Name", "File Path", True, "Sheet Name!"

So that worked perfectly, but I wanted to automate it so someone who doesn't understand how to code can use the function. So for the file path I set up a file dialog box to come up so the user can select the excel file. Again worked great.

所以这很好用,但我想自动化它,所以不懂代码的人可以使用这个功能。因此,对于文件路径,我设置了一个文件对话框,以便用户可以选择excel文件。再次工作得很好。

So now to the point, I want to create a dialog box for users to select the excel sheet to link as well. So essentially the user would select the excel file first and then select from a drop down box the sheet they want to link. Is this possible? If so how would I go about doing it. Here is my code so far:

所以到现在为止,我想创建一个对话框供用户选择要链接的Excel工作表。因此,用户基本上首先选择excel文件,然后从下拉框中选择他们想要链接的工作表。这可能吗?如果是这样,我将如何去做呢。这是我到目前为止的代码:

Public Sub linksheet()

Dim fd As FileDialog
Dim strpath As String
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.AllowMultiSelect = False
fd.Title = "Select Routing File"



'get the number of the button chosen
Dim FileChosen As Integer

FileChosen = fd.Show

If FileChosen <> -1 Then

Else

strpath = fd.SelectedItems(1)
DoCmd.TransferSpreadsheet acLink, acSpreadsheetTypeExcel12, _
        "Test Link", strpath, True, "Current!"

End If


End Sub

To add to this further I was attempting to utilize this code I found to get the names but I'm unsure how to store them as variables to use.

为了进一步增加这一点,我试图利用我发现的代码来获取名称,但我不确定如何将它们存储为要使用的变量。

Public Function WorkSheetNames(strwspath As String) As Boolean
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim strarray(25)

Set xlApp = CreateObject("Excel.application")
Set xlBook = xlApp.Workbooks.Open(strwspath, 0, True)
For Each xlSheet In xlBook.Worksheets
Debug.Print xlSheet.Name

Next xlSheet
xlBook.Close False
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
Set xlSheet = Nothing
End Function

What the above code does is list out each sheet name into the debug window, I just am finding it difficult to push those values to an array.

上面的代码所做的是将每个工作表名称列入调试窗口,我发现很难将这些值推送到数组中。

1 个解决方案

#1


2  

I don't see why you need to store the sheet names in an array. You could store them as a list in a string variable. Then you could assign that string as the RowSource property of a combo box which allows the user to select one of the available sheets.

我不明白为什么你需要将工作表名称存储在一个数组中。您可以将它们存储为字符串变量中的列表。然后,您可以将该字符串指定为组合框的RowSource属性,该属性允许用户选择一个可用的工作表。

Dim strSheets As String
' adapt your existing code to use this ...
For Each xlSheet In xlBook.Worksheets
    'Debug.Print xlSheet.Name
    strSheets = strSheets & ";" & xlSheet.Name
Next xlSheet
' discard leading semi-colon
strSheets = Mid(strSheets, 2)

After you have collected the sheet names, apply them as the combo box source.

收集工作表名称后,将它们应用为组合框源。

' ComboName should have Value List as Row Source Type
Me.ComboName.RowSource = strSheets 

推荐阅读
author-avatar
哒哒愛嘬萌
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有