Detecting if a field is required

Discussion in 'ASP General' started by Lorenzo Bolognini, Sep 28, 2003.

  1. Hi all,

    i need to detect whether a field is required or not. I'm using this code for
    building a string to convert later to an array (by Split) of which each
    element matches the field index (ex. Field(myArray(0)):

    For Each Field in rst.Fields
    If Field.Attributes And adFldIsNullable Then
    Required = 0 'field is NOT required
    Else
    Required = 1 'field is required
    End If

    strFieldRequired = strFieldRequired & Required & " "
    Next

    I'm running the above code against a table which has 4 fields in this exact
    order:
    - a PK
    - 2 required text fields
    - 1 NOT required text field

    And i'm getting this result when printing strFieldRequired:
    1 0 0 0

    So what's the problem with the code above? I've got some inspiration from
    the script at this URL:
    http://www.asptechniques.com/content.asp?a=co&cID=910

    Thank You,
    Lorenzo
     
    Lorenzo Bolognini, Sep 28, 2003
    #1
    1. Advertising

  2. Make sure you are using OLEDB to connect to your database; adFldIsNullable
    is not exposed to the standard ODBC/JET drivers. To see an OLEDB connection
    string, see http://www.aspfaq.com/2126 and make sure you have a recent
    installation of MDAC.





    "Lorenzo Bolognini" <> wrote in message
    news:5LEdb.134437$...
    > Hi all,
    >
    > i need to detect whether a field is required or not. I'm using this code

    for
    > building a string to convert later to an array (by Split) of which each
    > element matches the field index (ex. Field(myArray(0)):
    >
    > For Each Field in rst.Fields
    > If Field.Attributes And adFldIsNullable Then
    > Required = 0 'field is NOT required
    > Else
    > Required = 1 'field is required
    > End If
    >
    > strFieldRequired = strFieldRequired & Required & " "
    > Next
    >
    > I'm running the above code against a table which has 4 fields in this

    exact
    > order:
    > - a PK
    > - 2 required text fields
    > - 1 NOT required text field
    >
    > And i'm getting this result when printing strFieldRequired:
    > 1 0 0 0
    >
    > So what's the problem with the code above? I've got some inspiration from
    > the script at this URL:
    > http://www.asptechniques.com/content.asp?a=co&cID=910
    >
    > Thank You,
    > Lorenzo
    >
    >
     
    Aaron Bertrand [MVP], Sep 28, 2003
    #2
    1. Advertising

  3. "Aaron Bertrand [MVP]" <> ha scritto nel messaggio
    news:...
    > Make sure you are using OLEDB to connect to your database; adFldIsNullable
    > is not exposed to the standard ODBC/JET drivers. To see an OLEDB

    connection
    > string, see http://www.aspfaq.com/2126 and make sure you have a recent
    > installation of MDAC.


    The following is my connection string and i'm using ADO 2.8 on a Win2k with
    ALL the latest patches:

    cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & Server.Mappath("\xxxy\datasource\xyz.mdb") & _
    ";Persist Security Info=False"

    I'm noticing that someone else has had the same problem always with Access
    as you may see if you look for this MsgID: uGwIzjTjBHA.2024@tkmsftngp04 on
    microsoft.public.data.ado

    Thank You,
    Lorenzo

    P.S I live by aspfaq.com and i couldn't stress enough how much i'm thankfull
    to you!
     
    Lorenzo Bolognini, Sep 28, 2003
    #3
  4. Lorenzo Bolognini

    Bob Barrows Guest

    When I think back to the dozens of Access applications I've created, and the
    single ASP application I've created that used Access, I can safely say that
    I've never onec had to worry about whether a field was "Required" or not.
    The reason: I never created a nullable field. And even when i was using a
    database created by someone else, I did not care: I always put a default
    value into all fields. Instead of wasting devlopment and processing time
    with code code that tests the nullability of the fields in your table, start
    with the assumption that they are all required.

    Having said that, I'm wondering if you realize that an Access Text field can
    be created with the Required checkbox unchecked, but the "Allow zero-length
    string" checkbox unchecked, a contidtion which will not be covered in the
    Attributes property since it's a Jet-specific property.

    Bob Barrows

    Lorenzo Bolognini wrote:
    > "Aaron Bertrand [MVP]" <> ha scritto nel
    > messaggio news:...
    >> Make sure you are using OLEDB to connect to your database;
    >> adFldIsNullable is not exposed to the standard ODBC/JET drivers. To
    >> see an OLEDB connection string, see http://www.aspfaq.com/2126 and
    >> make sure you have a recent installation of MDAC.

    >
    > The following is my connection string and i'm using ADO 2.8 on a
    > Win2k with ALL the latest patches:
    >
    > cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    > "Data Source=" & Server.Mappath("\xxxy\datasource\xyz.mdb") &
    > _ ";Persist Security Info=False"
    >
    > I'm noticing that someone else has had the same problem always with
    > Access as you may see if you look for this MsgID:
    > uGwIzjTjBHA.2024@tkmsftngp04 on microsoft.public.data.ado
    >
     
    Bob Barrows, Sep 28, 2003
    #4
  5. "Bob Barrows" <> ha scritto nel messaggio
    news:...

    > Having said that, I'm wondering if you realize that an Access Text field

    can
    > be created with the Required checkbox unchecked, but the "Allow

    zero-length
    > string" checkbox unchecked, a contidtion which will not be covered in the
    > Attributes property since it's a Jet-specific property.


    Thank You very much but that would be a workaround and i think i cannot rely
    on that since i'm building a function for:
    - displaying all rows in a table and allowing inserts, edit and delete of
    records in that table
    - building a for with ready-made javascript (on the client side) and server
    controls based on the field attributes

    Making it the way u say may work for MY db but not on someone elses which i
    may not be allowed to modify if i want to reuse my function.

    Anyway thank you very much for pointing that out.

    Lorenzo
     
    Lorenzo Bolognini, Sep 28, 2003
    #5
  6. "Aaron Bertrand [MVP]" <> ha scritto nel messaggio
    news:...
    > Make sure you are using OLEDB to connect to your database; adFldIsNullable
    > is not exposed to the standard ODBC/JET drivers. To see an OLEDB

    connection
    > string, see http://www.aspfaq.com/2126 and make sure you have a recent
    > installation of MDAC.


    I've finally made it this way:

    rst.Open("tblAuthor"), cnn, adOpenDynamic, adLockOptimistic,
    adCmdTableDirect

    For Each Field in rst.Fields
    If Field.Attributes And adFldIsNullable Then
    Required = 0 'nullable
    Else
    Required = 1 'not nullable
    End If

    strFieldRequired = strFieldRequired & Required & " "
    Next

    I've played only with the ADO CommandTypeEnum i don't know if chainging the
    cursors may affect in any way the results but THIS way it works!! ;-)
    adCmdTableDirect is is the only CommandTypeEnum which returns the right
    results

    Think this could be placed on the aspfaq.com

    Lorenzo
     
    Lorenzo Bolognini, Sep 28, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Just D.
    Replies:
    0
    Views:
    467
    Just D.
    Mar 10, 2006
  2. Dale J. Kovacs

    Detecting Blank form field

    Dale J. Kovacs, Sep 10, 2003, in forum: ASP General
    Replies:
    0
    Views:
    134
    Dale J. Kovacs
    Sep 10, 2003
  3. Lorenzo Bolognini

    Detecting MEMO field

    Lorenzo Bolognini, Sep 21, 2003, in forum: ASP General
    Replies:
    3
    Views:
    181
    Lorenzo Bolognini
    Sep 22, 2003
  4. Sound
    Replies:
    2
    Views:
    501
    Randy Webb
    Sep 28, 2006
  5. jr
    Replies:
    3
    Views:
    499
Loading...

Share This Page