If find record in the table, enable a button

S

Song Su

I have a table called USERS and a field called USERID

When people click my Page:Student tab, I want to check if the network userid
is found in my users table. If found, enable cmdStudent button. Otherwise,
cmdStudent remain disabled.

Can anyone help?
 
S

Song Su

Thanks for the idea. For following code, I got 'type mismatch' error
message.

Private Sub Form_Open(CANCEL As Integer)
If Not DLookup("[userid]", "users", "[userid] = fOSUserName()") Then
cmdStudent.Enabled = True
Else
cmdStudent.Enabled = False
End If
End Sub

where did I do wrong?

fOSUserName() is the function in my module:

Function fOSUserName() As String
' Returns the network login name. This is used on frmMainForm
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If (lngX > 0) Then
fOSUserName = left$(strUserName, lngLen - 1)
Else
fOSUserName = vbNullString
End If
End Function
 
R

ruralguy via AccessMonster.com

You need to adjust for a string.
If Not DLookup("[userid]", "users", "[userid] = " & Chr(34) & fOSUserName() &
Chr(34) & ") Then

Song said:
Thanks for the idea. For following code, I got 'type mismatch' error
message.

Private Sub Form_Open(CANCEL As Integer)
If Not DLookup("[userid]", "users", "[userid] = fOSUserName()") Then
cmdStudent.Enabled = True
Else
cmdStudent.Enabled = False
End If
End Sub

where did I do wrong?

fOSUserName() is the function in my module:

Function fOSUserName() As String
' Returns the network login name. This is used on frmMainForm
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If (lngX > 0) Then
fOSUserName = left$(strUserName, lngLen - 1)
Else
fOSUserName = vbNullString
End If
End Function
Have you looked at the DLookup() or DCount() functions?
[quoted text clipped - 6 lines]
 
S

Song Su

I think I copied right:
Private Sub Form_Open(CANCEL As Integer)
If Not DLookup("[userid]", "users", "[userid] = " & Chr(34) &
fOSUserName() & Chr(34) & ") Then
cmdStudent.Enabled = True
Else
cmdStudent.Enabled = False
End If
End Sub

but it gave me 'syntax error'. any idea?

ruralguy via AccessMonster.com said:
You need to adjust for a string.
If Not DLookup("[userid]", "users", "[userid] = " & Chr(34) &
fOSUserName() &
Chr(34) & ") Then

Song said:
Thanks for the idea. For following code, I got 'type mismatch' error
message.

Private Sub Form_Open(CANCEL As Integer)
If Not DLookup("[userid]", "users", "[userid] = fOSUserName()") Then
cmdStudent.Enabled = True
Else
cmdStudent.Enabled = False
End If
End Sub

where did I do wrong?

fOSUserName() is the function in my module:

Function fOSUserName() As String
' Returns the network login name. This is used on frmMainForm
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If (lngX > 0) Then
fOSUserName = left$(strUserName, lngLen - 1)
Else
fOSUserName = vbNullString
End If
End Function
Have you looked at the DLookup() or DCount() functions?
[quoted text clipped - 6 lines]
Can anyone help?
 
R

ruralguy via AccessMonster.com

I'm thinking you don't need the final & " so...

If Not DLookup("[userid]", "users", "[userid] = " & Chr(34) &
fOSUserName() & Chr(34) ) Then


Song said:
I think I copied right:
Private Sub Form_Open(CANCEL As Integer)
If Not DLookup("[userid]", "users", "[userid] = " & Chr(34) &
fOSUserName() & Chr(34) & ") Then
cmdStudent.Enabled = True
Else
cmdStudent.Enabled = False
End If
End Sub

but it gave me 'syntax error'. any idea?
You need to adjust for a string.
If Not DLookup("[userid]", "users", "[userid] = " & Chr(34) &
[quoted text clipped - 35 lines]
 
S

Song Su

When I compile this, no error.
When I type ? fOSUserName() and hit enter in immediate window, it gave me
Song (correct)
So, I enter 1 record in my users table and userid as Su
When I run following code by open the form, command button not enable
(correct)

However, when I change the record from Su to Song and run the code, it gave
me 'type mismatch'.

I use fOSUserName() to get network userid and check against 'userid' field
in table 'users'. if found, enable command button. otherwise, stay
unenabled.

please help.

ruralguy via AccessMonster.com said:
I'm thinking you don't need the final & " so...

If Not DLookup("[userid]", "users", "[userid] = " & Chr(34) &
fOSUserName() & Chr(34) ) Then


Song said:
I think I copied right:
Private Sub Form_Open(CANCEL As Integer)
If Not DLookup("[userid]", "users", "[userid] = " & Chr(34) &
fOSUserName() & Chr(34) & ") Then
cmdStudent.Enabled = True
Else
cmdStudent.Enabled = False
End If
End Sub

but it gave me 'syntax error'. any idea?
You need to adjust for a string.
If Not DLookup("[userid]", "users", "[userid] = " & Chr(34) &
[quoted text clipped - 35 lines]
Can anyone help?
 
R

ruralguy via AccessMonster.com

DLookup() returns your requested field or a Null so try:

Private Sub Form_Open(CANCEL As Integer)
If IsNull(DLookup("[userid]", "users", "[userid] = " & Chr(34) & fOSUserName()
&
Chr(34) )) Then
cmdStudent.Enabled = False
Else
cmdStudent.Enabled = True
End If

End Sub


Song said:
When I compile this, no error.
When I type ? fOSUserName() and hit enter in immediate window, it gave me
Song (correct)
So, I enter 1 record in my users table and userid as Su
When I run following code by open the form, command button not enable
(correct)

However, when I change the record from Su to Song and run the code, it gave
me 'type mismatch'.

I use fOSUserName() to get network userid and check against 'userid' field
in table 'users'. if found, enable command button. otherwise, stay
unenabled.

please help.
I'm thinking you don't need the final & " so...
[quoted text clipped - 18 lines]
 
S

Song Su

Thank you! Thank you! Thank you!
It really works!

I did learn something today.

ruralguy via AccessMonster.com said:
DLookup() returns your requested field or a Null so try:

Private Sub Form_Open(CANCEL As Integer)
If IsNull(DLookup("[userid]", "users", "[userid] = " & Chr(34) &
fOSUserName()
&
Chr(34) )) Then
cmdStudent.Enabled = False
Else
cmdStudent.Enabled = True
End If

End Sub


Song said:
When I compile this, no error.
When I type ? fOSUserName() and hit enter in immediate window, it gave me
Song (correct)
So, I enter 1 record in my users table and userid as Su
When I run following code by open the form, command button not enable
(correct)

However, when I change the record from Su to Song and run the code, it
gave
me 'type mismatch'.

I use fOSUserName() to get network userid and check against 'userid' field
in table 'users'. if found, enable command button. otherwise, stay
unenabled.

please help.
I'm thinking you don't need the final & " so...
[quoted text clipped - 18 lines]
Can anyone help?
 
R

ruralguy via AccessMonster.com

That's great Song! Thanks for posting back with your success.

Song said:
Thank you! Thank you! Thank you!
It really works!

I did learn something today.
DLookup() returns your requested field or a Null so try:
[quoted text clipped - 32 lines]
 
Top