'====================================== ' Form: frmOrderCreate '====================================== ' An unbound form to demonstrate the use of the class module clsOrder '************************COPYRIGHT NOTICE************************* ' Copyright (C) 2000 DI Management Services Pty Ltd, ' Sydney Australia www.di-mgt.com.au. All rights reserved. ' This code was originally written by David Ireland. ' You are free to use it in any application ' provided this copyright notice is left intact. ' If you use it, or found it useful, or can suggest an improvement ' please let us know at code@di-mgt.com.au. '***************************************************************** Option Compare Database Option Explicit ' Declare an new instance of an Order object for this form Private moOrder As New clsOrder '--------------------------------------- Private Sub cmdCancel_Click() '--------------------------------------- ' Just close the form DoCmd.Close acForm, Me.Name End Sub '--------------------------------------- Private Sub cmdOK_Click() '--------------------------------------- On Error GoTo HandleError ' User has said OK, so let's save the order ' First, go gather the properties from the form's controls ' and check for missing fields ' (there are much fancier ways of doing this) If IsNull(Me.cboCustomer) Then Call MissingField(Me.cboCustomer, "You must enter a customer") GoTo Done Else moOrder.CustomerID = Me.cboCustomer End If If IsNull(Me.txtItem) Then Call MissingField(Me.txtQty, "You must enter an item") GoTo Done Else moOrder.Item = Me.txtItem End If If IsNull(Me.txtQty) Then Call MissingField(Me.txtQty, "You must enter a quantity") GoTo Done Else moOrder.Qty = Me.txtQty End If If IsNull(Me.txtUnitPrice) Then Call MissingField(Me.txtUnitPrice, "You must enter a price") GoTo Done Else moOrder.UnitPrice = Me.txtUnitPrice End If ' All gathered, so let's prompt the user If vbOK <> MsgBox("Do you want to save this order?", vbOKCancel) Then GoTo Done End If ' Got here, so let's save it If Not moOrder.Create Then MsgBox "Unable to create a new order", vbCritical GoTo Done Else MsgBox "Created new order with ID=" & moOrder.OrderID End If Done: Exit Sub HandleError: MsgBox "Error: " & Err.Number & vbCr & Err.Description Resume Done End Sub '--------------------------------------- Private Sub MissingField(ctl As Control, sMsg As String) '--------------------------------------- ' Purpose: Tells user they've not filled in control ' Requires: Reference to control; error message ' Returns: -- MsgBox sMsg, vbExclamation, "Missing Field" ctl.SetFocus End Sub