Results 1 to 6 of 6

Thread: [RESOLVED] String Functions in VB

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2006
    Posts
    74

    Resolved [RESOLVED] String Functions in VB

    I am making a form at my work. I have a e mail field. I wantto validate it. I was looking for a string function that can take character by character from the given textbox and check for "@" and then "." signs. Going through the help file i found a lot a of functions like substring and others but I couldn't understand how to use it. Can anyone give me a code for the above problem or can tell me in a better way how to use some of the functions.
    Thanks Anup

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,242

    Re: String Functions in VB

    Use the IndexOf() function. Basically you will be doing something like this:

    If <your string here>.IndexOf("@")>-1 then
    'You found one.
    End if

    Then repeat for any other characters you are looking for.
    My usual boring signature: Nothing

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,461

    Re: String Functions in VB

    You're better off using a regular expression to validate an e-mail address. Here's a simple one:
    VB Code:
    1. If Not System.Text.RegularExpressions.Regex.IsMatch(Me.TextBox1.Text, _
    2.                                                             "^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$") Then
    3.             MessageBox.Show("Please enter a valid e-mail address.")
    4.         End If
    There are more complex regex patterns that do a better job of validating but this one is adequate for most circumstances.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2006
    Posts
    74

    Re: String Functions in VB

    thanks guys that things is alread resolved i used the like operator instead but jim that was some cool suff if you can just tell what it means it would be great my email is vishwajeet_th@yahoo.com...........thanks shaggy for your help to that was easier to understand...........anyway guys i got a new problem that i thought i have resolved somehow..........i want to check whether the value entered by the user exists in database or not........as the field entred is primary key so i want to find whther there is a conflict if thee is i want to display a message in the database. i am using the following code but it is not working

    If Adodc1.Recordset.Fields(0).Value = txtid.Text Then
    MsgBox ("Employee Id already exist in database")
    txtid.SetFocus

    apart from the above code i have also tried these but it is not working

    If Adodc1.Recordset.Find(txtid.Text) Then
    MsgBox ("Employee Id already exist in database")
    txtid.SetFocus

    If Adodc1.Recordset.Fields(0) = txtid.Text Then
    MsgBox ("Employee Id already exist in database")
    txtid.SetFocus

    the last code was the one that i was using and it was working ok before....i know this should not be here i should make a new post or change the title but still if u can help me
    thanks anup.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,461

    Re: String Functions in VB

    Regular expressions have been around for a long time and they are very powerful, but the syntax is such that they are not easy to do anything remotely complex without a bit of practice. If you want more information then there are many sites on the Web devoted to the topic. Below are a couple that I've used in the past.

    http://www.regular-expressions.info/
    http://gnosis.cx/publish/programming...pressions.html

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,242

    Re: String Functions in VB

    That doesn't look like ADO.NET, is it older ADO? I don't recognize the recordset.

    The way I would do it, assuming you have a connection called conn would be something like this:

    VB Code:
    1. 'First get a command object:
    2. dim cmd as system.data.oledb.oledbCommand = conDB.CreateCommand
    3. dim i as integer
    4.  
    5. cmd.CommandText = "SELECT ? WHERE ? = ' & txtid.Text & "'"
    6. i = cmd.ExecuteScalar
    7.  
    8. 'See whether anything was returned.

    Something like that. Returning a scalar is very fast, faster than a recordset. However, I added the symbol ? which is not valid SQL. Basically, you want to replace that symbol with the field name. Also, this assumes that the field is an integer. It really doesn't matter what field you return, except that it can't be Null. If the row is found, then i will be set to something other than the 0 it starts out with.

    On the other hand, this may be a whole new realm, since it looks like a new version of ADO.

    I really have to get into regular expressions, they are just too cool.
    My usual boring signature: Nothing

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