Kamis, 27 April 2017

[MS_AccessPros] DLast : Random or Last?

 

I have the following code attached to a COPY  command button.  The purpose is to copy a parent record from one list box and the related child records from a second list box.  After copying the parent record, I get (I think) the record ID I just created by using the DLast function on the parent table.  I then use that ID field to insert into the newly created child records.

It works as far as I have tested so far.  I am concerned because MSDN and Office.com both say DLast and DFirst return random records.  TechOnTheNet says DLast returns the last record.  For me it appears to be returning the last record so far,  in the limited testing I have done.  But I don't know if I can trust it or not. If DLast and DFirst just return random numbers, why do they exist?  Why not just have DRandom?

Here is what I have written.  If there is a more reliable method I will appreciate your advise.  Comments on any portion of the procedure are appreciated.

Private Sub cmdCopySpec_Click()
    On Error GoTo ErrHandler
    Dim strSQL As String
    Dim lngSpecNameID As Long
    Dim lngNewSpecNameID As Long
   
'call custom function SaveNow() which checks for errors and saves any dirty record if error free
'if there is an error, SaveNow() logs the error message; if error, exit the Sub
    If Not SaveNow() Then Exit Sub
   
'verify that a Spec Name has been selected
    If Me.lstSpecNames.ItemsSelected.Count = 0 Then
        MsgBox "You must select a Specification Name to copy.", vbOKOnly, "No Spec Name Selected"
        Me.lstSpecNames.SetFocus
    Else
        If Me.lstSpecDetails.ListCount = 0 Then
            MsgBox "There are no spec details to copy.", vbRetryCancel, "No Details to Copy"
            Me.lstSpecNames.SetFocus
            Exit Sub
        End If
        MsgBox "Proceeding to copy data now."
       
        'duplicate the SpecName record but with new name
'
            lngSpecNameID = lstSpecNames.Value
            Debug.Print "lngSpecNameID = " & lngSpecNameID
           
            strSQL = "INSERT INTO [tblSpecNames] (SpecName, LocationID, ApplicationGroupID)" & _
            "SELECT  '_copy'&SpecName, LocationID, ApplicationGroupID " & _
            "FROM [tblSpecNames] WHERE SpecNameID = " & lngSpecNameID & ";"
            CurrentDb.Execute strSQL, dbFailOnError
         
            'Get the just created SpecNameID number as a new SpecNameID for use as the foreign key in the new records
            lngNewSpecNameID = DLast("SpecNameID", "tblSpecNames")
            Debug.Print "lngNewSpecNameID = " & lngNewSpecNameID
           
            strSQL = "INSERT INTO [tblSpecDetails] (SpecNameID, ParameterID, LIMSAnalysisID, " & _
                        "TargetValue, LLowLimit, LowLimit, HighLimit, HHighLimit, KPI, Priority)" & _
            "SELECT " & lngNewSpecNameID & " As SpecNameID, ParameterID, LIMSAnalysisID, TargetValue, " & _
                    "LLowLimit, LowLimit, HighLimit, HHighLimit, KPI, Priority " & _
            "FROM [tblSpecDetails] WHERE SpecNameID = " & lngSpecNameID & ";"
            CurrentDb.Execute strSQL, dbFailOnError
           
            Me.lstSpecNames.Requery
            Me.lstSpecDetails.Requery
    End If

Done:
    On Error GoTo 0    'reset error handling to VBA default
    Exit Sub
ErrHandler:
    MsgBox "Error " & Err.Number & ": " & Err.Description & vbNewLine & vbNewLine & _
        "Error is in procedure cmdCopySpec_Click of Form_frmConfigureSpecDetails." & vbNewLine & vbNewLine & _
        "Error was automatically logged for programmer notification." & vbNewLine & vbNewLine & _
        "Try again or click Cancel to close without correction.", vbRetryCancel, gstrAppTitle
    'call the public sub ErrorLog to enter info into ErrorLog table; clears Err Object info with Resume statement
    ErrorLog "Form_frmConfigureSpecDetails_cmdCopySpec_Click", Err.Number, Err.Description
    Resume Done
End Sub


__._,_.___

Posted by: david.pratt@outlook.com
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (1)

Have you tried the highest rated email app?
With 4.5 stars in iTunes, the Yahoo Mail app is the highest rated email app on the market. What are you waiting for? Now you can access all your inboxes (Gmail, Outlook, AOL and more) in one place. Never delete an email again with 1000GB of free cloud storage.


.

__,_._,___

Tidak ada komentar:

Posting Komentar