Using a combobox to check if record exist then populate form

Anonymous
2014-09-10T22:04:07+00:00

So I have posted my question to several forums and have had some good response but no resolution. I am quite new to Access 2010 and VBA. So here is my issue. I have built a Navigation Page with several forms(bound) so my users can enter data and saved to a table. What I have is a room delay form to capture information for a  medical procedure and to document when a delay occurs. My TABLE is tblDelay and my form is FormDelay.

On occasion my users will enter some information and save and close the form. Then will need to edit the record. I have attempted to create a Assession Num Search in the form header based on someone's recommendation using this code:

Private Sub cboAssessionSearch_AfterUpdate()

Const MESSAGETEXT = "This Assession Number already exists."

 If Not IsNull(DLookup("AssessionNumber", "tblDelay", "AssessionNumber=" & Me.AssessionNumber)) Then

 MsgBox MESSAGETEXT, vbExclamation, "Invalid Operation"

 'code to filter form for the existing record

 Me.Filter = "AssessionNumber=" & Me.AssessionNumber

 Me.FilterOn = True

 Else

 'code to move to new record row

 DoCmd.GoToRecord , , acNewRec

 End If

End Sub

I can find the populated Assession Numbers in the combobox but I need to have certain criteria. If the assession number exists the populate the form with the record fields so an edit can be made and an update to the record made in the table. If the Assession Number does not exist leave the form blank so a new record can be entered. Nothing happens when I select a Assession Number and hit enter. No error message indication record exists or not.

I have built several buttons (Add, Edit, Delate, Clear, and Close) in the header. If a assession Numbers need edited and updated then repopulate the form with the appropriate record found using the Assession Num Search combobox, If no record found, the enter new information.

Table Field Names

dept, Rm_Num, MRN, AssessionNumber, CaseNumber (Not inclusive of all fields, If I can get these filed to populate I can get the rest)

Form Names

cboDept, cboRmNum, txtMRN, txtAssessionNumber, txtCaseNumber (Not inclusive of all boxes)

Bound Form to table

Any help finding resolution would be of GREAT HELP!!! 

Thank you in advance,

Kerry

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

51 answers

Sort by: Newest
  1. Anonymous
    2014-09-11T21:36:18+00:00

    1.  How do I get the MessageText "No Matching Record" and "Warning" to fire? Can't seem to see that message with data input.

    2.   Also, Is it possible to leave the form blank until a Go To Contact has been selected.

    1.   Can we add a message for edit/update to ask if user really wants to change the data in a record.

    1.   That's really only in there as a backstop.  Normally that message will never be displayed, only in the unlikely circumstance that the combo box lists more distinct values than are actually represented in the form's recordset.  If a user enters a new value then the form's NotInList event procedure (q.v.) will execute and prompt the user for confirmation of whether they wish to add the new item to the list or not.  If they confirm then the form is moved to a new record, and the new value assigned to the control (actually two controls in my case as the name is parsed, but it would be one in yours).

    2.  You could move to an empty new record when the form opens by putting this in its Open event procedure:

            DoCmd.GoToRecord acForm, Me.Name, acNewRec

    3.  the simple way is to put the following in the form's BeforeUpdate event procedure:

        Const MESSAGETEXT = "Do you wish to save the changes to the record?"

        If MsgBox(MESSAGETEXT, vbQuestion + vbYesNo, "Confirm") = vbNo Then

            Cancel = True

            Me.Undo

        End If

    However, the fact that a record is updated does not per se mean the values of its data have been changed.  The above also is clumsy if the user attempts to save the record by closing the form.  For a solution which only prompts for confirmation only where the data has actually changed see the ChangedRecordDemo file in my same OneDrive folder.  The code for the form's module in this case is rather more complex, as follows:

    Option Compare Database

    Option Explicit

    Private Sub Form_BeforeInsert(Cancel As Integer)

        Me.DateTimeStamp = Now()

    End Sub

    Private Sub Form_BeforeUpdate(Cancel As Integer)

     On Error GoTo Err_Handler

        Const MESSAGETEXT = "Data has changed.  Save record?"

        If Not Me.NewRecord Then

            ' store unsaved values of bound controls in array

            StoreProposedVals Me

            ' if data in controls has changed get user

            ' confirmation to save record

            If RecordWillChange() Then

                If MsgBox(MESSAGETEXT, vbQuestion + vbYesNo, "Confirm") = vbNo Then

                    Cancel = True

                    Me.Undo

                End If

            End If

        Else

            If MsgBox("Save new record", vbQuestion + vbYesNo, "Confirm") = vbNo Then

                Cancel = True

                Me.Undo

            Else

                ' timestamp record

                Me.DateTimeStamp = Now()

                Me.UpdatedBy = GetUser()

            End If

        End If

    Exit_Here:

       Exit Sub

    Err_Handler:

       MsgBox Err.Description, vbExclamation, "Error"

       Resume Exit_Here

    End Sub

    Private Sub Form_Current()

        On Error GoTo Err_Handler

        If Not Me.NewRecord Then

            ' store current values of bound controls in array

            StoreCurVals Me

        End If

    Exit_Here:

        Exit Sub

    Err_Handler:

        MsgBox Err.Description, vbExclamation, "Error"

        Resume Exit_Here

    End Sub

    Private Sub Form_Error(DataErr As Integer, Response As Integer)

        Const IS_DIRTY = 2169

        ' suppress system error message if form

        ' is closed while record is unsaved,

        ' NB: changes to current record will be lost

        If DataErr = IS_DIRTY Then

             Response = acDataErrContinue

        End If

    End Sub

    The above code calls the following function to determine if the data in the record will in fact be changed if the record is saved:

    Public Function RecordWillChange() As Boolean

       Dim n As Integer, intlast As Integer

       Dim var As Variant

       Dim aOld(), aNew()

       intlast = UBound(aOldVals) - 1

       ' loop through array of original values

       ' and store in new array

       ReDim Preserve aOld(UBound(aOldVals))

       For Each var In aOldVals()

           aOld(n) = var

           n = n + 1

       Next var

       n = 0

       ' loop through array of edited values

       ' and store in new array

       ReDim Preserve aNew(UBound(aOld))

       For Each var In aNewVals()

           aNew(n) = var

           ' if any value has changed then return True

           If (IsNull(aNew(n)) And Not IsNull(aOld(n))) _

               Or (Not IsNull(aNew(n)) And IsNull(aOld(n))) _

               Or aNew(n) <> aOld(n) Then

               RecordWillChange = True

               Exit For

           End If

           n = n + 1

       Next var

    End Function

    Was this answer helpful?

    0 comments No comments
  2. ScottGem 68,840 Reputation points Volunteer Moderator
    2014-09-11T14:49:21+00:00

    I use the search combo frequently. The only kicker here is that you are suing a Navigation form. I have not worked greatly with Navigation forms but as I understand them, they are an overall form with tabs, that you place embedded subforms on. So as you select a tab, you are seeing a subform on that tab. For a search combo to work, it needs to be within the same form that you want to position the record to. If its not it won't work properly. 

    I suspect, Ken's example uses the Not In List event to fire messages that there are no matching records. 

    As for ensuring that users don't "accidentally edit a record", this is a concern with Access. What I do, is use the function found here: http://allenbrowne.com/ser-56.html to lock my controls until the user presses a button to unlock them. This still allows your search controls to work (since they need to be unbound), but prevents the user from unconsciously editing data.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2014-09-11T14:31:22+00:00

    Scott, thanks for the reply,

    I tried this yesterday. I was able to populate the combobox with my AssessionNumber but unable to get the record into the form for editing. Any suggestions?

    Kerry

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2014-09-11T13:27:56+00:00

    Ken, Thank you for the examples in FindRecord. I am reviewing the VB right now and have a question regarding cboGoToContact_AfterUpdate(). Please bear in mind I am fairly new at this. I am playing with the form and then reviewing the VB. How do I get the MessageText "No Matching Record" and "Warning" to fire? Can't seem to see that message with data input.

    Also, Is it possible to leave the form blank until a Go To Contact has been selected. That way for my situation the users don't accidentally edit a record. Can we add a message for edit/update to ask if user really wants to change the data in a record. If yes then save edited record as update and save modified date/time.

    Thank you,

    Kerry

    Was this answer helpful?

    0 comments No comments
  5. ScottGem 68,840 Reputation points Volunteer Moderator
    2014-09-11T13:08:10+00:00

    The Combobox wizard will create a search combo for you. Its the third option on the wizard.

    Was this answer helpful?

    0 comments No comments