Can someone explain this???

Greyowlsl

Mlak Mlak
Local time
Today, 14:14
Joined
Oct 4, 2006
Messages
204
Below is some code, which i can't understand what its doing, or what some of the commands mean such as rc.fields(10) etc. I just need someone to shine some light on what the process is here.

Below is the form current code for a Purchase Orders Form (this form also contains a subform called purchase_order_detail which is viewed as a datasheet):

Code:
Private Sub Form_Current()
    Dim rc As Recordset
    Dim recPOTab As Recordset
    Dim tot As Double
    Dim test As String
     ''**********************************************************************
    ''Open  Purchase_Order_Detail RecordSet
    Call libLatronics.dbOpenRecordSet("Purchase_Order_Detail", rc)
    While Not rc.EOF
        If rc.Fields(0) = Me.[Purchase Order Number] Then
            tot = tot + (rc.Fields(3) * rc.Fields(4))
        End If
        rc.MoveNext
    Wend
    ''Initialise variables
        Me.[Total] = tot
        Me.[Freight Cost] = 0
        Me.[Freight GST Cost] = 0
        Me.[Total Lines GST] = 0
    
    ''**********************************************************************
    ''Open  Purchase_Order Table
    Call libLatronics.dbOpenRecordSet("Purchase_Order", recPOTab)
    ''Search Database for required record.
    Me.Purchase_Order_Numbe.SetFocus
    If Me.Purchase_Order_Numbe.Text = "(AutoNumber)" Then Exit Sub
    recPOTab.FindFirst "[Purchase Order Number] = " & [Purchase Order Number]
    If recPOTab.NoMatch = False Then
        If [Currency] = "USD" Then
            Me.lblFrieghtGST.Visible = False
            Me.cboFreightGSTValue.Visible = False
           ''No Gst included at all
           Else
            Me.lblFrieghtGST.Visible = True
            Me.cboFreightGSTValue.Visible = True
            If [Freight GST Type] = "GST" Then
                [Freight GST Cost] = [Freight Cost] * 0.1
            ElseIf [Freight GST Type] = "No GST" Then
                [Freight GST Cost] = 0
            Else
                ''If we are dealing with a record that does not have these parameters set, then set
                Me.txtfreightcost.SetFocus
                Me.txtfreightcost.Text = "0.00"
                [Freight GST Type] = "No GST"
            End If
            [Total Lines GST] = tot * 0.1
        End If
    End If
 Me.POlookup.SetFocus
End Sub

And this is the module (libLatronics):

Code:
Public Sub dbOpenRecordSet(ByVal tablename As String, ByRef rc As Recordset)
On Error GoTo Error_dbOpenRecordSet
    Dim db As Database
    Set db = CurrentDb()
    Set rc = db.OpenRecordset(tablename, dbOpenDynaset)
Exit_dbOpenRecordSet:
    Exit Sub
Error_dbOpenRecordSet:
    MsgBox Err.DESCRIPTION, vbInformation, "Latronics Says:"
    Resume Exit_dbOpenRecordSet
End Sub

Public Function DoesTableExist(ByVal szTableName As String) As Boolean
    ' Returns True if table exists
    Dim rcd As DAO.Recordset
   ' Try to open the table to see if it exists
    On Error Resume Next
    Set rcd = CurrentDb.OpenRecordset(szTableName)
    ' If there's no error, ie. table exists, returns True
    If Err = 0 Then
        DoesTableExist = True
    Else
        DoesTableExist = False
    End If
    Set rcd = Nothing
End Function

Public Function vbGetLibControl(ByVal strFunction As String) As String
On Error GoTo Error_vbOpenLibControl
    Dim tblLibCTRL As Recordset
    ''Initialise return value
    vbGetLibControl = ""
    'Read Latronics Control Table to Obtain Value of function
    Call libLatronics.dbOpenRecordSet("LatronicsControl", tblLibCTRL)
    tblLibCTRL.FindFirst "[Function] = '" & strFunction & "'"
    If Not tblLibCTRL.NoMatch Then
        ''check for null
        If Not IsNull(tblLibCTRL.Fields![COntrolField].Value) Then
            vbGetLibControl = tblLibCTRL.Fields![COntrolField].Value
        End If
    End If
    tblLibCTRL.Close
Exit_vbOpenLibControl:
    Exit Function
Error_vbOpenLibControl:
    MsgBox Err.DESCRIPTION, vbInformation, "Latronics Says:"
    Resume Exit_vbOpenLibControl
End Function

Public Function vbDeleteAndOpenTable(ByVal sTblName As String, ByRef recTable As Recordset) As Boolean
On Error GoTo Error_vbDeleteAndOpenTable

    vbDeleteAndOpenTable = True
    If sTblName = "" Then
        MsgBox "No Table name given to inputted to function 'vbDeleteAndOpenTable'", vbCritical, "Cannot Open Table"
        vbDeleteAndOpenTable = False
        Exit Function
    End If
     ''Delete all records from current table
    CurrentDb.Execute " DELETE * FROM [" & sTblName & "];" ''Delete all
    ''Open Table to save results to
    Call libLatronics.dbOpenRecordSet(sTblName, recTable)
    
Exit_vbDeleteAndOpenTable:
    Exit Function
Error_vbDeleteAndOpenTable:
    vbDeleteAndOpenTable = False
    Call MsgBox("Error In Module 'vbDeleteAndOpenTable'" & vbCrLf & vbCrLf & _
       "With the following Error: " & Err.DESCRIPTION, vbExclamation, _
           "Error Opening table: '" & sTblName & "'")
    Resume Exit_vbDeleteAndOpenTable
End Function

Thanks for your time.
 
There are several ways to refer to a field in a recordset.

rs!fieldname
rs("fieldname")
rs.Fields("fieldname")
rs.Fields(index) ' The index can be either the name of the field or its integer position in the fields collection.

It will probably all make sense once you realise this.
 
Last edited:
Great, yes it does now.
Thanks Galaxiom
 

Users who are viewing this thread

Back
Top Bottom