Hi all,
I'm trying to make a form wich can make new users in a Access database. However, usernames can't be duplicate, but I can't seem to get the code to work so users can't enter 2 of the same entries. Can anyone help?

    Private Sub btnAanmaken_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAanmaken.Click
        Dim strNaam As String
        Dim blnZoeken As Boolean
        strNaam = txtNaam.Text
        GebruikersDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        Dim intcount As Integer = 0
        For Each Row As DataGridViewRow In GebruikersDataGridView.Rows
            If GebruikersDataGridView.Rows(intcount).Cells(1).Value.ToString = strNaam Then
                GebruikersDataGridView.Rows(intcount).Selected = True
                blnZoeken = True
                Exit For
            End If
            intcount += 1
        Next Row
        If blnZoeken = True Then
            MessageBox.Show("Naam bestaat al", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            If txtWachtwoord.Text = txtWachtwoordHerhaal.Text Then
                MessageBox.Show(strNaam & " is met succes aangemaakt!", "Succes", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.Validate()
                Me.GebruikersBindingSource.EndEdit()
                Me.TableAdapterManager.UpdateAll(Me.EasybyteDataSet)
                Me.Close()
                Hoofdmenu.Show()
            Else
                MessageBox.Show("Wachtwoord is fout", "Fout", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End If
    End Sub

The typical way is to do something like

select the count of records with the requested username
if count > 0 then
    that name is already taken
else
    add the name to the database
end if

The select would look like

"SELECT COUNT(*) FROM mytable WHERE Username = 'newuser'"

which with parameters would be

Dim con As New OleDbConnection(your connection string here)
Dim cmd As New OleDbCommand("", con)

cmd.CommandText = "SELECT COUNT(*) FROM mytable WHERE Username = ?"
cmd.Parameters.AddWithValue("@username", txtUserName.Text)

If cmd.ExecuteNonQuery() > 0 ...

But i added my .mdb file in my project so the connection string isn't needed anymore. Is there a way to use this without connection string?

AFAIK, you are using TYPED dataset. What you need to do is, in the table inside you dataset, you need to perform a check with the username you take as an input from user and if it exists display an error message. On the other hand insert an entry in database.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.