O
oldblindpew
I've been trying to create a dialog form for finding a record and displaying
it to a form. I have several forms that can use this same dialog form. It
was all working fine based on the FindRecord method until I got the bright
idea of doing away with my user-defined alphabetical key field in favor of an
autonumber surrogate key.
With help from this forum, I finally got things working again, and without
resorting to hard-coding any object names. Here is the code:
Option Compare Database 'Use database order for string comparisons.
Option Explicit 'Requires variables be declared before use.
Private Sub cmdOkFindFirm_Click()
'Find record for main form based on selection in dialog form.
On Error GoTo Err_OkFindFirm
Dim lngSelect As Long
Dim rst As DAO.Recordset
Dim frm As Form
Dim strArgs As String
'Check to see if no selection was made.
If IsNull(Me!lstFirm) Then
MsgBox "Make a selection or click Cancel", , "No Selection"
GoTo Exit_OkFindFirm
End If
'Store the selection (firm ID number from list box) in a variable.
lngSelect = Me!lstFirm
'Create recordset clone for the main form's recordset.
'Note: Simply closing the dialog form will not make "Me" refer
'back to the main form. "Me" refers only to the form in which
'the code is running (the dialog form).
'When the dialog form was opened, the name of the main form was
'passed to the dialog form as the OpenArgs argument of the
'DoCmd.OpenForm method. That name now goes into a string variable
'(strArgs),
strArgs = Me.OpenArgs
'so the form can be put in a form variable (frm),
Set frm = Forms(strArgs)
'so the recordset clone can be created.
Set rst = frm.RecordsetClone
'All this seems needlessly complicated, but is apparently necessary.
'Find the selected record in the recordset clone.
rst.FindFirst "[aIDFirm] = " & lngSelect
'Display the selected record in the main form.
frm.Bookmark = rst.Bookmark
Exit_OkFindFirm:
On Error Resume Next
DoCmd.Close ObjectType:=acForm, ObjectName:=Me.Name
DoCmd.GoToControl "cmdSearchFirm"
rst.Close
Set rst = Nothing
Exit Sub
Err_OkFindFirm:
MsgBox "Error #: " & Err & Chr(13) & Err.Description
Resume Exit_OkFindFirm
End Sub
My question now is, how do I make this sub available to all my different
forms. I know it has to do with Public vs. Private, but my reference books
don't seem to clearly discuss this major consideration. Thanks.
it to a form. I have several forms that can use this same dialog form. It
was all working fine based on the FindRecord method until I got the bright
idea of doing away with my user-defined alphabetical key field in favor of an
autonumber surrogate key.
With help from this forum, I finally got things working again, and without
resorting to hard-coding any object names. Here is the code:
Option Compare Database 'Use database order for string comparisons.
Option Explicit 'Requires variables be declared before use.
Private Sub cmdOkFindFirm_Click()
'Find record for main form based on selection in dialog form.
On Error GoTo Err_OkFindFirm
Dim lngSelect As Long
Dim rst As DAO.Recordset
Dim frm As Form
Dim strArgs As String
'Check to see if no selection was made.
If IsNull(Me!lstFirm) Then
MsgBox "Make a selection or click Cancel", , "No Selection"
GoTo Exit_OkFindFirm
End If
'Store the selection (firm ID number from list box) in a variable.
lngSelect = Me!lstFirm
'Create recordset clone for the main form's recordset.
'Note: Simply closing the dialog form will not make "Me" refer
'back to the main form. "Me" refers only to the form in which
'the code is running (the dialog form).
'When the dialog form was opened, the name of the main form was
'passed to the dialog form as the OpenArgs argument of the
'DoCmd.OpenForm method. That name now goes into a string variable
'(strArgs),
strArgs = Me.OpenArgs
'so the form can be put in a form variable (frm),
Set frm = Forms(strArgs)
'so the recordset clone can be created.
Set rst = frm.RecordsetClone
'All this seems needlessly complicated, but is apparently necessary.
'Find the selected record in the recordset clone.
rst.FindFirst "[aIDFirm] = " & lngSelect
'Display the selected record in the main form.
frm.Bookmark = rst.Bookmark
Exit_OkFindFirm:
On Error Resume Next
DoCmd.Close ObjectType:=acForm, ObjectName:=Me.Name
DoCmd.GoToControl "cmdSearchFirm"
rst.Close
Set rst = Nothing
Exit Sub
Err_OkFindFirm:
MsgBox "Error #: " & Err & Chr(13) & Err.Description
Resume Exit_OkFindFirm
End Sub
My question now is, how do I make this sub available to all my different
forms. I know it has to do with Public vs. Private, but my reference books
don't seem to clearly discuss this major consideration. Thanks.