如何使用OpenArgs将SQL参数传递给窗体



我有一个数据库(不是我自己构建的(,它使用3个单独的表单来完成一件事。

相反,我想将一个SQL字符串传递给OpenArgs,以便使用1表单。

我想使用的表单的原始代码:

Private Sub Form_Open(Cancel As Integer)
Dim strSQL As String
If Not IsNull(Me.OpenArgs) Then
strSQL = "SELECT tbl_COMBINED.[First Name] AS [Name Badge], 'P' AS Logo, Format(Now(),""yyyy"") &  STOCKHOLDERS MEETING' AS MEETING " _
& "FROM tbl_COMBINED " _
& "GROUP BY tbl_COMBINED.[First Name], 'P', Format(Now(),""yyyy"") & ' STOCKHOLDERS MEETING', " _
& "tbl_COMBINED.ACCOUNT, tbl_COMBINED.Came " _
& "HAVING tbl_COMBINED.ACCOUNT = '" & CStr(Me.OpenArgs) & "' " _
& "AND ((tbl_COMBINED.Came) Is Null Or (tbl_COMBINED.Came)) = 0"
Me.RecordSource = strSQL

End If
End Sub

使用调用其他每个表单

DoCmd.OpenForm "frm_newmanualnamebadge", "", "",, acNormal

来自Main窗体,并且在行源中具有SQL字符串。我想消除行源并使用1形式。我将每个按钮的字符串设置为:

strManuel = "SELECT tbl_manual_name_badge.NAMEBADGE1, tbl_manual_name_badge.MEETING, " _
& "tbl_manual_name_badge.LOGO, tbl_manual_name_badge.Stockerholder " _
& "FROM tbl_manual_name_badge"
DoCmd.OpenForm "frm_newmanualnamebadge", "", "",, acNormal, strManual

然而,将strManual作为SQL字符串传递给表单时,每次运行它时都会得到一个"#姓名"而不是输入的名称。

这是我在表格上使用的代码:

If Not IsNull(Me.OpenArgs) Then

strSQL = "SELECT tbl_COMBINED.[First Name] AS [Name Badge], 'P' AS Logo " _
& "FROM tbl_COMBINED " _
& "GROUP BY tbl_COMBINED.[First Name], 'P', " _
& "tbl_COMBINED.ACCOUNT, tbl_COMBINED.Came " _
& "HAVING tbl_COMBINED.ACCOUNT = '" & CStr(Me.OpenArgs) & "' " _
& "AND ((tbl_COMBINED.Came) Is Null Or (tbl_COMBINED.Came)) = 0"

Me.RecordSource = strSQL

ElseIf IsNull(Me.OpenArgs) Then
strSQL = "SELECT tbl_manual_name_badge.NAMEBADGE1, tbl_manual_name_badge.MEETING, " _
& "tbl_manual_name_badge.LOGO, tbl_manual_name_badge.Stockerholder " _
& "FROM tbl_manual_name_badge"

Me.RecordSource = strSQL

End If

好吧,要么传递一个值,要么传递整个sql字符串。

但是,如果您为表单传递完整的sql字符串,那么这毫无意义:

If Not IsNull(Me.OpenArgs) Then
strSQL = "SELECT tbl_COMBINED.[First Name] AS [Name Badge], 'P' AS Logo " _
& "FROM tbl_COMBINED " _
& "GROUP BY tbl_COMBINED.[First Name], 'P', " _
& "tbl_COMBINED.ACCOUNT, tbl_COMBINED.Came " _
& "HAVING tbl_COMBINED.ACCOUNT = '" & CStr(Me.OpenArgs) & "' " _
& "AND ((tbl_COMBINED.Came) Is Null Or (tbl_COMBINED.Came)) = 0"
Me.RecordSource = strSQL

我的意思是,OpenArgs是一个完整的sel字符串,我非常确信ACCOUNT=;一些巨大的sql字符串"永远不会起作用。

所以,你会想要这个:

Dim strSQL As String
If Not IsNull(Me.OpenArgs) Then
strSQL = me.OpenArgs
else
strSQL = "SELECT tbl_manual_name_badge.NAMEBADGE1, tbl_manual_name_badge.MEETING, " _
& "tbl_manual_name_badge.LOGO, tbl_manual_name_badge.Stockerholder " _
& "FROM tbl_manual_name_badge"

End If
Me.RecordSource = strSQL

所以,我们现在的逻辑是:

if passed sql string (openargs), then that becomes our sql
if no open arges, then use the defined sql we have in the on-load

相关内容

  • 没有找到相关文章

最新更新