How Do I use something like Environ(username) in VBA ?

W

WANNABE

I am trying to test a condition of a table value on form open, and the value
I need to test is the USERNAME. I used Environ(username) on the fields
Default property to set the value. But I'm not smart enough to figure out
all the intricate differences between what is needed in VBA and what is
needed in forms, YET!! Now the following is the code that I have started
with and there are 2 errors.

Private Sub Form_Open(Cancel As Integer)
Dim stDocName, stLinkCriteria, stUserId As String
Dim Test As Boolean
' stUserId = Environ(UserName)
' This is for testing because the previous line caused errors
stUserId = "myname"
' This line is now erroring out, something about cancelled previous
operation.
DLookup("UserID", "Emp", "UserID= stUserID ") = Test
If Test Then
stDocName = "frm_UserEntry"
Me.Visible = False
DoCmd.OpenForm stDocName, , acEdit, stLinkCriteria
Else
'do that
End If
End Sub
Any Help would be greatly appreciated! thank you!
 
G

Graham Mandeno

The only thing missing is a couple of quotes:

stUserId = Environ("UserName")

In fact, I think you will find that you also have those quotes where you
have used it for a DefaultValue property, otherwise it won't work.
 
W

WANNABE

Thank you, I just found this to work
stUserId = Environ$("Username")
But I don't fully understand the meaning of the $
Thanks again..

Graham Mandeno said:
The only thing missing is a couple of quotes:

stUserId = Environ("UserName")

In fact, I think you will find that you also have those quotes where you
have used it for a DefaultValue property, otherwise it won't work.
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand


WANNABE said:
I am trying to test a condition of a table value on form open, and the
value I need to test is the USERNAME. I used Environ(username) on the
fields Default property to set the value. But I'm not smart enough to
figure out all the intricate differences between what is needed in VBA and
what is needed in forms, YET!! Now the following is the code that I have
started with and there are 2 errors.

Private Sub Form_Open(Cancel As Integer)
Dim stDocName, stLinkCriteria, stUserId As String
Dim Test As Boolean
' stUserId = Environ(UserName)
' This is for testing because the previous line caused errors
stUserId = "myname"
' This line is now erroring out, something about cancelled previous
operation.
DLookup("UserID", "Emp", "UserID= stUserID ") = Test
If Test Then
stDocName = "frm_UserEntry"
Me.Visible = False
DoCmd.OpenForm stDocName, , acEdit, stLinkCriteria
Else
'do that
End If
End Sub
Any Help would be greatly appreciated! thank you!
 
G

Graham Mandeno

The $ is only to provide compatibility with earlier versions of Basic. It's
the old type-cast character to force the function to return a string.

I suggest you use the one without the $.
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

WANNABE said:
Thank you, I just found this to work
stUserId = Environ$("Username")
But I don't fully understand the meaning of the $
Thanks again..

Graham Mandeno said:
The only thing missing is a couple of quotes:

stUserId = Environ("UserName")

In fact, I think you will find that you also have those quotes where you
have used it for a DefaultValue property, otherwise it won't work.
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand


WANNABE said:
I am trying to test a condition of a table value on form open, and the
value I need to test is the USERNAME. I used Environ(username) on the
fields Default property to set the value. But I'm not smart enough to
figure out all the intricate differences between what is needed in VBA
and what is needed in forms, YET!! Now the following is the code that I
have started with and there are 2 errors.

Private Sub Form_Open(Cancel As Integer)
Dim stDocName, stLinkCriteria, stUserId As String
Dim Test As Boolean
' stUserId = Environ(UserName)
' This is for testing because the previous line caused errors
stUserId = "myname"
' This line is now erroring out, something about cancelled previous
operation.
DLookup("UserID", "Emp", "UserID= stUserID ") = Test
If Test Then
stDocName = "frm_UserEntry"
Me.Visible = False
DoCmd.OpenForm stDocName, , acEdit, stLinkCriteria
Else
'do that
End If
End Sub
Any Help would be greatly appreciated! thank you!
 
W

WANNABE

Thank you.

Graham Mandeno said:
The $ is only to provide compatibility with earlier versions of Basic.
It's the old type-cast character to force the function to return a string.

I suggest you use the one without the $.
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

WANNABE said:
Thank you, I just found this to work
stUserId = Environ$("Username")
But I don't fully understand the meaning of the $
Thanks again..

Graham Mandeno said:
The only thing missing is a couple of quotes:

stUserId = Environ("UserName")

In fact, I think you will find that you also have those quotes where you
have used it for a DefaultValue property, otherwise it won't work.
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand


"WANNABE" <breichenbach AT istate DOT com> wrote in message
I am trying to test a condition of a table value on form open, and the
value I need to test is the USERNAME. I used Environ(username) on the
fields Default property to set the value. But I'm not smart enough to
figure out all the intricate differences between what is needed in VBA
and what is needed in forms, YET!! Now the following is the code that I
have started with and there are 2 errors.

Private Sub Form_Open(Cancel As Integer)
Dim stDocName, stLinkCriteria, stUserId As String
Dim Test As Boolean
' stUserId = Environ(UserName)
' This is for testing because the previous line caused errors
stUserId = "myname"
' This line is now erroring out, something about cancelled previous
operation.
DLookup("UserID", "Emp", "UserID= stUserID ") = Test
If Test Then
stDocName = "frm_UserEntry"
Me.Visible = False
DoCmd.OpenForm stDocName, , acEdit, stLinkCriteria
Else
'do that
End If
End Sub
Any Help would be greatly appreciated! thank you!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top