|
-
Nov 9th, 2004, 05:04 AM
#1
Thread Starter
New Member
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:
Private Sub cmdLokatie_Click()
Dim doos As Byte
If IsNull(Me!lokatieToevoegen) Then
doos = MsgBox("mag geen nul zijn", vbCritical + vbOKOnly, "fout")
Exit Sub
End If
Dim rst As New ADODB.Recordset
Dim strSQL As String
strSQL = "Select * From Lokatie"
rst.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
With rst
.AddNew
!Lokatie = Me!lokatieToevoegen
.Update
.Close
End With
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
-
Nov 9th, 2004, 07:09 AM
#2
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:
Private Sub cmdLokatie_Click()
Dim doos As Byte
If IsNull(Me!lokatieToevoegen) Then
doos = MsgBox("mag geen nul zijn", vbCritical + vbOKOnly, "fout")
Exit Sub
End If
Dim rst As New ADODB.Recordset
Dim strSQL As String
'---- single quotes around a string
'---- don't need me... its there anyhow...
'---- edit - added the where bit I forgot ;) duuuh - sorry!!
strSQL = "Select * From Lokatie [b]Where Lokatie = '" & lokatieToevoegen & "'"[/b]
rst.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
With rst
if .eof then
.AddNew
!Lokatie = Me!lokatieToevoegen
.Update
.Close
end if
End With
End Sub
Vince
Last edited by Ecniv; Nov 9th, 2004 at 08:28 AM.
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...
-
Nov 9th, 2004, 07:21 AM
#3
Thread Starter
New Member
Syntaxerror
hmm, thanx but now an error appears:
The Component FROM contains a syntaxerror?
and the next line is colored yellow:
VB Code:
rst.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
Can you do something with that?
But till now, thnx for the help
-
Nov 9th, 2004, 07:36 AM
#4
Thread Starter
New Member
OOPs
I got it allready, you forgot , my code is now like here and it is working fine;
VB Code:
Private Sub cmdLokatie_Click()
Dim doos As Byte
If IsNull(Me!lokatieToevoegen) Then
doos = MsgBox("Voer alstublieft eerst een nieuw item toe", vbCritical + vbOKOnly, "Onjuiste invoer")
Exit Sub
End If
Dim rst As New ADODB.Recordset
Dim strSQL As String
strSQL = "Select * From Lokatie Where (Lokatie = '" & lokatieToevoegen & "')"
rst.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
With rst
If .EOF Then
.AddNew
!Lokatie = Me!lokatieToevoegen
.Update
.Close
Else
doos = MsgBox("Dit item bestaat al", vbCritical + vbOKOnly, "Onjuiste invoer")
End If
End With
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|