使用DoCmd.TransferText网络权限将{csv文件导入ms访问权限

时间:2016-04-13 05:54:49

标签: vba import permissions

我有一个简单的vba脚本,可以将csv从网络共享导入到我的访问数据库中的新表中。我可以访问网络共享,它可以在我的机器上正常运行。我希望其他无法访问网络共享的人也能够执行该脚本。我有一个具有所需权限的通用网络帐户,我希望我可以将其嵌入到脚本中吗?

我已经看过这个以及其他论坛和MSDN并且无法找到如何做到这一点,大多数似乎都关注数据库和数据库连接权限。

三江源

Sub Import_data()

DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM Soil_moisture_data"
DoCmd.SetWarnings True
DoCmd.TransferText acImportDelim, , "Soil_moisture_data", "full network path and file", True
MsgBox "Soil moisture data has been updated"

End Sub

1 个答案:

答案 0 :(得分:0)

这是一个解决方案。它创建网络位置的本地映射网络驱动器,而不是导入数据,因为它将从本地驱动器中的csv文件导入;完成后,它将删除映射的驱动器。

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set wshNet = CreateObject("WScript.Network")

strUsername = Domain\username

strPassword = Password

strDestination = GetNextLetter("F", wshNet)

wshNet.MapNetworkDrive strDestination, "\\ServerAddress\Folder", , strUsername, strPassword

DoCmd.TransferText acImportDelim, , "Daily_data", strDestination & "\RestOfThePathIncludingFileName", True

wshNet.RemoveNetworkDrive strDestination, True

Set objFSO = Nothing
Set wshNetwork = Nothing

此解决方案是在https://www.experts-exchange.com/questions/21108808/using-vba-to-copy-file-to-a-network-share-with-username-and-password.html

之后完成的
Function GetNextLetter(DriveLetter, wshNet)

   'Start x at the ascii value of the drive letter to start the search
   'unless something is passed in. This sample uses capital letters and
   'starts at F.

   If IsEmpty(DriveLetter) Then

    x = 70

   Else

    x = Asc(DriveLetter)

   End If   

   Set oDrives = wshNet.EnumNetworkDrives
   'Step by two since the first item in the collection is the drive letter
   'and the second item is the network mapping

   For i = 0 To oDrives.Count - 1 Step 2

    If Chr(x) & ":" = oDrives.Item(i) Then

        x = x + 1

    End If

   Next   

   GetNextLetter = Chr(x) & ":"

End Function