VB Code stuck in Loop

dubmunkey

Registered User.
Local time
Today, 23:13
Joined
Jan 6, 2006
Messages
62
Hi all,

i have a form which has a button giving access to a restricted area. In the past to restrict users from entering it ive used the Environ Function in VB and it has always worked fine.

now i need to restrict everyone but three users so used the same code and amended it but it is not working:

The code:

If UserID <> "g.spicer" Then GoTo notvaliduser

takes you to an error message which kicks you out.

but despite being logged in as g.spicer it is still taking me to the notvaliduser and kicking me out.

So i tested it by adding this code:

If UserID = "g.spicer" Then GoTo greg (where greg is an error message: "user logged in as"&(UserId)"

and tells me i am in fact logged on as g.spicer.

Therefore when i remove that line of code why does it recognise me as g.spicer but take me to the notvaliduser anyway?

any ideas?

this works on my other forms and ive tried redoing the code and compacting and repairing but it is still not working.

The code in full:

Private Sub confnotes_Click()
On Error GoTo Err_confnotes_Click
Dim UserID As String
Dim stDocName As String
Dim stLinkCriteria As String


'get computer ans user details

UserID = Environ("USERNAME")

DoCmd.SetWarnings False
DoCmd.SetWarnings True

If UserID <> "g.spicer" Then GoTo notvaliduser


notvaliduser:
Call MsgBox("ACCESS DENIED. Only Senior Members of Teaching staff have access.", vbCritical, "Invalid User")



stDocName = "Confidential"

stLinkCriteria = "[StudentID]=" & "'" & Me![StudentID] & "'"
DoCmd.OpenForm stDocName, , , stLinkCriteria, , acDialog

Exit_confnotes_Click:
Exit Sub

Err_confnotes_Click:
MsgBox Err.Description
Resume Exit_confnotes_Click

End Sub
 
dubmunkey said:
Private Sub confnotes_Click()
On Error GoTo Err_confnotes_Click
Dim UserID As String
Dim stDocName As String
Dim stLinkCriteria As String


'get computer ans user details

UserID = Environ("USERNAME")

DoCmd.SetWarnings False
DoCmd.SetWarnings True

If UserID <> "g.spicer" Then GoTo notvaliduser


notvaliduser:
Call MsgBox("ACCESS DENIED. Only Senior Members of Teaching staff have access.", vbCritical, "Invalid User")



stDocName = "Confidential"

stLinkCriteria = "[StudentID]=" & "'" & Me![StudentID] & "'"
DoCmd.OpenForm stDocName, , , stLinkCriteria, , acDialog

Exit_confnotes_Click:
Exit Sub

Err_confnotes_Click:
MsgBox Err.Description
Resume Exit_confnotes_Click

End Sub

Your missing an End if after the If which is causing an error which is being trapped. It's best to comment out any error trapping whilst testing code to see what the error are first.

Hope this helps. TS

Try:

Code:
Private Sub confnotes_Click()

On Error GoTo Err_confnotes_Click
    Dim UserID As String
    Dim stDocName As String
    Dim stLinkCriteria As String


 'get computer ans user details

 UserID = Environ("USERNAME")
If UserID <> "g.spicer" Then 

Call MsgBox("ACCESS DENIED.  Only Senior Members of Teaching staff have access.", vbCritical, "Invalid User")

Else

stDocName = "Confidential"
stLinkCriteria = "[StudentID]=" & "'" & Me![StudentID] & "'"
DoCmd.OpenForm stDocName, , , stLinkCriteria, , acDialog

End If

Exit Sub

Err_confnotes_Click:
MsgBox Err.Description

End Sub
 
what an idiot i am? :eek: cant believ i forgot to put the end if...else statement...sometimes cant see the wood for the trees...

you are a star!!

greg
 
No worries. :)

In general it is best not to use Goto statements unless error trapping as it can turn code into spaghetti. Your
Code:
UserID <> "g.spicer"
is a logic test. The fact that it may not be g.spicer isn't actually an error and therefore it's better dealt with by a logical test such as an if else end if construct rather than
Code:
GoTo notvaliduser

TS
 
cool, i have looked at the code and will change it so it works on the if...else format instead...makes sense- less room for error there...

cheers

greg
 
cool, i think i will change the code so it's using the else...if statements instead- makes sense...less room for error there

cheers

greg
 

Users who are viewing this thread

Back
Top Bottom