简体   繁体   English

可以在 vba 中的公共常量上使用 dlookup 吗?

[英]it is possible to use a dlookup on a public const in vba?

I have about 10 forms in my access and in all of them I need to put some basic information like, date and user, to do that I use the same code on all forms, I'm searching a form to set this information and just call我有大约 10 个表单,我需要在所有表单中输入一些基本信息,例如日期和用户,为此我在所有表单上使用相同的代码,我正在搜索一个表单来设置此信息,只是称呼

vba now现在的vba

Private sub Form_Load()

Dim userId As String
userId = Environ("Username")

Dim uSer As String
uSer = DLookup("User", "Adm_User", "UserId='" & Environ$("Username") & "'")

Dim dtNow as string
dtNow = Format(Now, "dd/mm/yyyy hh:mm")

me.text_userId.Caption = userId
me.text_uSer.Caption = uSer
me.text_dtNow.Caption = dtNow

vba that i want我想要的vba

Public Const userId As String = Environ("Username")
Public Const uSer As Variant = DLookup("User", "Adm_User", "UserId='" & Environ$("Username") & "'")
Public Const dtNow As String = Format(Now, "dd/mm/yyyy hh:mm")

Private sub Form_Load()

me.text_userId.Caption = userId
me.text_uSer.Caption = uSer
me.text_dtNow.Caption = dtNow

The simple method is to keep a set of functions in a "Common" module:简单的方法是在“通用”模块中保留一组函数:

Public Function GetUserId() As String

    Dim UserId  As String

    UserId = Environ("Username")

    GetUserId = UserId

End Function


Public Function GetUser() As String

    Static User As String

    If User = "" Then 
        User = Nz(DLookup("User", "Adm_User", "UserId = '" & GetUserId & "'"))
    End If

    GetUser = User

End Function


Public Function FormatNow() As String

    Dim Text    As String

    Text = Format(Now, "dd/mm/yyyy hh:nn")

    FormatNow = Text

End Function

Then call these at the form load:然后在表单加载时调用它们:

Private Sub Form_Load()

    Me!text_userId.Caption = GetUserId
    Me!text_user.Caption = GetUser
    Me!text_dtNow.Caption = FormatNow

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM