我正在使用Access 2010数据库,我无法弄清楚如何在单个常量字符串中存储函数调用的多个参数。
例如,我们想要打开一个包含以下内容的表单:
DoCmd.OpenForm "someformname",acNormal,,"ID=50",acFormEdit, acWindowNormal,"OPENARGS"
我可以将所有这些存储在全局常量中,例如
Public Const C_FORMOPENEDIT as String
然后打开这样的表单?
DoCmd.OpenForm C_FORMOPENEDIT
它似乎不能以这种方式工作。如何让VBA认识到这是一系列以逗号分隔的参数?或者这不可能吗?
注意:我已经尝试通过参数名称(例如FormName :=
)调用它,我也已经验证了字符串中的任何引号(如果需要)都会通过。 (比如""")
答案 0 :(得分:1)
你想做的事情是不可能的。您的常量将被评估为好像它是表单的名称。由于它是恒定的而不是动态的,我建议创建单独的程序,例如
Public Sub FormOpenEdit(sFormName As String)
DoCmd.OpenForm sFormName, acNormal, , "ID=50", acFormEdit, acWindowNormal, "OPENARGS"
End Sub
Public Sub FormOpenAdd(sFormName As String)
DoCmd.OpenForm sFormName, acNormal, , "ID=50", acFormAdd, acWindowNormal, "OPENARGS"
End Sub
或者你可以构建一个Enum来选择模式,这样你就可以只使用一个子
答案 1 :(得分:0)
您可以使用以下代码。我用它来从功能区菜单打开表单 - xml只能存储字符串。带有参数的字符串应略微修改,如注释中所述。 如果您使用带有默认参数的DoCmd.OpenForm并且表单默认视图是数据表,它仍会以单个表单的形式打开。该功能还包括一段代码,可以解决这个问题。
' non-constant arguments should be able to evaluated by Eval function
' Replace quotes (") by "{" and "}"
' Example of arguments:
' "acNormal, , {[CODE]='DO022666'}, acFormEdit, GetEditFormWindowMode()"
Public Sub MyOpenForm(strFrm As String, strFrmArgs As String)
Dim frm As Form
Dim iDefView As Integer
Dim astrArgs() As String
Dim avArgs(6) As Variant
Dim i As Integer
Dim arg As Variant
Dim bArg0Defined As Boolean
'default args
avArgs(0) = acNormal
avArgs(3) = acFormEdit
avArgs(4) = acWindowNormal
astrArgs = Split(strFrmArgs, ",")
For i = 0 To UBound(astrArgs)
If i > 5 Then Exit For
arg = EvalFormArgument(astrArgs(i))
If Not IsNull(arg) Then
avArgs(i) = arg
End If
Next i
'If View argument ommited, check for DefaultView=Datasheet form property and open form in Datasheet view
bArg0Defined = False
If UBound(astrArgs) > 0 Then
If Len(Trim(astrArgs(0))) > 0 Then
bArg0Defined = True
End If
End If
If Not bArg0Defined Then
'Determine form properties
DoCmd.OpenForm strFrm, acDesign, , , , acHidden
Set frm = Forms(strFrm)
iDefView = frm.DefaultView
DoCmd.Close acForm, strFrm
Set frm = Nothing
If iDefView = 2 Then 'Datasheet view
avArgs(0) = acFormDS
End If
End If
DoCmd.OpenForm strFrm, avArgs(0), avArgs(1), avArgs(2), avArgs(3), avArgs(4), avArgs(5)
End Sub
Public Function EvalFormArgument(A As String) As Variant
Dim ret As Variant
A = Trim(A)
If A = "" Then
EvalFormArgument = Null
Exit Function
End If
Select Case A
Case "acNormal": ret = 0
Case "acDesign": ret = 1
Case "acFormDS": ret = 3
Case "acFormPivotChart": ret = 5
Case "acFormPivotTable": ret = 4
Case "acLayout": ret = 6
Case "acPreview": ret = 2
Case "acFormAdd": ret = 0
Case "acFormEdit": ret = 1
Case "acFormPropertySettings": ret = -1
Case "acFormReadOnly": ret = 2
Case "acDialog": ret = 3
Case "acHidden": ret = 1
Case "acIcon": ret = 2
Case "acWindowNormal": ret = 0
Case Else
A = Replace(A, "{", """")
A = Replace(A, "}", """")
ret = Eval(A)
End Select
EvalFormArgument = ret
End Function