Tim
Thanks for trying to help. Imay not have made it clear what I was trying to
achieve. My code is working fine.It checks for a record ok but I want my
resulting message box to show what it was searching for.In my particular
database my unbound form asks for a car registration no to be input before a
new record can be set up. The query checks the record set as these numbers
are not allowed to be duplicated. So the user clicks a command button, this
opens the form asking for the reg no of the car say ABC1234. This record is
found and the message box says "A client with this Car Reg already exists". I
would like it to read "A client with car reg ABC1234 already exists"(or
whatever was typed into the form). As I say the code I have works fine I am
just having trouble with the syntax of the message box and whilst not crucial
would allow my interface to look a bit more polished. Anyway here is the code
I am using:
Private Sub cmdOK_Click()
Dim strCarReg As String
Dim qry As QueryDef
Dim NumRecords As Byte
On Error GoTo Error_Trap:
Me.txtCarReg.SetFocus
strCarReg = Me.txtCarReg.Text
Set qry = CurrentDb.QueryDefs("qryCarReg")
qry.Parameters("CarReg").Value = strCarReg
Dim Rs As DAO.Recordset
Set Rs = qry.OpenRecordset
Rs.MoveLast
NumRecords = Rs.RecordCount
Check_Number:
If NumRecords > 0 Then
'This means the Car Reg already exists
Dim ViewRecord As Integer
ViewRecord = MsgBox("A client with this Car Reg already exists! View
record?", vbExclamation + vbYesNo, "VALIDATION ALERT. EXISTING RECORD FOUND")
If ViewRecord = vbYes Then
'Open clients form and go to record
Dim strFilter As String
strFilter = "CarReg='" & strCarReg & "'"
DoCmd.Close acForm, "frmInputCarReg"
DoCmd.OpenForm "frmCLIENTDATAENTRY", , , strFilter
Forms("frmCLIENTDATAENTRY").Surname.SetFocus
Forms("frmCLIENTDATAENTRY").cmdFindClient.Enabled = False
Else
txtCarReg.SetFocus
txtCarReg.Value = Null
cmdOK.Enabled = False
cmdReset.Enabled = False
End If
Else
Forms("frmCLIENTDATAENTRY").SetFocus
DoCmd.GoToRecord , , acNewRec
Forms("frmCLIENTDATAENTRY").CarReg.SetFocus
Forms("frmCLIENTDATAENTRY").CarReg.Text = strCarReg
Forms("frmCLIENTDATAENTRY").Surname.SetFocus
Close_Form
End If
Exit Sub
Error_Trap:
If Err.Number = 3021 Then
NumRecords = 0
Resume Check_Number
Else
MsgBox Err.Number & vbCrLf & Err.Description
Exit Sub
End If
End Sub
Private Sub Command20_Click()
End Sub
Private Sub cmdReset_Click()
txtCarReg.SetFocus
txtCarReg.Text = ""
cmdOK.Enabled = False
cmdReset.Enabled = False
End Sub
Private Sub Form_Open(Cancel As Integer)
Me. CarReg.SetFocus
cmdReset.Enabled = False
End Sub
Private Sub txtCarReg_Exit(Cancel As Integer)
If Me.txtCarReg.Text = "" Then
Me.cmdOK.Enabled = False
Me.cmdReset.Enabled = False
Else
Me.cmdOK.Enabled = True
Me.cmdReset.Enabled = True
End If
End Sub