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 Basic
  4. Count records using a wildcard...

Count records using a wildcard...

Scheduled Pinned Locked Moved Visual Basic
questioncsharphelp
7 Posts 3 Posters 0 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.
  • C Offline
    C Offline
    CCG3
    wrote on last edited by
    #1

    I am working with VB.Net 2005 and I am looking at an Access 2003 mdb. I need to look into a table and see if any records exists that have a value in a certain field based on what is keyed in from a text box...here is what I have so far. I think I am close but when I get to the line "Count2 = CInt(cmd.ExecuteScalar)" I get error..."No value given for one or more required parameters." So I am certainly missing something. 'Check to make sure the Current Department already exisits Dim Count2 As Integer = 0 Using Connection As New OleDbConnection _ ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Comet 631 Databases\Converted to 2003\C022008.mdb") Dim cmd As OleDbCommand = New OleDbCommand _ ("Select Count(ActAcct) as ExistingRecords From [SHACT] where (@CurrDepart)=?", Connection) cmd.Parameters.AddWithValue("@CurrDepart", OleDbType.VarChar).Value = Me.txtCurGL.Text.ToString + "%" Connection.Open() Count2 = CInt(cmd.ExecuteScalar) End Using 'if the Current Department doesn't exisit then throw message and exit sub If Count2 <= 0 Then MessageBox.Show("The GL Department " & Me.txtCurGL.Text.ToString & " does not exisit.", _ "Copy Department", MessageBoxButtons.OK) Me.txtCurGL.Focus() Me.txtCurGL.SelectAll() Return End If

    N 1 Reply Last reply
    0
    • C CCG3

      I am working with VB.Net 2005 and I am looking at an Access 2003 mdb. I need to look into a table and see if any records exists that have a value in a certain field based on what is keyed in from a text box...here is what I have so far. I think I am close but when I get to the line "Count2 = CInt(cmd.ExecuteScalar)" I get error..."No value given for one or more required parameters." So I am certainly missing something. 'Check to make sure the Current Department already exisits Dim Count2 As Integer = 0 Using Connection As New OleDbConnection _ ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Comet 631 Databases\Converted to 2003\C022008.mdb") Dim cmd As OleDbCommand = New OleDbCommand _ ("Select Count(ActAcct) as ExistingRecords From [SHACT] where (@CurrDepart)=?", Connection) cmd.Parameters.AddWithValue("@CurrDepart", OleDbType.VarChar).Value = Me.txtCurGL.Text.ToString + "%" Connection.Open() Count2 = CInt(cmd.ExecuteScalar) End Using 'if the Current Department doesn't exisit then throw message and exit sub If Count2 <= 0 Then MessageBox.Show("The GL Department " & Me.txtCurGL.Text.ToString & " does not exisit.", _ "Copy Department", MessageBoxButtons.OK) Me.txtCurGL.Focus() Me.txtCurGL.SelectAll() Return End If

      N Offline
      N Offline
      Noctris
      wrote on last edited by
      #2

      For as far as I can see.. you are requesting a fieldname in your table with a parameter : "...where (@CurrDepart)=?"... Which is something you can't do in access... you must use an actual string in the Sql command.. Looking at your code.. i'm gessing you made a mistake cause you don't do any check on the me.txtCurGL.text.tostring string and even add a % ( which is not valid for a column name so...

      with cmd
      .connection = connection
      .commandtext = "select count(ActAcct) as existingRecords from [SHACT] where yourtablenamehere = @CurrDepart"
      .parameters.addwithvalue("@CurrDepart",me.txtCurGL.text.Tostring & "%")
      end with
      connection.open()

      Should work a bit better

      C 1 Reply Last reply
      0
      • N Noctris

        For as far as I can see.. you are requesting a fieldname in your table with a parameter : "...where (@CurrDepart)=?"... Which is something you can't do in access... you must use an actual string in the Sql command.. Looking at your code.. i'm gessing you made a mistake cause you don't do any check on the me.txtCurGL.text.tostring string and even add a % ( which is not valid for a column name so...

        with cmd
        .connection = connection
        .commandtext = "select count(ActAcct) as existingRecords from [SHACT] where yourtablenamehere = @CurrDepart"
        .parameters.addwithvalue("@CurrDepart",me.txtCurGL.text.Tostring & "%")
        end with
        connection.open()

        Should work a bit better

        C Offline
        C Offline
        CCG3
        wrote on last edited by
        #3

        Thank you for your reply Noctris. This is what I have now, but I don't think the wildcard is working because no matter what I enter it doesn't find a Count (always 0). Any idea what I might be doing wrong now? 'Check to make sure the Current Department already exisits Dim Count2 As Integer = 0 Using Connection As New OleDbConnection _ ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Comet 631 Databases\Converted to 2003\C022008.mdb") Dim cmd As OleDbCommand = New OleDbCommand _ ("Select Count(ActAcct) as ExistingRecords From [SHACT] where (ActAcct)= @CurrDepart", Connection) cmd.Parameters.AddWithValue("@CurrDepart", Me.txtCurGL.Text.ToString & "%") Connection.Open() Count2 = CInt(cmd.ExecuteScalar) End Using 'if the Current Department doesn't exisit then throw message and exit sub If Count2 <= 0 Then MessageBox.Show("The GL Department " & Me.txtCurGL.Text.ToString & " does not exisit.", _ "Copy Department", MessageBoxButtons.OK) Me.txtCurGL.Focus() Me.txtCurGL.SelectAll() Return End If

        A 1 Reply Last reply
        0
        • C CCG3

          Thank you for your reply Noctris. This is what I have now, but I don't think the wildcard is working because no matter what I enter it doesn't find a Count (always 0). Any idea what I might be doing wrong now? 'Check to make sure the Current Department already exisits Dim Count2 As Integer = 0 Using Connection As New OleDbConnection _ ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Comet 631 Databases\Converted to 2003\C022008.mdb") Dim cmd As OleDbCommand = New OleDbCommand _ ("Select Count(ActAcct) as ExistingRecords From [SHACT] where (ActAcct)= @CurrDepart", Connection) cmd.Parameters.AddWithValue("@CurrDepart", Me.txtCurGL.Text.ToString & "%") Connection.Open() Count2 = CInt(cmd.ExecuteScalar) End Using 'if the Current Department doesn't exisit then throw message and exit sub If Count2 <= 0 Then MessageBox.Show("The GL Department " & Me.txtCurGL.Text.ToString & " does not exisit.", _ "Copy Department", MessageBoxButtons.OK) Me.txtCurGL.Focus() Me.txtCurGL.SelectAll() Return End If

          A Offline
          A Offline
          Ashfield
          wrote on last edited by
          #4

          CCG3 wrote:

          ("Select Count(ActAcct) as ExistingRecords From [SHACT] where (@CurrDepart)=?", Connection) cmd.Parameters.AddWithValue("@CurrDepart", OleDbType.VarChar).Value = Me.txtCurGL.Text.ToString + "%"

          I think all that is wrong is (@CurrDepart) should be ActAcct (based on your first posting and this one. Your bsic query should be select count(*) from tablename where columnname = parameter Hope this helps

          Bob Ashfield Consultants Ltd

          C 1 Reply Last reply
          0
          • A Ashfield

            CCG3 wrote:

            ("Select Count(ActAcct) as ExistingRecords From [SHACT] where (@CurrDepart)=?", Connection) cmd.Parameters.AddWithValue("@CurrDepart", OleDbType.VarChar).Value = Me.txtCurGL.Text.ToString + "%"

            I think all that is wrong is (@CurrDepart) should be ActAcct (based on your first posting and this one. Your bsic query should be select count(*) from tablename where columnname = parameter Hope this helps

            Bob Ashfield Consultants Ltd

            C Offline
            C Offline
            CCG3
            wrote on last edited by
            #5

            thanks, this is what I did to get past this issue and I have tested it several times and it works fine... 'Check to make sure the Current Department already exisits Dim Count2 As Integer = 0 Using Connection As New OleDbConnection _ ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Comet 631 Databases\Converted to 2003\C022008.mdb") Dim cmd As OleDbCommand = New OleDbCommand _ ("Select Count(ActAcct) as ExistingRecords From [SHACT] where (ActAcct) Like '" & Me.txtCurGL.Text & "%'", Connection) Connection.Open() Count2 = CInt(cmd.ExecuteScalar) End Using 'if the Current Department doesn't exisit then throw message and exit sub If Count2 <= 0 Then MessageBox.Show("The GL Department " & Me.txtCurGL.Text.ToString & " does not exisit.", _ "Copy Department", MessageBoxButtons.OK) Me.txtCurGL.Focus() Me.txtCurGL.SelectAll() Return End If

            N 1 Reply Last reply
            0
            • C CCG3

              thanks, this is what I did to get past this issue and I have tested it several times and it works fine... 'Check to make sure the Current Department already exisits Dim Count2 As Integer = 0 Using Connection As New OleDbConnection _ ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Comet 631 Databases\Converted to 2003\C022008.mdb") Dim cmd As OleDbCommand = New OleDbCommand _ ("Select Count(ActAcct) as ExistingRecords From [SHACT] where (ActAcct) Like '" & Me.txtCurGL.Text & "%'", Connection) Connection.Open() Count2 = CInt(cmd.ExecuteScalar) End Using 'if the Current Department doesn't exisit then throw message and exit sub If Count2 <= 0 Then MessageBox.Show("The GL Department " & Me.txtCurGL.Text.ToString & " does not exisit.", _ "Copy Department", MessageBoxButtons.OK) Me.txtCurGL.Focus() Me.txtCurGL.SelectAll() Return End If

              N Offline
              N Offline
              Noctris
              wrote on last edited by
              #6

              Hi.. Happy for you it works.. i would however keep working with the parameter seeing it is a bit safer ( if someone put's a " ' " in the textbox, the appllication will fail right now.. You should do:

              'Check to make sure the Current Department already exisits
              Dim Count2 As Integer = 0
              Using Connection As New OleDbConnection _
              ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Comet 631 Databases\Converted to 2003\C022008.mdb")
              Dim cmd As new OleDbCommand
              with cmd
              .commandtext = "Select Count(ActAcct) as ExistingRecords From [SHACT] where (ActAcct) Like @department"
              .parameters.addwithvalue("@department",me.txtCurGL.text & "%")
              .connection = connection
              end with
              Connection.Open()
              Count2 = CInt(cmd.ExecuteScalar)
              End Using
              'if the Current Department doesn't exisit then throw message and exit sub
              If Count2 <= 0 Then
              MessageBox.Show("The GL Department " & Me.txtCurGL.Text.ToString & " does not exisit.", _
              "Copy Department", MessageBoxButtons.OK)
              Me.txtCurGL.Focus()
              Me.txtCurGL.SelectAll()
              Return
              End If

              It's a bit cleaner and safer..

              C 1 Reply Last reply
              0
              • N Noctris

                Hi.. Happy for you it works.. i would however keep working with the parameter seeing it is a bit safer ( if someone put's a " ' " in the textbox, the appllication will fail right now.. You should do:

                'Check to make sure the Current Department already exisits
                Dim Count2 As Integer = 0
                Using Connection As New OleDbConnection _
                ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Comet 631 Databases\Converted to 2003\C022008.mdb")
                Dim cmd As new OleDbCommand
                with cmd
                .commandtext = "Select Count(ActAcct) as ExistingRecords From [SHACT] where (ActAcct) Like @department"
                .parameters.addwithvalue("@department",me.txtCurGL.text & "%")
                .connection = connection
                end with
                Connection.Open()
                Count2 = CInt(cmd.ExecuteScalar)
                End Using
                'if the Current Department doesn't exisit then throw message and exit sub
                If Count2 <= 0 Then
                MessageBox.Show("The GL Department " & Me.txtCurGL.Text.ToString & " does not exisit.", _
                "Copy Department", MessageBoxButtons.OK)
                Me.txtCurGL.Focus()
                Me.txtCurGL.SelectAll()
                Return
                End If

                It's a bit cleaner and safer..

                C Offline
                C Offline
                CCG3
                wrote on last edited by
                #7

                Thank you very much Noctris, I will give that a shot. But most importantly I will remember this advice. In this case the text boxes are masked and only accept numeric characters, no symbols at all (not even positive/negative). But thanks again for helping you do a great job and it is much appreciated!!!

                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