Article ID: 125648
Article Last Modified on 11/6/2000
Sub Customer_ID_NotInList (NewData As String, Response As Integer)
Dim DB As Database
Dim RS As Recordset
Dim Msg As String
Dim CR As String: CR = Chr$(13)
' Exit Sub if the user cleared the selection.
If NewData = "" Then Exit Sub
' Ask if the user wants to add the new customer.
Msg = "'" & NewData & "' is not in the list." & CR & CR
Msg = Msg & "Do you want to add it?"
If MsgBox(Msg, 32 + 4) = 7 Then
' If the user chooses No, instruct the user to try again.
Response = DATA_ERRCONTINUE
MsgBox "Please try again."
Else
' If the user does not choose No, create a new record in the
' Customer table.
On Error Resume Next
' Open the Customer table.
Set DB = DBEngine.Workspaces(0).Databases(0)
Set RS = DB.OpenRecordset("Customers", DB_OPEN_DYNASET)
RS.AddNew
Msg = "Please enter a unique 5-character Customer ID."
RS![Customer ID] = InputBox(Msg)
RS![Company Name] = NewData
RS.Update
' If an error occurred while adding the record...
If Err Then
' ...instruct the user to try again.
Response = DATA_ERRCONTINUE
Beep: MsgBox Error$, 48
MsgBox "Please try again."
Else
' If no error occurred, add the element to the combo box
' list.
Response = DATA_ERRADDED
End If
End If
End Sub
Sub Customer_ID_NotInList (NewData As String, Response As Integer)
Dim Result
Dim Msg As String
Dim CR As String: CR = Chr$(13)
' If the user cleared the selection, exit now.
If NewData = "" Then Exit Sub
' Ask if the new customer should be added.
Msg = "'" & NewData & "' is not in the list." & CR & CR
Msg = Msg & "Do you want to add it?"
If MsgBox(Msg, 32 + 4) = 6 Then
' If Yes, launch the Customers form in data entry
' .. mode as a DIALOG form, passing the unique company name
' .. in the NewData variable as an argument to be used as
' .. the default company name in the new Customer record.
DoCmd OpenForm "Customers", , , , A_ADD, A_DIALOG, NewData
End If
' See if the user created the customer...
Result = DLookup("[Company Name]", "Customers", _
"[Company Name]=""" & NewData & """")
If IsNull(Result) Then
' ... if not then instruct the user to try again...
Response = DATA_ERRCONTINUE
MsgBox "Please try again!"
Else
' ... otherwise, add the element to the combo list.
Response = DATA_ERRADDED
End If
End Sub
Sub Form_Load ()
' If OpenArgs is not null (contains a new company name)...
If IsNull(Me.OpenArgs) = False Then
' ...use the contents as the Company Name field.
Me![Company Name] = Me.OpenArgs
End If
End Sub
Keywords: kbhowto kbusage KB125648