views:

21

answers:

1

In my database I have a form for searching products (fields : id, name, manufacturer_id[set to display manufacturer's name], category_id, price). I would like to have the ability of adding manufacturers through this form. So I've created vba script but it does not work. Still when I enter new name it gives me prompt to "Select element from the list". What am I doing wrong ?

Private Sub manufacturer_id_NotInTheList(NewData As String, Response As Integer)

Dim strSQL As String, strInfo As String

strInfo = "Manufacturer " & NewData & " is not on the list." & vbCrLf & "Add?"
If MsgBox(strInfo, vbYesNo + vbQuestion, "Element not on the list") = vbYes Then
  strSQL = "INSERT INTO manufacturer (name, country, id_distributor) VALUES ('" & NewData & "','Undefined', '0');"
  DoCmd.SetWarnings (False)
  DoCmd.RunSQL strSQL
  Response = acDataErrAdded
Else
  Response = acDataErrContinue
  NewData = ""
  Me.manufacturer_id.Text = ""
End If

End Sub
+2  A: 

If the Limit to List property of your drop down box is set to true, Access will intercept and complain first (because your user has added a manufacturer that is not in the list).

Try setting Limit to List to false, then your procedure should run.

rrrhys
But doesn't he want it set to True? Otherwise, what's the point of having this code at all, since you can just type in whatever you like, rather than having it be restricted to the values in the other table?
David-W-Fenton
I gathered he calls this method to validate the field (say, on submit), as the else statement then clears the text field and populates some error field if the user chooses not to add a new manufacturer. So as long as he makes sure this method runs when it is a new item, it is restricting haphazard user entry by asking them (I see what you are saying though and agree with you to an extent)
rrrhys
The whole point of choosing a combo box as your control is to restrict data entry. If you've used it for that, you don't need to validate it on saving the whole record.
David-W-Fenton