Data Validation

ewong1

Eric Wong
Local time
Today, 02:53
Joined
Dec 4, 2004
Messages
96
I am attempting to use the following code to validate whether or not a user has logged into the database before. It pulls the users windows log in username and checks to see if it is in the tblUsers file. I haven't placed anything in the results of the condition because I haven't acheived a successful if then else statement yet. Can someone let me know if they see an error in this code? Thanks!

Code:
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function fOSUserName() As String
    'Returns the network login name
    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 = ""
    End If
End Function

Public Function IsNothing(ByVal varValueToTest) As Integer
'-----------------------------------------------------------
'   Function Purpose: Tests for results that have no value
'       Null = Nothing
'       Empty = Nothing
'       Number = 0 is Nothing
'       String = "" is Nothing
'       Date/Time is never nothing
'   Inputs: A value to test for logical "nothing"
'   Outputs: True = value passed is a logical "nothing", False = value passed has valid data
'   Created By: JLV 01/31/95
'   Last Revised: JLV 01/31/95
'-----------------------------------------------------------

    On Error GoTo IsNothing_Err
    IsNothing = True
    
    Select Case VarType(varValueToTest)
        Case 0  'Empty
            GoTo IsNothing_Exit
        Case 1  'Null
            GoTo IsNothing_Exit
        Case 2, 3, 4, 5, 6 ' Integer, Long, Single, Double, Currency
            If varValuetToTest <> 0 Then IsNothing = False
        Case 7 ' Date/Time
            IsNothing = False
        Case 8 ' String
            If (Len(varValueToTest) <> 0 And varValueToTest <> " ") Then IsNothing = False
    End Select

IsNothing_Exit:
    On Error GoTo 0
    Exit Function

IsNothing_Err:
    IsNothing = True
    Resume IsNothing_Exit
            
End Function


Private Sub Form_Load()
    Dim strUser As String
    Dim strCondition As String
    Dim Result As Integer
    Dim Validation As String
        
    strUser = fOSUserName()
    strCondition = "[idUser] = " & strUser
    
    If IsNothing(DLookup("[idUser]", "tblUsers", strCondition)) Then
        MsgBox "Nothing", vbOKOnly, "Test"
        Result = acDataErrContinue
    Else
        MsgBox "Data Present", vbOKOnly, "Test"
        Result = acDataErrAdded
    End If
    
End Sub
 
I see what you are saying, except for the fact that the [idUser] field in reality is not a number, it is a PK text field. So in my Condition:

strCondition = "[idUser] = " & strUser

This can also be interpreted as:

strCondition = "txtUser = " & strUser

Make any sense?
 
it would be the following if it were text:

strCondition = "[idUser]='" & strUser & "'"
 
ewong1 said:
Make any sense?
Makes perfect sense, but is still wrong. As modest pointed out, and as detailed in the link I posted, text values must be surrounded by single quotes.
 

Users who are viewing this thread

Back
Top Bottom