My.Computer.Name导致运行时错误424

时间:2014-07-25 01:12:00

标签: vba ms-access-2013

需要在Access窗体上使用My.Computer.Name,但它会导致424运行时错误("需要对象")。我从Microsoft的Office Dev网站复制并粘贴了My.Computer.Name的示例代码,该代码生成相同的运行时错误。我也尝试了具有相同结果的My.Computer.FileSystem.DirectoryExists。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

查看http://answers.microsoft.com/en-us/office/forum/office_2007-access/how-can-i-get-computer-name-using-access-2007-vba/aa5471c7-f06c-e011-8dfc-68b599b31bf5?tab=MoreHelp

基本上,使用

'more simple but easier to "spoof"
Environ("computername")

'Should use as long as it works on your Access server
'Simply call "ComputerName()" whenever you need to get the user's computer
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, ByRef nSize As Long) As Long
Function ComputerName() As String
  Dim stBuff As String * 255
  Dim lAPIResult As Long
  Dim lBuffLen As Long
  lBuffLen = 255
  lAPIResult = GetComputerName(stBuff, lBuffLen)
  If lBuffLen > 0 Then
    ComputerName = Left(stBuff, lBuffLen)
  End If
End Function