Syntax for "Not" in VBA

CharlesWh

Registered User.
Local time
Today, 16:20
Joined
Jan 19, 2006
Messages
36
Hi all, I'm using this bit of code on my splash form to ensure that the Db can only be used on computers which I have authorised it for.

'Checks the computer being used is authorised to be able to use this database
Dim sHostName As String
sHostName = Environ$("computername")

If sHostName <> "ComputerName" or "AnotherName" Then DoCmd.Quit

What I am struggling with is the syntax for NOT. in other words if its not the above then database quits.
 
I think the error is because you are using an Or without two fully qualified conditional statements. You can try ...

Code:
If sHostName <> "ComputerName" Or sHostName <> "AnotherName" Then DoCmd.Quit

-dK
 
If sHostName <> "ComputerName" or sHostName <> "AnotherName" Then DoCmd.Quit

if you get too many ors, use a case instead - easier to read

----------
it would be nice to have

if shostname not in ("comp1","comp2") etc

but you cant use in, in that way unfortunately
 
Am I correct in the <> is the syntax for "if it's not this"

Thnks for the replies, will test and post back
 
Have tested with the correct computer names but the database still quits?

If sHostName <> "ComputerName" or sHostName <> "AnotherName" Then DoCmd.Quit
 
If sHostname is equal to "Computername" then it will not be equal to "AnotherName" so the condition always evaluates to True and so you always quit

The expression needs to be something like this
Code:
If sHostName = "ComputerName" or  sHostName = "AnotherName" Then
 
else
 DoCmd.Quit
end if
I know its a bit messy but I havent had time to tidy it up.
 
silly me - should be and not or

If sHostName <> "ComputerName" AND sHostName <> "AnotherName" Then DoCmd.Quit
 
Both of those cause the Db to close. I dont think the <> is the right thing for NOT is it?
 
If sHostName <> "ComputerName" AND sHostName <> "AnotherName" Then DoCmd.Quit

of couse it is - it depends on what you want to do

you are saying in this

if shostname is not equal to "computername" and also
shostname is not equal to "anothername" then quit

try this to see what is going on!

Code:
If sHostName <> "ComputerName" AND sHostName <> "AnotherName" Then 
  msgbox("Quitting: Shostname is " & shostname)
  DoCmd.Quit
end if
 
I think my error may be down to the point at which the code executes?
 

Users who are viewing this thread

Back
Top Bottom