Forum Discussion
Gerodo
Jan 17, 2025Copper Contributor
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...
- Jan 17, 2025
- Environ("USERNAME") is not reliable, since it can be edited by the user. CreateObject("WScript.Network").UserName is not as easy to spoof.
- Don't use "Like" when you test for equality. Use "=".
- 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 & "'"))
Gerrit_Viehmann
Jan 17, 2025Brass Contributor
- Environ("USERNAME") is not reliable, since it can be edited by the user. CreateObject("WScript.Network").UserName is not as easy to spoof.
- Don't use "Like" when you test for equality. Use "=".
- 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 & "'"))
- GerodoJan 19, 2025Copper Contributor
Thank you so much for that guidance. it worked perfectly. I had struggled with this for a while. You comments regarding Environ duly noted .