Results 1 to 4 of 4

Thread: check if new item is allready in a table

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Posts
    6

    check if new item is allready in a table

    hi there here is another question. I have made a form to add records to a table. But i want to disable this possibility in 2 situations;

    1. if the textfield is empty
    2. if the string in the textfield allready excist as a record in the table.

    First i will give you my code

    VB Code:
    1. Private Sub cmdLokatie_Click()
    2. Dim doos As Byte
    3.  
    4. If IsNull(Me!lokatieToevoegen) Then
    5. doos = MsgBox("mag geen nul zijn", vbCritical + vbOKOnly, "fout")
    6. Exit Sub
    7. End If
    8.  
    9.  
    10.  
    11.  
    12. Dim rst As New ADODB.Recordset
    13.  
    14. Dim strSQL As String
    15.  
    16. strSQL = "Select * From Lokatie"
    17.  
    18.  
    19.  
    20. rst.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
    21.  
    22.  
    23. With rst
    24.  
    25.     .AddNew
    26.  
    27.     !Lokatie = Me!lokatieToevoegen
    28.        
    29.    .Update
    30.  
    31.     .Close
    32.  
    33. End With
    34.  
    35.  
    36. End Sub

    As you can see, i have solved my first problem, but how can i check if the new Item to be added by the user, isn't allready a record in the table, so i can end the procedure with exit sub, en give a msgbox why the new item isnt added?


    I hope you can help me

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Just need to see if an exactly matching record exists already.
    (or you could use wildcards to a close match.. but be careful with them).

    VB Code:
    1. Private Sub cmdLokatie_Click()
    2. Dim doos As Byte
    3.  
    4. If IsNull(Me!lokatieToevoegen) Then
    5. doos = MsgBox("mag geen nul zijn", vbCritical + vbOKOnly, "fout")
    6. Exit Sub
    7. End If
    8.  
    9.  
    10.  
    11.  
    12. Dim rst As New ADODB.Recordset
    13.  
    14. Dim strSQL As String
    15.  
    16. '---- single quotes around a string
    17. '---- don't need me... its there anyhow...
    18. '---- edit - added the where bit I forgot ;) duuuh - sorry!!
    19. strSQL = "Select * From Lokatie [b]Where Lokatie = '" & lokatieToevoegen & "'"[/b]
    20.  
    21.  rst.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
    22.  
    23.   With rst
    24.  
    25.     if .eof then
    26.       .AddNew
    27.       !Lokatie = Me!lokatieToevoegen
    28.       .Update
    29.       .Close
    30.     end if
    31.  
    32.  End With
    33. End Sub


    Vince
    Last edited by Ecniv; Nov 9th, 2004 at 08:28 AM.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Posts
    6

    Syntaxerror

    hmm, thanx but now an error appears:

    The Component FROM contains a syntaxerror?
    and the next line is colored yellow:

    VB Code:
    1. rst.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic

    Can you do something with that?

    But till now, thnx for the help

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Posts
    6

    OOPs

    I got it allready, you forgot
    Code:
    where ()
    , my code is now like here and it is working fine;

    VB Code:
    1. Private Sub cmdLokatie_Click()
    2. Dim doos As Byte
    3.  
    4. If IsNull(Me!lokatieToevoegen) Then
    5. doos = MsgBox("Voer alstublieft eerst een nieuw item toe", vbCritical + vbOKOnly, "Onjuiste invoer")
    6. Exit Sub
    7. End If
    8.  
    9.  
    10.  
    11.  
    12. Dim rst As New ADODB.Recordset
    13.  
    14. Dim strSQL As String
    15.  
    16. strSQL = "Select * From Lokatie Where (Lokatie = '" & lokatieToevoegen & "')"
    17.  
    18. rst.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
    19.  
    20.   With rst
    21.  
    22.     If .EOF Then
    23.       .AddNew
    24.       !Lokatie = Me!lokatieToevoegen
    25.       .Update
    26.       .Close
    27.     Else
    28.     doos = MsgBox("Dit item bestaat al", vbCritical + vbOKOnly, "Onjuiste invoer")
    29.     End If
    30.  
    31.  End With
    32. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width