Forum Discussion

Gerodo's avatar
Gerodo
Copper Contributor
Jan 17, 2025
Solved

VBA code to check if a user is in a table

A shout to VBA Experts, I am looking for some guidance. I am a novice Access/VBA user I am self taught through forums and U-Tube. I have made a defect record program in Access, I came across a piece...
  • Gerrit_Viehmann's avatar
    Jan 17, 2025
    1. Environ("USERNAME") is not reliable, since it can be edited by the user. CreateObject("WScript.Network").UserName is not as easy to spoof.
    2. Don't use "Like" when you test for equality. Use "=".
    3. DCount will give a result (0), when no entry is found and a number > 0 otherwise. In VBA "=" assignment will cast to the correct type most of the time, but being explicit is not wrong. Therefore you can write:
      Command27.Enabled = CBool(DCount("*", "DMUsers", "ActiveUser = True And UserName = '" & StrUserName & "'"))

Resources