Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2009
    Posts
    22

    Unanswered: passing field name to function

    I've tried this a number of ways based on other threads I've seen but can't get it to work. I'd like to create a function that accepts a field name and then checks if it is true or false (data type is boolean).

    I'd like to call a function and pass in the name of the field and have the function check it's value, display a message if false, and then pass back whether the field was true or false. For instance....

    private function checkField(sField name as string) as boolean

    if sField = False then
    msgbox "Error - please try a different form"
    checkField = false
    else
    checkField = true
    end if

    end function

    Any help would be appreciated - Thanks!

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    The column is boolean right?

    Try googling DLOOKUP() in the first instance - that will get you started. We can help with some "tweaking".
    Note that DLOOKUP is inefficient, so if you plan to call this loads we probably need more info for a better solution.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  3. #3
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Actually, just out of curiosity - could you post some of the names of the columns you plan to check with this please?
    Testimonial:
    pootle flump
    ur codings are working excelent.

  4. #4
    Join Date
    Feb 2009
    Posts
    22
    Yes. The columns are all boolean.
    Field names are sessionID_t1, sessionID_t2, sessionID_t3, sessionID_ta1, etc...

    dLookup actually works great when I hard code in the parameters. I'm having trouble with syntax though. Here is what I've got:
    -------------------------------------------------------
    Private Function checkValue(sFieldName As String) As Boolean

    checkValue = DLookup("sessionID_" & sFieldName, "tblSession", "session_ID = " & ReturnSessionID())

    ---------------------------------------------------------
    The call is for example checkValue("t1")

    This gives me the error:
    Run-Time error '2001':
    You canceled the previous operation.

    note: ReturnSessionID() returns a long integer.
    Last edited by eric2125; 05-28-09 at 18:18.

  5. #5
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    I feared that. Please read this thread - let me know if the design sounds familiar.
    http://www.dbforums.com/database-con...-form-not.html
    Testimonial:
    pootle flump
    ur codings are working excelent.

  6. #6
    Join Date
    Feb 2009
    Posts
    22
    I do see some similarities there but to be honest I don't completely understand everything in that post. I think you are suggesting that the design I have used is flawed, perhaps? I guess I am just looking for a quick fix here. Overall the database works very well and we are going live soon - I am just trying to eliminate some repetitive code.

  7. #7
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    I found this in my archives....

    You may be able to expand upon/modify it to fit your needs.

    'Note: Passes FLDName to function (the FieldName). Returns boolean (False if no records exist, True if records do exist)
    Function GetACodeFOE(FLDName As Variant, RID As Integer) As Boolean
    Dim rv As ADODB.Recordset
    Dim strSQX As String
    Set rv = New ADODB.Recordset
    strSQX = "Select * from ChecksFOE where [" & FLDName & "] = " & RID & ""
    rv.Open strSQX, CurrentProject.Connection, adOpenKeyset, adLockReadOnly
    If rv.EOF And rv.BOF Then
    rv.Close
    Set rv = Nothing
    GetACodeFOE = False
    Else
    rv.Close
    Set rv = Nothing
    GetACodeFOE = True
    Exit Function
    End If
    End Function
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  8. #8
    Join Date
    Feb 2009
    Posts
    22
    Thanks for the response - I was able to find my error and now it works great. Thanks for the help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •