vba函数返回最近日期的记录。给出另一个标准

时间:2013-12-25 21:54:30

标签: vba ms-access access-vba ms-access-2010

我需要在ms access 2010表单中设置文本框的默认值。默认值必须是CommunicationTable中的最新日期,其中ClientNumber与表单中当前记录关联的ClientNumber相同。下面的代码引用了正确的ClientNumber,但我不确定如何获取最新日期。我担心DMax可能不适合获取最新日期。我应该如何更改以下内容以获取最新日期?

=DMax("DateOfCommunication","[CommunicationTable]","[ClientNumber]=  "  
& [Forms]![Main]![NavigationSubform].[Form]![ClientNumber] & "")  

我意识到我还应该发布嵌套上述函数的更大函数:

=DLookUp("[Level]","[CommunicationTable]","DateOfCommunication= "  
& DMax("DateOfCommunication","[CommunicationTable]","[ClientNumber]=  "  
& [Forms]![Main]![NavigationSubform].[Form]![ClientNumber] & ""))  

此外,表单本身也绑定到CommunicationTable。此VBA函数位于DefaultValue对话框中,我通过文本框的属性表进入该对话框。所以我不确定在这种情况下创建一个SQL查询是否可行。


编辑:

我上传了数据库的精简副本,该副本会重现问题 at this file sharing site

找到代码:

1.) Open the CommunicationEntryForm and  
2.) open the AfterUpdate() event procedure for the ClientNumber field.  

接下来,重现问题:

1.) close `CommunicationEntryForm`    
2.) In the Main form(which should already be open), click the View tab to open  
    the most recent CommunicationForm for any Client you want.  Note the Level  
    number for that Communication.  
3.) Click on the Communication tab.  This will leave the form and show a list  
    of CommunicationForms for that Client.  
4.) Click the Create New Form button.  This will open up CommunicationEntryForm.  
    The value for Level should be the same as the value you noted in step 1 above.  
    The problem is that this is blank.  

有人可以告诉我如何解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

我要做的是先通过以下方式获取日期:

Dim db as Database
Dim rec as Recordset

Set db = CurrentDB
Set rec = db.OpenRecordset("SELECT Top 1 DateOfCommunication FROM CommunicationTable WHERE ClientNumber= " & [Forms]![Main]![NavigationSubform].[Form]![ClientNumber] & " ORDER BY DateOfCommunication DESC")

这将获得最近的日期。然后,在您的上述VBA中,您可以将rec(0)固定在计算的位置:

Me.MyDateField = DLookUp("[Level]","[CommunicationTable]","DateOfCommunication= #" & rec(0) & "#")  

将“MyDateField”替换为日期字段的实际名称。

我很确定你需要英镑符号(或者孩子们今天称之为“主题标签”......),以便Access可以对日期值进行计算。

答案 1 :(得分:1)

@CodeMed - 我确实下载了数据库,但发现你遇到的问题不是你所描述的 - 例如当你“添加”新通信时,你只需覆盖现有记录。我设法得到你想要的结果,但它只是改变了3条记录。您的非样本程序是否实际上能够创建和添加新记录?实际上,我只是将现有代码更改为:

Private Sub cmdNewCommForm_Click()
Dim cNum As Long
Dim strSQL As String
Dim rs As Recordset
Dim db As Database
Set db = CurrentDb
cNum = Forms!Main!NavigationSubform.Form!ClientNumber
strSQL = "SELECT Top 1 Co.Level AS MaxOfLevel " & _
         "FROM CommunicationTable co Where Co.ClientNumber = " & cNum
Set rs = db.OpenRecordset(strSQL)
Forms!Main!NavigationSubform.Form!NavigationSubform.SourceObject = "CommunicationEntryForm"
Forms!Main!NavigationSubform.Form!NavigationSubform!ClientNumber = cNum
Forms!Main!NavigationSubform.Form!NavigationSubform!DateOfCommunication = Date
If rs.RecordCount > 0 Then
    Forms!Main!NavigationSubform.Form!NavigationSubform!Level = rs!MaxOfLevel
Else
    Forms!Main!NavigationSubform.Form!NavigationSubform!Level = 0
End If

Set rs = Nothing
Set db = Nothing

End Sub