Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Using a combobox to go to a selected form record

Question posted by: dozingquinn (Newbie) on July 4th, 2008 12:06 PM
Hello,

I have a button [cmdPatientfind] which is beside a combo box [cboPatientfind]. The user selects the patient ID (integer) from the combo box, then presses the button. The user is then taken to another form [frm_Patients] which is filtered to the selected patient ID.

However once they view the filtered record, the user can't scroll forward or backward to review the other records around it. Is there a way to alter the code behind my button to allow this? The code I currently have is below:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdPatientfind_Click()
  2. Dim stLinkCriteria As String
  3.      stLinkCriteria = "tpPatient = " & Me.cboPatientfind
  4.      stDocName = "frm_Patients"
  5.     DoCmd.OpenForm stDocName, , , stLinkCriteria
  6. End Sub


Thanks for your help.
RuralGuy's Avatar
RuralGuy
Member
43 Posts
July 4th, 2008
06:31 PM
#2

Re: Using a combobox to go to a selected form record
Your code uses the WhereCondition of the OpenForm command which simply applies a filter to the form. In order to do as you want there are several approaches. I prefer to pass the data in the OpenArgs argument of the OpenForm command and then move to the right record in the OnOpen event of the next form.

Reply
dozingquinn's Avatar
dozingquinn
Newbie
28 Posts
July 4th, 2008
10:42 PM
#3

Re: Using a combobox to go to a selected form record
Thanks for the tip RuralGuy. As I'm a newbie to all this, would you be able to show me an example of how the code should be?

Thanks again.

Reply
ADezii's Avatar
ADezii
Expert
4,021 Posts
July 5th, 2008
12:08 AM
#4

Re: Using a combobox to go to a selected form record
This is actually a 2 step process:
  1. In the Click() Event of cmdPatientFind, place the following code:
    Expand|Select|Wrap|Line Numbers
    1. Private Sub cmdPatientFind_Click()
    2. Dim strMsg As String
    3.  
    4. strMsg = "You must first select a Patient ID from the Drop Down List!"
    5.  
    6. 'See if a value exists in cboPatientFind
    7. If IsNull(Me![cboPatientFind]) Then     'No value
    8.   MsgBox strMsg, vbExclamation, "No Patient ID to Find"
    9.     'Set Focus to Find Combo Box and Drop it Down
    10.     Me![cboPatientFind].SetFocus
    11.     Me![cboPatientFind].Dropdown
    12. Else    'value exists in cboPatientFind, pass ID in OpenArgs
    13.   DoCmd.OpenForm "frmPatients", acNormal, , , acFormEdit, _
    14.                   acWindowNormal, Me![cboPatientFind]
    15. End If
    16. End Sub
  2. In the Open() Event of frmPatients, place the following code:
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Form_Open(Cancel As Integer)
    2. 'I have no idea of the size of your Database, so what I have given you
    3. 'is the most efficient Find mechanism there is, as far as it relates to
    4. 'your situation
    5. Dim rst As DAO.Recordset
    6.  
    7. Set rst = Me.RecordsetClone     'Clone of the Form's Recordset
    8.  
    9. rst.FindFirst "[PatientID] = " & Me.OpenArgs
    10.  
    11. If rst.NoMatch Then     'Not a Matchamundo!
    12.   MsgBox "No Record(s) found for Patient ID: (" & Me.OpenArgs & ")", _
    13.           vbExclamation, "No Patient ID Found"
    14.   DoCmd.Close acForm, "frmPatients", acSaveNo       'Close frmPatients
    15. Else
    16.   Me.Bookmark = rst.Bookmark
    17. End If
    18. End Sub
  3. Any questions, please feel free to ask

Reply
dozingquinn's Avatar
dozingquinn
Newbie
28 Posts
July 5th, 2008
03:58 AM
#5

Re: Using a combobox to go to a selected form record
Thanks for your detailed response ADezii. Very much appreciated.

I have entered the following code

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdPatientFind_Click()
  2. Dim strMsg As String
  3.  
  4. strMsg = "You must first select a Patient ID from the Drop Down List!"
  5.  
  6. 'See if a value exists in cboPatientFind
  7. If IsNull(Me![cboPatientfind]) Then     'No value
  8.   MsgBox strMsg, vbExclamation, "Please select a patient to view."
  9.     'Set Focus to Find Combo Box and Drop it Down
  10.     Me![cboPatientfind].SetFocus
  11.     Me![cboPatientfind].Dropdown
  12. Else    'value exists in cboPatientFind, pass ID in OpenArgs
  13.   DoCmd.OpenForm "frm_Patients", acNormal, , , acFormEdit,
  14.                   acWindowNormal , Me![cboPatientfind]
  15. End If
  16. End Sub


However Access is giving me a 'compile error: syntax error' for the code on line 13. (the DoCmd bit). I've double checked and [frm_Patients] is the name of the form, so that's not the problem. Could you please take a look for me.

Thanks again.

Reply
RuralGuy's Avatar
RuralGuy
Member
43 Posts
July 5th, 2008
04:11 AM
#6

Re: Using a combobox to go to a selected form record
One too many commas maybe.
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm "frm_Patients", , , , acFormEdit, , Me![cboPatientfind]

Reply
dozingquinn's Avatar
dozingquinn
Newbie
28 Posts
July 5th, 2008
10:44 AM
#7

Re: Using a combobox to go to a selected form record
Thanks RuralGuy. I amended the code to your suggestion and it seems to have fixed the error. Now I'm having difficulty with the Form_open function:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2. 'I have no idea of the size of your Database, so what I have given you
  3. 'is the most efficient Find mechanism there is, as far as it relates to
  4. 'your situation
  5. Dim rst As DAO.Recordset
  6.  
  7. Set rst = Me.RecordsetClone     'Clone of the Form's Recordset
  8.  
  9. rst.FindFirst "[tpPatient] = " & Me.OpenArgs
  10.  
  11. If rst.NoMatch Then     'Not a Matchamundo!
  12.   MsgBox "No Record(s) found for Patient No. (" & Me.OpenArgs & ")", _
  13.           vbExclamation, "No Patient Number Found"
  14.   DoCmd.Close acForm, "frmPatients", acSaveNo       'Close frmPatients
  15. Else
  16.   Me.Bookmark = rst.Bookmark
  17. End If
  18. End Sub


I'm getting the error: "Compile error: User-defined type not defined" relating to line 5 of the code (the rst As DAO.Recordset bit). Any ideas?

Reply
ADezii's Avatar
ADezii
Expert
4,021 Posts
July 5th, 2008
11:05 AM
#8

Re: Using a combobox to go to a selected form record
Quote:
Thanks RuralGuy. I amended the code to your suggestion and it seems to have fixed the error. Now I'm having difficulty with the Form_open function:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2. 'I have no idea of the size of your Database, so what I have given you
  3. 'is the most efficient Find mechanism there is, as far as it relates to
  4. 'your situation
  5. Dim rst As DAO.Recordset
  6.  
  7. Set rst = Me.RecordsetClone     'Clone of the Form's Recordset
  8.  
  9. rst.FindFirst "[tpPatient] = " & Me.OpenArgs
  10.  
  11. If rst.NoMatch Then     'Not a Matchamundo!
  12.   MsgBox "No Record(s) found for Patient No. (" & Me.OpenArgs & ")", _
  13.           vbExclamation, "No Patient Number Found"
  14.   DoCmd.Close acForm, "frmPatients", acSaveNo       'Close frmPatients
  15. Else
  16.   Me.Bookmark = rst.Bookmark
  17. End If
  18. End Sub


I'm getting the error: "Compile error: User-defined type not defined" relating to line 5 of the code (the rst As DAO.Recordset bit). Any ideas?

Make sure you have a Reference set for the Microsoft DAO X.XX Object Library

Reply
dozingquinn's Avatar
dozingquinn
Newbie
28 Posts
July 5th, 2008
01:26 PM
#9

Re: Using a combobox to go to a selected form record
Quote:
Make sure you have a Reference set for the Microsoft DAO X.XX Object Library


Sorry ADezii, but I really don't know what this means, or how to go about setting the reference.

Reply
ADezii's Avatar
ADezii
Expert
4,021 Posts
July 5th, 2008
04:53 PM
#10

Re: Using a combobox to go to a selected form record
Quote:
Sorry ADezii, but I really don't know what this means, or how to go about setting the reference.
  1. Select a Form, Report, or Module in the Database Window.
  2. From the Menu Bar, select View ==> Code.
  3. From the Menu Bar, select Tools ==> References.
  4. The Microsoft DAO X.X Object Library needs to be selected, if not:
    1. Scroll down until you see the Microsoft DAO X.X Object Library in the List Box.
    2. Select this Library by clicking in the Check Box.
      Click OK.
  5. Now, run the code.

Reply
dozingquinn's Avatar
dozingquinn
Newbie
28 Posts
July 6th, 2008
06:12 AM
#11

Re: Using a combobox to go to a selected form record
Excellent. Thanks ADezii and RuralGuy - the solution now works, as requested.

However it's had a minor flow on affect to another button on the same form. The other (main) button on the form is used to simply open the form [frm_patients] at its first record. However, now that we've altered the form_open code to frm_patients, I'm getting the error:

Runtime error 3077: "Syntax error (missing operator) in expression"

It then shows the code

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2.  
  3. Dim rst As DAO.Recordset
  4.  
  5. Set rst = Me.RecordsetClone     'Clone of the Form's Recordset
  6.  
  7. rst.FindFirst "[tpPatient] = " & Me.OpenArgs
  8.  
  9. If rst.NoMatch Then     'Not a Matchamundo!
  10.   MsgBox "No Record(s) found for Patient No. (" & Me.OpenArgs & ")", _
  11.           vbExclamation, "No Patient Number Found"
  12.   DoCmd.Close acForm, "frmPatients", acSaveNo       'Close frmPatients
  13. Else
  14.   Me.Bookmark = rst.Bookmark
  15. End If
  16. End Sub


..and highlights line 7. Is there a simple way to alter this code, so that when I push the separate button [cmdPatient], the form [frm_Patients] is opened to its first record?

Reply
ADezii's Avatar
ADezii
Expert
4,021 Posts
July 6th, 2008
12:05 PM
#12

Re: Using a combobox to go to a selected form record
Quote:
Excellent. Thanks ADezii and RuralGuy - the solution now works, as requested.

However it's had a minor flow on affect to another button on the same form. The other (main) button on the form is used to simply open the form [frm_patients] at its first record. However, now that we've altered the form_open code to frm_patients, I'm getting the error:

Runtime error 3077: "Syntax error (missing operator) in expression"

It then shows the code

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2.  
  3. Dim rst As DAO.Recordset
  4.  
  5. Set rst = Me.RecordsetClone     'Clone of the Form's Recordset
  6.  
  7. rst.FindFirst "[tpPatient] = " & Me.OpenArgs
  8.  
  9. If rst.NoMatch Then     'Not a Matchamundo!
  10.   MsgBox "No Record(s) found for Patient No. (" & Me.OpenArgs & ")", _
  11.           vbExclamation, "No Patient Number Found"
  12.   DoCmd.Close acForm, "frmPatients", acSaveNo       'Close frmPatients
  13. Else
  14.   Me.Bookmark = rst.Bookmark
  15. End If
  16. End Sub


..and highlights line 7. Is there a simple way to alter this code, so that when I push the separate button [cmdPatient], the form [frm_Patients] is opened to its first record?

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2. 'process the following code, only if a value exists in the
  3. 'OpenArgs Argument, otherwise simply fall through
  4. If Me.OpenArgs <> "" Then          'ADDED 7/6/2008
  5.   Dim rst As DAO.Recordset
  6.  
  7.   Set rst = Me.RecordsetClone     'Clone of the Form's Recordset
  8.  
  9.   rst.FindFirst "[tpPatient] = " & Me.OpenArgs
  10.  
  11.   If rst.NoMatch Then     'Not a Matchamundo!
  12.     MsgBox "No Record(s) found for Patient No. (" & Me.OpenArgs & ")", _
  13.             vbExclamation, "No Patient Number Found"
  14.     DoCmd.Close acForm, "frmPatients", acSaveNo       'Close frmPatients
  15.   Else
  16.     Me.Bookmark = rst.Bookmark
  17.   End If
  18. End If          'ADDED 7/6/2008
  19. End Sub

Reply
dozingquinn's Avatar
dozingquinn
Newbie
28 Posts
July 11th, 2008
01:57 PM
#13

Re: Using a combobox to go to a selected form record
Apologies for the late response to your solution ADezii. I have just tested it and it works perfectly. Thank you for helping me out with your thorough explanations. You are a true asset to the bytes community.

Quinn

Reply
ADezii's Avatar
ADezii
Expert
4,021 Posts
July 11th, 2008
11:53 PM
#14

Re: Using a combobox to go to a selected form record
Quote:
Apologies for the late response to your solution ADezii. I have just tested it and it works perfectly. Thank you for helping me out with your thorough explanations. You are a true asset to the bytes community.

Quinn

Thank you for your kind Reply.

Reply
Reply
Not the answer you were looking for? Post your question . . .
189,084 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Microsoft Access / VBA Forum Contributors