根据记录集中的数据打开表单

时间:2016-03-13 21:19:10

标签: vba ms-access access-vba

我有一个应用程序可以搜索 SQL Server 数据库上的客户信息。

有三种形式:

  1. 第一个允许用户根据几个不同的标准查找信息,例如帐号,姓氏电话号码等。
  2. 第二个表单正确显示第一个表单中的信息。
  3. 我想基于第二个表单上的结果集中的值打开第三个表单,该字段名为Master_ID

    我创建了一个全局变量,在单击Master_ID字段时,会在第二个表单上存储Master_ID字段的内容。

    然后我想在包含以下代码的按钮上执行on click事件:

    Private Sub tranHisBtn_Click()
    
    
    
        Dim transferSQL As String
    
        transferSQL = "SELECT dbo_Master_Accounts.Master_ID, dbo_Master_Accounts.FirstName, dbo_Master_Accounts.LastName, dbo_Transaction_Table.Date_of_Transaction, Format([dbo_Transaction_Table]![Time_of_Transaction],""hh:nn:ss ampm"") AS TranTime, dbo_Transaction_Table.Sku, dbo_Transaction_Table.Description, Right([dbo_Transaction_Table]![Description],6) AS tranAccnt, [dbo_Transaction_Table]![ArAmt]*-1 AS Amnt, dbo_Master_Accounts.Master_ID " & vbCrLf
        transferSQL = transferSQL + "FROM dbo_Master_Accounts INNER JOIN dbo_Transaction_Table ON dbo_Master_Accounts.Master_ID = dbo_Transaction_Table.Account_Number " & vbCrLf
        transferSQL = transferSQL + "WHERE (((dbo_Transaction_Table.Description) Like ""%Transfer To%"") AND ((dbo_Master_Accounts.Master_ID)=" & Chr$(34) & GBL_Master_Id & Chr$(34) & ")) " & vbCrLf
        transferSQL = transferSQL + "ORDER BY dbo_Transaction_Table.Date_of_Transaction, Format([dbo_Transaction_Table]![Time_of_Transaction],""hh:nn:ss ampm"");"
    
        Dim trancon As ADODB.Connection
        Set trancon = CurrentProject.Connection
        Dim tranRs As New ADODB.Recordset
        tranRs.ActiveConnection = trancon
        tranRs.CursorType = adOpenStatic
        tranRs.Open transferSQL
    
        'DoCmd.OpenForm "TransferbyNumFM" ', , , "Master_Id = 'transRs.fields(0)'"
    
        MsgBox "good"
    
    End Sub
    

    我无法弄清楚如何设置第三种形式的记录来源。一切都运行良好,直到我尝试打开第三个表格。

2 个答案:

答案 0 :(得分:1)

正如我所说:使用DoCmd打开表单,并将第3个表单的OnOpen事件中Form对象的记录源设置为您拥有的SQL 字符串。也不需要创建记录集。并且永远不要尝试将记录源作为DoCmd.OpenForm

中的过滤器传递

哦,我的错误:因为你有一个全局变量(即在模块中声明的公共变量),你甚至不需要将它作为OpenArgs传递,你只需在Button Click上执行以下操作

 DoCmd.OpenForm "TransferbyNumFm"

然后,在Form3的类模块中执行

Option Compare Database
Option Explicit

Private Sub Form_Open(Cancel As Integer)

    Dim transferSQL As String

    transferSQL = _
        "SELECT dbo_Master_Accounts.Master_ID, " & _
            "dbo_Master_Accounts.FirstName, " & _
            "dbo_Master_Accounts.LastName, " & _
            "dbo_Transaction_Table.Date_of_Transaction, " & _
            "Format([dbo_Transaction_Table]![Time_of_Transaction],'hh:nn:ss ampm') AS TranTime, " & _
            "dbo_Transaction_Table.Sku, dbo_Transaction_Table.Description, " & _
            "Right([dbo_Transaction_Table]![Description],6) AS tranAccnt, " & _
            "[dbo_Transaction_Table]![ArAmt]*-1 AS Amnt " & _
        "FROM dbo_Master_Accounts " & _
            "INNER JOIN dbo_Transaction_Table ON dbo_Master_Accounts.Master_ID = dbo_Transaction_Table.Account_Number " & _
        "WHERE(((dbo_Transaction_Table.Sku) Like '%Transfer%')) " & _
        "ORDER BY dbo_Transaction_Table.Date_of_Transaction"

    Me.RecordSource = transferSQL

End Sub

Form3的Recordsource可以在此之前取消分配,但是你需要它来添加字段(但你可以在以后删除它)

答案 1 :(得分:0)

这就是我编译的内容,并打开表单但不填充数据:

 Dim transferSQL As String

   transferSQL = "SELECT dbo_Master_Accounts.Master_ID, "
   transferSQL = transferSQL + "dbo_Master_Accounts.FirstName, "
   transferSQL = transferSQL + "dbo_Master_Accounts.LastName, "
   transferSQL = transferSQL + "dbo_Transaction_Table.Date_of_Transaction, "
   transferSQL = transferSQL + "Format([dbo_Transaction_Table]![Time_of_Transaction],'hh:nn:ss ampm') "
   transferSQL = transferSQL + "AS TranTime, dbo_Transaction_Table.Sku, dbo_Transaction_Table.Description, "
   transferSQL = transferSQL + "Right([dbo_Transaction_Table]![Description],6) AS tranAccnt, "
   transferSQL = transferSQL + "[dbo_Transaction_Table]![ArAmt]*-1 AS Amnt "
   transferSQL = transferSQL + "FROM dbo_Master_Accounts INNER JOIN dbo_Transaction_Table ON dbo_Master_Accounts.Master_ID = dbo_Transaction_Table.Account_Number "
   transferSQL = transferSQL + "WHERE(((dbo_Transaction_Table.Sku) Like '%Transfer%')) "
   transferSQL = transferSQL + "ORDER BY dbo_Transaction_Table.Date_of_Transaction"


    Dim trancon As ADODB.Connection
    Set trancon = CurrentProject.Connection
    Dim tranRs As New ADODB.Recordset
    tranRs.ActiveConnection = trancon
    tranRs.CursorType = adOpenStatic
    tranRs.Open transferSQL


    DoCmd.OpenForm "TransferbyNumFm", , transferSQL, _
       wherecondition:="Master_ID=" & (GBL_Master_Id)

GBL_Mast_ID有正确的数据,所以我认为我遇到了DoCmd的问题,我不明白。感谢。

JPL