Howdy rew2009 . . .
Taking into account the worse secnario (returning to a record after you've closed then reopened the DB), I have to introduce you to [blue]DB Properties![/blue] Be aware ... these properties are [blue]non-volatile[/blue] and stay with the DB even if its closed or transported elsewhere. [blue]The properties can simply be looked at as presistant read/write global variables that never die![/blue]. Normally variable [blue]scope[/blue] has to be taken into account.
You'll be using the [blue]PrimaryKey[/blue] of each subform as a reference to return to the last selected record in that subform. As such you'll be making a [blue]DB Property[/blue] to hold the [blue]PrimaryKey[/blue] of each subform of interest. The properties only have to be made once ... so lets get on with it ...
[ol][li]In the modules window click new.[/li]
[li]Click save and name the module [blue]modDBProps[/blue].[/li]
[li]Copy/paste the following in the module:
Code:
[blue]Public Function CreateDbProp(prpName As String, prpTyp As Long, prpVal) As Boolean
[green]'prpTyp: dbBoolean , dbByte, dbCurrency, dbDate, dbDecimal, dbDouble,
' dbFloat, dbInteger, dbLong, dbMemo, dbSingle, dbText[/green]
On Error GoTo GotErr
Dim prp As Property
Set prp = CurrentDb.CreateProperty(prpName, prpTyp, prpVal)
CurrentDb.Properties.Append prp
CreateDbProp = True
SeeYa:
Set prp = Nothing
Exit Function
GotErr:
Call DbPropErrMsg(prpName)
Resume SeeYa
End Function
Public Function GetDbProp(prpName As String)
On Error GoTo GotErr
GetDbProp = CurrentDb.Properties(prpName)
Exit Function
GotErr:
Call DbPropErrMsg(prpName)
'Returns NUll if property not found.
GetDbProp = Null
End Function
Public Function SetDbProp(prpName As String, ByVal prpVal) As Boolean
'Note: you can't set a db property to Null!
On Error GoTo GotErr
If IsNull(prpVal) Then prpVal = ""
CurrentDb.Properties(prpName) = prpVal
SetDbProp = True
Exit Function
GotErr:
Call DbPropErrMsg(prpName)
End Function
Public Function DelDbProp(prpName As String) As Boolean
On Error GoTo GotErr
CurrentDb.Properties.Delete prpName
DelDbProp = True
Exit Function
GotErr:
Call DbPropErrMsg(prpName)
End Function
Public Sub DbPropErrMsg(prpName As String)
If Err.Number = 3265 Or Err.Number = 3270 Then
Msg = "DB property '" & prpName & "' Not Found!@ @"
Style = vbInformation + vbOKOnly
Title = "Property Not Found Error! . . ."
Call uMsg
ElseIf Err.Number = 3367 Then
Msg = "The db property '" & prpName & "' Already Exist!" & _
"@The creation of this property is CANCELLED! . . .@"
Style = vbInformation + vbOKOnly
Title = "Can't Create . . . Property Exists!"
Call uMsg
ElseIf Err.Number = 3421 Then
Msg = "Can't set the DB property '" & prpName & "' !" & DL & _
"@The value supplied doesn't match the data type of the property!@"
Style = vbInformation + vbOKOnly
Title = "Data Type Conversion Error! . . ."
Call uMsg
Else
Msg = "Error " & Err.Number & ": " & Err.Description & "@ @"
Style = vbCritical + vbOKOnly
Title = "System Error! . . ."
Call uMsg
End If
End Sub[/blue]
[/li]
[li]Save the module.[/li]
[li]Stay in the module and call up the [blue]Immediate Window[/blue] (Ctrl+G). Your going to make the properties here! To do so just enter the [blue]CreateDbProp[/blue] function preceded by a question like so:
Code:
[blue]?CreateDbProp([blue]prpName[/blue], [blue]prpTyp[/blue], [blue]prpVal[/blue])[/blue]
... and fill in the arguements. Note: [blue]prpTyp[/blue] is shown in [green]green[/green] at the top of the function. [red]Do not use any quotations here![/red] Also bear in mind that [blue]prpTyp[/blue] [blue]should follow the type of the primarykey for the subform in question![/blue]
For [blue]prpVal[/blue] use quotes [blue]"[/blue]Your Text[blue]"[/blue] if [blue]prpTyp[/blue] is dbMemo or dbText ... hash marks [blue]#[/blue]date[blue]#[/blue] for dbDate.
As an example: say we have a subform named [blue]subMain1[/blue] with a primarykey type of [blue]Long Integer[/blue]. In the immwdiate window you would have something like:
Code:
[blue]?CreateDbProp("subMain1LV", dbLong, 0)[/blue]
When you hit enter the function returns true if the property was created. Note the LV in the name subMain1[purple]
LV[/purple] simply means [blue]Last Visited[/blue]. This is in the interested of keeping the name short ... which you should be doing thruout the db. Also note that you have to initialize a value for [blue]prpVal[/blue]!
So now you have a db property called [purple]
subMain1LV[/purple]. Continue in the same way for your other subforms.[/li]
[li]From here you simply call the functions [blue]GetDbProp(prpName)[/blue] & [blue]SetDbProp(prpName, prpVal)[/blue] to get and set the properties! Keep the prpType you set in mind when you set the value![/li]
[li]Close the module.[/li][/ol]
Now your ready to try one of the sunforms and its db property. Open the subform in design view and in the [blue]On Unload[/blue] event copy/paste the following ...
Code:
[blue] SetDbProp "[purple][B][I]dbpropertyname[/I][/B][/purple]", Me![purple][B][I]YourPrimarykeyName[/I][/B][/purple][/blue]
... and the [blue]On Load[/blue] event:
Code:
[blue] Me.Recordset.FindFirst "[[purple][B][I]YourPrimarykeyName[/I][/B][/purple]] = " & GetDbProp("[purple][B][I]dbpropertyname[/I][/B][/purple]")[/blue]
Save and perform your testing.
[blue]Your Thoughts? . . .[/blue]
See Ya! . . . . . .
Be sure to see thread181-473997 [blue]Worthy Reading![/blue]
![[thumbsup2] [thumbsup2] [thumbsup2]](/data/assets/smilies/thumbsup2.gif)
Also faq181-2886 [blue]Worthy Reading![/blue]
![[thumbsup2] [thumbsup2] [thumbsup2]](/data/assets/smilies/thumbsup2.gif)