Link to home
Start Free TrialLog in
Avatar of Clifton Hughes
Clifton Hughes

asked on

Opening forms with an input box

I am trying to open a Form in Access with an input box, but I want to make sure the record exists in the table.

Private Sub Command6_Click()
On Error GoTo Err_ExistingExamID

    Dim strInput As String
    Dim recordCount As Integer
    Dim tryAgain As Integer
    
    'enter loop
    Do
        'get input
        strInput = InputBox("Please enter a valid Exam ID")

        'check input
        If Len(strInput) <> 0 Then
            'look for Booking reference number in tblHABookings
            recordCount = Nz(DCount("*", "tblExams", "[ExID] = '" & strInput & "'"), 0)
            
            If recordCount <> 0 Then 'booking reference was found
                tryAgain = vbNo
                'open Amend form
                DoCmd.OpenForm "frmExaminations", , , "ExID= '" & strInput & "'"
            Else ' booking reference was NOT found
                'prompt user again
                tryAgain = MsgBox("Exam ID '" & strInput & "' was not found. Try again?", vbYesNo)
            End If
        Else
            tryAgain = vbNo
        End If
    Loop Until tryAgain = vbNo

Exit_ExistingExamID:
    Exit Sub

Err_ExistingExamID:
    MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description
    'MsgBox Err.Description
    Resume Exit_ExistingExamID

End Sub

Open in new window

Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

What difficulty are you having?
Avatar of Clifton Hughes
Clifton Hughes

ASKER

When I execute the command, I get the Error telling me the data type mismatch in criteria expression. I should note the ExID is an autonumber.
ASKER CERTIFIED SOLUTION
Avatar of Arana (G.P.)
Arana (G.P.)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks arana!! I knew if was something simple I was overlooking.