Results 1 to 14 of 14

Thread: [RESOLVED] Check If Name Exists In MS Access

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Location
    Nairobi
    Posts
    75

    Resolved [RESOLVED] Check If Name Exists In MS Access

    I know this might be something very simple but it has gotten me scratching my head for some minutes. I want to check if the user entered UserName already exists in MS Access, if it does, the given is rejected.

    Here is what I have:

    Code:
     
    If DTR("UserName") <> TxtUserName.Text.ToString Then
       'If Username is available, save the information to the database
       MyQry = "INSERT INTO UsersTable(UserName,FullName,UserPassword) VALUES('" & TxtUserName.Text "','" & TxtFullName.Text & "','" & TxtConfirmPassword.Text & "')"
       MyCmd = New OleDbCommand(MyQry, Conn)
       With MyCmd
          .ExecuteNonQuery()
       End With
    Close()
    Else
       MsgBox(Prompt:="The provided UserName is not availble. Plese choose another one.", Buttons:=MsgBoxStyle.Exclamation, Title:="UserNameError")
    End If
    Last edited by bentumkoitaba; Mar 27th, 2019 at 03:57 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Check If Name Exists In MS Access

    Please don't post code snippets with fat wads of leading whitespace. Why so many people think it's a great idea to cut off the whitespace from the first line and then leave it in all the others and make the code less readable for us as a result eludes me. Use the Alt key when selecting a block of text in VS to exclude the leading whitespace. If you aren't going to do that, at least make the effort to delete it from your post before submitting.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Check If Name Exists In MS Access

    This question has been asked and answered many, many times before. Query the database for a matching record. If you get one back then there's obviously a matching record, so you don't add another one. If you don't get one back then there's not a matching record, so add away.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,190

    Re: Check If Name Exists In MS Access

    Use parameters to protect your code from SQL injection issues!

    Quote Originally Posted by jmcilhinney View Post
    Use the Alt key when selecting a block of text in VS to exclude the leading whitespace.
    Huh. In all these decades of using Windows, I either never heard of this block select function or forgot about it.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Check If Name Exists In MS Access

    I never knew that, either. I don't have much white space in my code, and if I copy something from VS, I only copy the text. There are a bajillion shortcuts in Windows, I doubt anybody knows them all (though if anybody does, jmcilhinney would be my guess), but this one seems particularly odd/useless. Apparently, MS decided it was worth adding a shortcut for it, though.
    My usual boring signature: Nothing

  6. #6
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: Check If Name Exists In MS Access

    hi,

    you can set the field in the Access Table to ..No dupplicates
    and then catch the Message as an error
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  7. #7
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: Check If Name Exists In MS Access

    Code:
    If DTR("UserName") <> TxtUserName.Text.ToString Then
    If this is the line your talking about then it's an easy fix.

    Code:
    If DTR("UserName").ToString <> TxtUserName.Text Then
    Because DTR("UserName") is an object, so if you want to compare it to a string value then you need to convert it to a string. Also, there is no reason to add ToString to a TextBox.Text value, ".Text" is always a string already.

    This,
    Code:
    If DTR("UserName") <> TxtUserName.Text.ToString Then
    Wouldn't even compile and run if you had Option Strict set to ON. I would suggest doing that, it will make catching these types of errors much easier.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Check If Name Exists In MS Access

    Quote Originally Posted by topshot View Post
    In all these decades of using Windows, I either never heard of this block select function or forgot about it.
    It's not a general Windows feature but a VS-specific feature. They presumably realised that copying a block of code without fat wads of leading whitespace would be useful.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Location
    Nairobi
    Posts
    75

    Re: Check If Name Exists In MS Access

    Thanks everyone for your contributions on this simple issue.

    Thanks too that I've learnt about Alt key.

    I got the solution of this question anyway, I think I was't thinking when I asked in the first place. Thanks anyway.

    Code:
    'Check if there is duplicate UserName in the Database
    MyQry = "SELECT * FROM UsersTable WHERE  UserName = '" & TxtUserName.Text & "'"
    MyCmd = New OleDbCommand(MyQry, Conn)
    MyCmd.ExecuteNonQuery()
    DTR = MyCmd.ExecuteReader
    Select Case DTR.Read
        Case True
            MsgBox(Prompt:="The provided UserName is not availble. Plese choose another one.", Buttons:=MsgBoxStyle.Exclamation, Title:="UserNameError")
        Case Else
            'If Username is available, save the information to the database
            Dim DBUserName As String = DTR("UserName")
            Dim _UserName As String = TxtUserName.Text
            MyQry = "INSERT INTO UsersTable(UserName,FullName,UserPassword) VALUES('" & TxtUserName.Text & "','" & TxtFullName.Text & "','" & TxtConfirmPassword.Text & "')"
            MyCmd = New OleDbCommand(MyQry, Conn)
            With MyCmd
                .ExecuteNonQuery()
            End With
            Close()
    End Select

  10. #10
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,190

    Re: Check If Name Exists In MS Access

    Quote Originally Posted by jmcilhinney View Post
    It's not a general Windows feature but a VS-specific feature. They presumably realised that copying a block of code without fat wads of leading whitespace would be useful.
    Actually it is a Windows feature according to what I found when I searched for it (I was not doing it correctly at first), and it works in Word (since at least Word 97), Notepad, Notepad++. It does not work in Chrome. It must have to do with the type of container the text is within.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Check If Name Exists In MS Access

    Quote Originally Posted by topshot View Post
    Actually it is a Windows feature according to what I found when I searched for it (I was not doing it correctly at first), and it works in Word (since at least Word 97), Notepad, Notepad++. It does not work in Chrome. It must have to do with the type of container the text is within.
    I just tested in Notepad and it didn't work for me. Did you have to do anything special to make it work?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,190

    Re: [RESOLVED] Check If Name Exists In MS Access

    Sorry, was typing faster than my brain. Only Notepad++ and Word, not Notepad or Wordpad (I rarely use those anyway).

  13. #13
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: [RESOLVED] Check If Name Exists In MS Access

    duplicate

  14. #14
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: [RESOLVED] Check If Name Exists In MS Access

    Code:
    'Check if there is duplicate UserName in the Database
    MyQry = "SELECT * FROM UsersTable WHERE  UserName = '" & TxtUserName.Text & "'"
    MyCmd = New OleDbCommand(MyQry, Conn)
    MyCmd.ExecuteNonQuery()
    DTR = MyCmd.ExecuteReader
    Select Case DTR.Read
        Case True
            MsgBox(Prompt:="The provided UserName is not availble. Plese choose another one.", Buttons:=MsgBoxStyle.Exclamation, Title:="UserNameError")
        Case Else
            'If Username is available, save the information to the database
            Dim DBUserName As String = DTR("UserName")
            Dim _UserName As String = TxtUserName.Text
            MyQry = "INSERT INTO UsersTable(UserName,FullName,UserPassword) VALUES('" & TxtUserName.Text & "','" & TxtFullName.Text & "','" & TxtConfirmPassword.Text & "')"
            MyCmd = New OleDbCommand(MyQry, Conn)
            With MyCmd
                .ExecuteNonQuery()
            End With
            Close()
    End Select
    You might think this code is working correctly but there are definite problems.

    This code seem to do absolutely nothing,
    Code:
    MyCmd = New OleDbCommand(MyQry, Conn)
    MyCmd.ExecuteNonQuery()
    You should be using Parameters in your queries. Try your code and use "O'Brian" as the username.

    There's no reason to be using a DataReader or a Select Statement.

    Here is an simple example,
    Code:
            Using con As New OleDbConnection(My.Settings.waterConnectionString)
                con.Open()
                Using cmd As New OleDbCommand("Select Count(*) from autonum WHERE Field1 = ?", con)
                    cmd.Parameters.AddWithValue("?", Me.TextBox1.Text)
                    Dim num As Integer = CInt(cmd.ExecuteScalar)
                    If num > 0 Then
                        MessageBox.Show("Name already in Database")
                    Else
                        cmd.CommandText = "INSERT INTO autonum (Field1) VALUES(?)"
                        num = cmd.ExecuteNonQuery
                        If num > 0 Then
                            MessageBox.Show("User Added")
                        End If
                    End If
                End Using
            End Using

Tags for this Thread

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