record controleren voor nieuwe toevoegen

Status
Niet open voor verdere reacties.

bubani

Gebruiker
Lid geworden
8 nov 2004
Berichten
13
Hallo daar,

Ik heb een formuliertje handmatig gemaakt waarmee, wat de bedoeling is, de gebruiker records kan toevoegen aan tabellen. ik heb bijvoorbeeld een listbox, (lokatieToevoegen), waar de gebruiker kan kijken of de toe te voegen lokatie al bestaat. zo niet dan typt hij een nieuwe lokatie in en drukt op lokatie toevoegen.

Als de gebruiker een leeg record probeert toe te voegen verschijnt er een msgbox, dit heb ik kunnen oplossen. Mar als de lokatie nou toch bestaat, en de gebruiker drukt op toevoegen, dan geeft acces een error. Ik wil dit niet, ik wil graag dat het toevoegen dan wordt afgebroken en dat de geruiker een bericht krijgt dat het record al bestaat. Dus er moet eerst gecontroleerd worden of er een record met een identieke inhoud aanwezig is. kan iemand mij helpen, hier is de code?


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
 
In welke applicatie ben je aan het werk?

Als het access is kun je gebruik maken van het onNotInList event van de listbox of combobox. Deze krijgt de ingevoerde waarde als parameter mee, je kunt dan dus kijken of je die wilt toevoegen, wilt wijgeren etc.

Uit de helpfile van Access:
NotInList Event — Event Procedures Example

The following example uses the NotInList event to add an item to a combo box.

To try this example, create a combo box called Colors on a form. Set the combo box's LimitToList property to Yes. To populate the combo box, set the combo box's RowSourceType property to Value List, and supply a list of values separated by semicolons as the setting for the RowSource property. For example, you might supply the following values as the setting for this property: Red; Green; Blue.

Next add the following event procedure to the form. Switch to Form view and enter a new value in the text portion of the combo box.

Private Sub Colors_NotInList(NewData As String, _
Response As Integer)
Dim ctl As Control

' Return Control object that points to combo box.
Set ctl = Me!Colors
' Prompt user to verify they wish to add new value.
If MsgBox("Value is not in list. Add it?", _
vbOKCancel) = vbOK Then
' Set Response argument to indicate that data
' is being added.
Response = acDataErrAdded
' Add string in NewData argument to row source.
ctl.RowSource = ctl.RowSource & ";" & NewData
Else
' If user chooses Cancel, suppress error message
' and undo changes.
Response = acDataErrContinue
ctl.Undo
End If
End Sub
Note The above example adds an item to an unbound combo box. When you add an item to a bound combo box, you add a value to a field in the underlying data source. In most cases you can't simply add one field in a new record — depending on the structure of data in the table, you probably will need to add one or more fields to fulfill data requirements. For instance, a new record must include values for any fields comprising the primary key. If you need to add items to a bound combo box dynamically, you must prompt the user to enter data for all required fields, save the new record, and then requery the combo box to display the new value.



Grtz,

Mark van Bree
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan