If Record Exist

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gayano
    New Member
    • Jul 2007
    • 36

    If Record Exist

    HI,
    What i trying to do is
    when a user try to add new item that already in the table
    (example try to add a "name" that already in the "Name_Table ")
    msgbox pops up and tell the user that the name is already in the table

    is there anyway to do this in Visual basic 2005

    Thanks (Any help)
  • edpudol
    New Member
    • Aug 2007
    • 5

    #2
    Use a sql statement something like

    Select * from table where name='juan dela crus'

    if the return value is 1 or more then the name is exist

    Comment

    • gayano
      New Member
      • Jul 2007
      • 36

      #3
      Thanks

      Could you give me a example vb code

      Thank you

      Comment

      • xtianixm
        New Member
        • Jul 2007
        • 52

        #4
        SAMPLE:


        SQL = Select * from table where name='juan dela crus'

        set rs = conn.execute(SQ L)

        if not rs.eof then

        msgbox "Oyee file already exist ^__^"

        end if


        ..hope that solve your problem ^__^

        Comment

        • debasisdas
          Recognized Expert Expert
          • Dec 2006
          • 8119

          #5
          before inserting the record execute this

          select count(name_fiel d) from table_name where name_field= 'name';

          if it returns 1 or more ,you can return the message and no need to execute the insert statment.

          Comment

          • gayano
            New Member
            • Jul 2007
            • 36

            #6
            Thank you guys
            Thank you veeeeeeeeeeeeee eeeeeery much

            Comment

            • gayano
              New Member
              • Jul 2007
              • 36

              #7
              sorry guys i need more info

              What to do before writing those codes that you gave me.
              cz im not verry good at sql

              Im using sql server 2005


              do i have open a connection to my database first??
              can you give me A to Z example??

              Comment

              • edpudol
                New Member
                • Aug 2007
                • 5

                #8
                Originally posted by gayano
                sorry guys i need more info

                What to do before writing those codes that you gave me.
                cz im not verry good at sql

                Im using sql server 2005


                do i have open a connection to my database first??
                can you give me A to Z example??
                Yes you need to open new connection first...

                e.g.

                Dim dbs As ADODB.Connectio n
                Dim rec As ADODB.Recordset

                Set dbs = New ADODB.Connectio n
                Set rec = New ADODB.Recordset

                sConnString = "Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=C:\MyDat abase.mdb"
                dbs.Open sConnString
                rec.Open "SELECT * FROM TABLE WHERE name='juan dela cruz'", dbs

                If rec.EOF And rec.BOF Then
                MsgBox "not exist"
                Else
                MsgBox "exist"
                End If

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  I think it might help a lot if you tell/show us what you've got so far. We don't know how you're storing the data yet.

                  Comment

                  • gayano
                    New Member
                    • Jul 2007
                    • 36

                    #10
                    This is What i done for inserting data?? this works greate but i dont want user to add same data again and again sowhat should i do??

                    at the time i dont have any diclaration fo sql conn... ,,,, (Dont mind about the Japanese Charactors in the msg box's)

                    [CODE=vbnet]Private Sub InsertData()
                    Dim Truckcode As Integer
                    Dim TruckNo As String
                    Dim TruckMake As String
                    Dim TruckRegDate As Date
                    Dim TruckOption As String
                    Dim TruckDriver As String
                    Dim TruckTons As Integer
                    Dim TruckSS As Date
                    Dim TruckSE As Date


                    Truckcode = CInt(txtTruckCo de.Text)
                    TruckNo = txtTruckNo.Text
                    TruckMake = txtTruckMake.Te xt
                    TruckRegDate = CDate(txtmTruck RegDate.Text)
                    TruckOption = txtTruckOption. Text
                    TruckDriver = txtTruckDriver. Text
                    TruckTons = CInt(txtTruckTo ns.Text)
                    TruckSS = CDate(txtmTruck SS.Text)
                    TruckSE = CDate(txtmTruck SE.Text)

                    Try
                    Me.TrucksTableA dapter.Insert(T ruckcode, TruckNo, TruckMake, TruckRegDate, TruckOption, TruckDriver, TruckTons, TruckSS, TruckSE)
                    Me.TrucksTableA dapter.Update(M e.UnkouTrucks.T rucks)
                    Catch ex As Exception
                    MsgBox("入力間違ってま す 管理人にお問い合わせしてくださ い....っていうかガヤンに聞 いて" & ex.Message)

                    End Try
                    MsgBox("トラック1台追 加されました", MsgBoxStyle.Inf ormation, "確認")



                    ''************* *** Update and Fill Truck Form *************** *******''

                    Try

                    Trucks.TrucksTa bleAdapter.Upda te(UnkouTrucks. Trucks)
                    Trucks.TrucksTa bleAdapter.Fill (UnkouTrucks.Tr ucks)
                    Catch ex As Exception
                    MsgBox("入力間違ってま す 管理人にお問い合わせしてくださ い....っていうかガヤンに聞 いて" & ex.Message)
                    End Try


                    ''************* *** Clear The form for the next entry *************** **''
                    ClearForm()

                    End Sub[/CODE]

                    Thank you
                    Last edited by Killer42; Aug 4 '07, 12:02 AM. Reason: Added [CODE=vbnet] tag

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      A lot of this is VB.Net-specific, so I can't help with it (I use "old faithful", VB6). But the simplest thing might be to put unique indexes on one or more of the fields. That way, the database would reject any duplicates, and you can just catch the exception.

                      Otherwise, you need to do a search for the new values before storing them. I don't know the syntax to do that with a "data adaptor".

                      Comment

                      • gayano
                        New Member
                        • Jul 2007
                        • 36

                        #12
                        Thank you killer42,

                        anyway when it catch the errors but unfortunatly it still insert the data.
                        any wrong in the code??

                        like where shoukd i catch the exception??

                        can someone do a example in here??!!

                        Comment

                        Working...