Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Studio
  4. Check if record exists

Check if record exists

Scheduled Pinned Locked Moved Visual Studio
databasehelpworkspace
2 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 12783945
    wrote on last edited by
    #1

    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 String

    Dim 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")
    
    L 1 Reply Last reply
    0
    • U User 12783945

      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 String

      Dim 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")
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Okay and did you have a question? What does your posting have to do with Visual Studio? Do you think that maybe the Visual Basic forum might be better suited?

      Speed of sound - 1100 ft/sec Speed of light - 186,000 mi/sec Speed of stupid - instantaneous.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups