Private Sub cbtnAdd_Click()
Dim dXValue As Double
Dim dYValue As Double
Dim lItemNum As Long
' ----------------------------------------------------
' Phase 1 - Check for Valid Data
' ----------------------------------------------------
'Check if X Co-ord is populated
If Me.txtXCoord.Value = "" Then
MsgBox "Please supply a value for X."
Me.txtXCoord.SetFocus
Exit Sub
End If
'Check if the X Co-ord is a valid number
If Not IsNumeric(Me.txtXCoord.Value) Then
MsgBox "X Coordinate must be numeric."
Me.txtXCoord.SetFocus
Exit Sub
End If
'Check if Y Co-ord is populated
If Me.txtYCoord.Value = "" Then
MsgBox "Please supply a value for Y."
Me.txtYCoord.SetFocus
Exit Sub
End If
'Check if the Y Co-ord is a valid number
If Not IsNumeric(Me.txtYCoord.Value) Then
MsgBox "Y Coordinate must be numeric."
Me.txtYCoord.SetFocus
Exit Sub
End If
'Now that we know that we have numeric values
'we can pass them into our variables
dXValue = txtXCoord.Value
dYValue = txtYCoord.Value
' ----------------------------------------------------
' Phase 2 - Check if the x value is already used
' ----------------------------------------------------
'Loop through the existing values, trying
'to find a match
With Me.lstCoords
For lItemNum = 0 To .ListCount - 1
If dXValue = CDbl(.List(lItemNum, 0)) Then
MsgBox "X Coordinate is in use."
Me.txtXCoord.SetFocus
Exit Sub
End If
Next lItemNum
End With
' ----------------------------------------------------
' Phase 3 - Add the new values and clear the textboxes
' ----------------------------------------------------
'Add the values to the listbox
With Me.lstCoords
.AddItem (dXValue)
.Column(1, .ListCount - 1) = dYValue
End With
'Clear the Y textbox
Me.txtYCoord.Value = ""
'Clear the X textbox and move the
'cursor to it
With Me.txtXCoord
.Value = ""
.SetFocus
End With
End Sub