Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access 如何在Access 2010中调用函数?_Ms Access_Vba - Fatal编程技术网

Ms access 如何在Access 2010中调用函数?

Ms access 如何在Access 2010中调用函数?,ms-access,vba,Ms Access,Vba,我知道这个问题已经被问了一遍又一遍,但我无法理解我找到的任何指南 我对Access和编写VBA完全是初学者,所以我找到了一些代码,可以帮助我在Access中将许多文件导入到单独的表中 我已经尝试了几种不同的方法,将代码放入宏或按钮中并进行调用,但都没有成功 代码可能有问题,但我知道的还不够清楚。我也很确定我在尝试调用函数时犯了其他错误。请帮帮我 代码如下: Option Compare Database Option Explicit Function DoImport() Dim strP

我知道这个问题已经被问了一遍又一遍,但我无法理解我找到的任何指南

我对Access和编写VBA完全是初学者,所以我找到了一些代码,可以帮助我在Access中将许多文件导入到单独的表中

我已经尝试了几种不同的方法,将代码放入宏或按钮中并进行调用,但都没有成功

代码可能有问题,但我知道的还不够清楚。我也很确定我在尝试调用函数时犯了其他错误。请帮帮我

代码如下:

Option Compare Database
Option Explicit

Function DoImport()

Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean

' Change this next line to True if the first row in EXCEL worksheet
' has field names
blnHasFieldNames = True

' Replace C:\Documents\ with the real path to the folder that
' contains the EXCEL files
strPath = "C:\Documents and Settings\user\Desktop\folder"

' Replace tablename with the real name of the table into which
' the data are to be imported
strTable = "tablename"

strFile = Dir(strPath & "*.xls")
Do While Len(strFile) > 0
   strPathFile = strPath & strFile
   strTable = Left(strFile, Len(strFile) - 4)
   DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
         strTable, strPathFile, blnHasFieldNames

' Uncomment out the next code step if you want to delete the
' EXCEL file after it's been imported
'       Kill strPathFile

   strFile = Dir()
Loop

End Function

要调用此过程,它需要以按钮所在的当前形式存在,或者驻留在自己的模块中

由于函数不返回值或没有任何参数可调用,因此您可以在按钮的单击事件中键入以下VBA代码:

DoImport
如果希望确保代码实际正在运行,可以在可执行代码行上按F9设置断点

或者在要调试的位置键入单词Stop

除非按照代码注释的建议对文本字符串进行更改,否则代码本身将不会非常有用

代码本身的可重用性不强,因此下一步应该使用参数进行研究,以便在运行时调用函数时,可以提供文件夹名和表名等


代码本身将搜索特定文件夹中的Excel文件,并尝试将每个文件导入Microsoft Access,使用文件名作为表名。

创建一个新模块并将下面的代码粘贴到其中如何。保存它,并根据需要命名它

Public Function DoImport(strPath AS String, strTable AS String, _
                         blnHasFieldNames AS Boolean, RemoveFile AS Boolean)
  Dim strFile As String

  strFile = Dir(strPath & "*.xls")
  Do While Len(strFile) > 0
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, strTable, _
                              strPath & strFile, blnHasFieldNames
    if RemoveFile Then Kill strPath & strFile
    strFile = Dir()
  Loop
End Function
然后,您可以通过以下方式调用该函数:

DoImport "C:\imports\Excel\", "MyTableName", True, True

这允许您传递路径、表名、文件是否包含字段名以及导入后是否要删除文件。这样,您就不必不断更改函数的代码。

此代码显然是从网站复制而来的。你在哪一行看到错误?你有没有在上面提到的网站上查过你的参考资料,以确保你启用了正确的库?哦,是的,我确实复制了它。一旦将其放入子模块中,函数和结束函数上就会弹出错误。如果子模块/结束子模块在其中,并且我取出函数部分,则不会出现错误,但它不起作用。它告诉我Sub未定义。在VBA中,过程可以是函数或子例程。子例程中不能有函数。或函数中的子例程。通常,您只会在希望返回值的情况下创建一个函数,这样代码示例中的子例程或sub就可以suffice@Mark3308,感谢您提供有关SUB与函数的信息。我有一本傻瓜书,说真的,我对这本书很陌生,但里面没有关于这方面的东西。我猜如果你是一个哑巴,你不应该尝试VBA!我试试这个。非常感谢。你所描述的代码正是我需要它做的。而且,我应该只需要做一次这个巨大的导入,所以重用代码对我来说并不是那么重要。不过,你一开始的解释确实帮助我解释了一些事情。谢谢你!我要试着弄清楚这件事。