Ms access 函数不返回屏幕分辨率值
函数工作并获得屏幕分辨率。但当我试着回到sub时,fun_x和fun_y都是空的。为什么?Ms access 函数不返回屏幕分辨率值,ms-access,vba,Ms Access,Vba,函数工作并获得屏幕分辨率。但当我试着回到sub时,fun_x和fun_y都是空的。为什么? Private Sub Form_Open(Cancel As Integer) Dim sub_x As Long, sub_y As Long ScreenRes fun_x:=sub_x, fun_y:=sub_y Debug.Print sub_x, sub_y, fun_x, fun_y End Sub 模块名称:MOD_GET_RES Option Compare Database
Private Sub Form_Open(Cancel As Integer)
Dim sub_x As Long, sub_y As Long
ScreenRes fun_x:=sub_x, fun_y:=sub_y
Debug.Print sub_x, sub_y, fun_x, fun_y
End Sub
模块名称:MOD_GET_RES
Option Compare Database
Declare Function GetSystemMetrics32 Lib "User32" _
Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
Function ScreenRes(ByVal fun_x As Long, ByVal fun_y As Long)
fun_x = GetSystemMetrics32(0) ' width in points
fun_y = GetSystemMetrics32(1) ' height in points
End Function
您已经传递了函数参数
ByVal
(ue),因此当它返回时,只修改一个副本。隐式或不隐式使用ByRef
(参照)使其工作:
Function ScreenRes(fun_x As Long, fun_y As Long)
您还可以将GetSystemMetrics32
(为什么要使用更长的别名?)设为私有
编辑:下面是一个小例子的更多解释
Option Explicit
Sub CantChange(ByVal a As Integer, ByVal b As Integer)
'When this gets called, a copy of the original variables
'is pushed on the stack.
a = a * 2 'The copy is altered.
b = b + 1
'When leaving this Sub, the copy is discarded.
End Sub
'exactly the same Sub procedure... except for the (implicit) ByRef
Sub Change(a As Integer, b As Integer)
'When this gets called, references
'(i.e. the location of the original variable)
'are pushed on the stack.
a = a * 2 'The original variable is altered.
b = b + 1
End Sub
Sub Test()
Dim a As Integer, b As Integer
a = 1
b = 5
Debug.Print "before:", a, b
CantChange a, b
Debug.Print "unchanged:", a, b
Change a, b
Debug.Print "changed:", a, b
End Sub
您已经传递了函数参数
ByVal
(ue),因此当它返回时,只修改一个副本。隐式或不隐式使用ByRef
(参照)使其工作:
Function ScreenRes(fun_x As Long, fun_y As Long)
您还可以将GetSystemMetrics32
(为什么要使用更长的别名?)设为私有
编辑:下面是一个小例子的更多解释
Option Explicit
Sub CantChange(ByVal a As Integer, ByVal b As Integer)
'When this gets called, a copy of the original variables
'is pushed on the stack.
a = a * 2 'The copy is altered.
b = b + 1
'When leaving this Sub, the copy is discarded.
End Sub
'exactly the same Sub procedure... except for the (implicit) ByRef
Sub Change(a As Integer, b As Integer)
'When this gets called, references
'(i.e. the location of the original variable)
'are pushed on the stack.
a = a * 2 'The original variable is altered.
b = b + 1
End Sub
Sub Test()
Dim a As Integer, b As Integer
a = 1
b = 5
Debug.Print "before:", a, b
CantChange a, b
Debug.Print "unchanged:", a, b
Change a, b
Debug.Print "changed:", a, b
End Sub
这起作用了,但我不明白为什么。我想我需要做一些关于差异的阅读。我添加了一个简单的例子,我希望现在更清楚;)谢谢你花时间把这些整理好。很有助于理解为什么,而不是如何。这是有效的,但我不明白为什么。我想我需要做一些关于差异的阅读。我添加了一个简单的例子,我希望现在更清楚;)谢谢你花时间把这些整理好。非常有助于理解为什么而不是如何。