Check if record exists
-
Hi, I am totally new to VB and need some help in some code. I have a user form and an access mdb database file. On the user form there is a name field which once the user has entered a name and pressed a button I want the code to check on the database if that name exists, if it does then do something else not. Please help.
Imports System.Data.OleDb
Public Class Form1
Dim con As New OleDb.OleDbConnection
Dim inc As Integer
Dim MaxRows As Integer
Dim dbProvider As String
Dim dbSource As String
Dim MyDocumentsFolder As String
Dim TheDatabase As String
Dim FullDatabasePath As StringDim ds As New DataSet Dim da As OleDb.OleDbDataAdapter Dim sql As String Private Sub Form1\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;" TheDatabase = "/AddressBook.mdb" MyDocumentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) FullDatabasePath = MyDocumentsFolder & TheDatabase dbSource = "Data Source = " & FullDatabasePath con.ConnectionString = dbProvider & dbSource con.Open() sql = "SELECT \*FROM tblContacts" da = New OleDb.OleDbDataAdapter(sql, con) da.Fill(ds, "AddressBook") con.Close() MaxRows = ds.Tables("AddressBook").Rows.Count inc = -1 End Sub Private Sub NavigateRecords() txtFirstName.Text = ds.Tables("AddressBook").Rows(inc).Item(1) txtSurname.Text = ds.Tables("AddressBook").Rows(inc).Item(2) cboxHouse.Checked = ds.Tables("AddressBook").Rows(inc).Item(3) End Sub Private Sub btnNext\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click If inc <> MaxRows - 1 Then inc = inc + 1 NavigateRecords() Else MessageBox.Show("No More Rows") End If End Sub Private Sub btnPrevious\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click If inc > 0 Then inc = inc - 1 NavigateRecords() ElseIf inc = -1 Then MessageBox.Show("No Records Yet") ElseIf inc = 0 Then MessageBox.Show("First Record")
-
Hi, I am totally new to VB and need some help in some code. I have a user form and an access mdb database file. On the user form there is a name field which once the user has entered a name and pressed a button I want the code to check on the database if that name exists, if it does then do something else not. Please help.
Imports System.Data.OleDb
Public Class Form1
Dim con As New OleDb.OleDbConnection
Dim inc As Integer
Dim MaxRows As Integer
Dim dbProvider As String
Dim dbSource As String
Dim MyDocumentsFolder As String
Dim TheDatabase As String
Dim FullDatabasePath As StringDim ds As New DataSet Dim da As OleDb.OleDbDataAdapter Dim sql As String Private Sub Form1\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;" TheDatabase = "/AddressBook.mdb" MyDocumentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) FullDatabasePath = MyDocumentsFolder & TheDatabase dbSource = "Data Source = " & FullDatabasePath con.ConnectionString = dbProvider & dbSource con.Open() sql = "SELECT \*FROM tblContacts" da = New OleDb.OleDbDataAdapter(sql, con) da.Fill(ds, "AddressBook") con.Close() MaxRows = ds.Tables("AddressBook").Rows.Count inc = -1 End Sub Private Sub NavigateRecords() txtFirstName.Text = ds.Tables("AddressBook").Rows(inc).Item(1) txtSurname.Text = ds.Tables("AddressBook").Rows(inc).Item(2) cboxHouse.Checked = ds.Tables("AddressBook").Rows(inc).Item(3) End Sub Private Sub btnNext\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click If inc <> MaxRows - 1 Then inc = inc + 1 NavigateRecords() Else MessageBox.Show("No More Rows") End If End Sub Private Sub btnPrevious\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click If inc > 0 Then inc = inc - 1 NavigateRecords() ElseIf inc = -1 Then MessageBox.Show("No Records Yet") ElseIf inc = 0 Then MessageBox.Show("First Record")