Link to home
Start Free TrialLog in
Avatar of TechGuise
TechGuiseFlag for United States of America

asked on

How to open a form based on data entered into INPUTBOX, unless there are more than one matching records

Hello Experts,
I have records with the following type of Primary Key...   YYYY-MM-11111.  
(Year record was created, month created, sequential 5 digit # counting from the 1st of each year)  
Relatively new database, currently they are up to "2015-02-00841"  

I'd like to tie some code to a command button that asks for the last 5 digits, and opens a form with the matching record.   The challenge comes in that if there are more than one records that have those last 5 digits, it would need to bring up a list of the matching records... the user could then select a record from that list to open the form.

The above is the basic need....
What would be awesome is that if it did find more than one record that matched the last 5 digits BUT all but one were more than XX days old, it would not present the list, but go ahead and open the form with the most recent record.

Any help would be appreciated.
Avatar of PatHartman
PatHartman
Flag of United States of America image

A simple way is to use a combo.  Create a query that selects the ID's as field 1 and then the last 5 digits as field 2 suffixed by the YYYY-MM instead of prefixed by it.  Order the query by field 2.  Use this query as the rowsource for the combo.  Hide field1, show field 2.  If you add the combo to a bound form using the wizard, one of the options will be to find a record on the current form, choose that one.
try this as you requested

Private Sub Command0_Click()
Dim strFind As String, strPrefix As String, strNum As String
strPrefix = Year(Now())
strNum = InputBox("Enter: Number 00000")
Stop
If strNum & "" <> "" Then
    strFind = strPrefix & "-**-" & strNum
    Debug.Print strFind
    DoCmd.OpenForm "Form1", , , "[PKName] Like '" & strFind & "'"
End If
End Sub
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

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