CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2000
    Location
    Ontario, Canada
    Posts
    1

    Putting VB code into Access to validate

    Can anyone help me, I need a procedure to validate input into an Access table field that will not allow for a space or a hyphen. The database was already set up and there are already spaces and hyphens in the field.


  2. #2
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: Putting VB code into Access to validate

    if you are getting information from the user from a text box, for example, then you can not allow hyphens or spaces. there are several ways of doing this. i would recommend using the Replace function and take out those characters before yuo build your SQL statement to insert those values.

    Sub ValidateData()
    dim sName as string

    sName = VBA.Replace(txtName.Text, "-")
    sName = VBA.Replace(sName, " ")

    sql = "INSERT INTO YourTable (Name) VALUES ('" & sName & "')"
    End Sub





    the variable sName now contains the text from the text box, but without any spaces or hyphens.

    as for the data already in the table, you would have to write a little procedure to open the table and the go through the records one at a time and take out the spaces and hyphens. maybe something like this:


    Sub FixData()
    dim rs as adodb.recordset
    dim cn as adodb.connection
    dim x as long

    set cn = new adodb.connection
    cn.connecionstring = "Your connection string"

    cn.open

    sql = "SELECT * FROM theTable"

    set rs = new adodb.recordset

    rs.open sql, cn, adOpenDynamic

    if not rs.eof then
    while not rs.eof
    for x = 0 to rs.fields.count
    rs(x) = VBA.Replace(rs(x), " ")
    rs(x) = VBA.Replace(rs(x), "-")
    rs.update
    next
    rs.movenext
    wend
    end if

    rs.close
    set rs = nothing
    cn.close
    set cn = nothing




    that should give you some ideas about how to clean up the data and keep it clean.

    hope this helps,

    John


    John Pirkey
    MCSD
    http://www.ShallowWaterSystems.com
    http://www.stlvbug.org
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured