FredixBlog


Just my log about anything I could find enjoyable.



Create a linked table with VBA

author Posted by: fredometro on date Feb 1st, 2004 | filed Filed under: MS-Access

Usually we would just need to refresh the existing linked tables, but in some cases, it is necessary to create a linked table from scratch.
This is useful when linking a text file or Excel sheet which name changes over the time.


public sub ReconnectTable(sTable, sConnect)
Dim db as Database
Dim td As TableDef

on error goto Err_Reconnect
   Set db = currentdb()

on error resume next
' Delete the table
   db.TableDefs.Delete sTable
on error goto Err_Reconnect

' Create the table def.
   Set td = db.CreateTableDef(sTable)
' Connect the table
   td.Connect = ";DATABASE=" & sConnect & ";"
' Set the source table name
   td.SourceTableName = sTable
' Add the table def to the tabledefs collection
   db.TableDefs.Append td
' refresh the link
   td.RefreshLink

db.close

exit sub

Err_Reconnect:
   msgbox "Error while reconnecting table " & sTable & ": " & error$

end sub

Running total on a form

author Posted by: fredometro on date Jan 25th, 2004 | filed Filed under: MS-Access

It is sometimes needed to show the running total of the records. On a report, there is no problem as this feature in built-in. But on a form, a bit of coding is needed.
Here is a function that will work on any form. The most of it is that you can sort or filter the records, the running total still works because it works with the form’s recordsetclone.

Parameters of the function:
[Amountfield] is the field name we want to totalize
[Keyfield] is the field name of the primary key of your form’s table or query, or simply an unique field.


Public Function RunTotal(AmountField, KeyField)
Dim SumRs As Recordset
Dim frm As Form
Dim RunningValue As Double
Dim IsCancelled As Boolean

Set frm = Forms(AmountField.Parent.Name)
Set SumRs = frm.RecordsetClone

With SumRs
    ' Setting first amount
    RunningValue = 0
    On Error Resume Next
    .FindFirst "True"
   
    ' Summing all other
    While .Fields(KeyField.Name) <> frm.Controls(KeyField.Name) And Not IsCancelled
        RunningValue = RunningValue + .Fields(AmountField.Name)
        On Error Resume Next
        .FindNext "True"
        If Err <> 0 Then IsCancelled = True
        On Error GoTo 0
    Wend
       
    ' Do the current one
    RunningValue = RunningValue + .Fields(AmountField.Name)
End With

Set SumRs = Nothing
RunTotal = RunningValue
End Function

AddReference()

author Posted by: fredometro on date Jan 1st, 2004 | filed Filed under: MS-Access

This function creates a reference to the specific library.


Function AddReference(chNomFichier As String) As Boolean
Dim ref As Reference

     On Error GoTo Erreur_AddReference
     Set ref = References.AddFromFile(chNomFichier)
     AddReference = True

    Sortie_AddReference:
     Exit Function

    Erreur_AddReference:
     MsgBox Err & ": " & Err.Description
     AddReference = False
     Resume Sortie_AddReference
End Function


Warning: include(/home/www/web421/html/phpTraffic/write_logs.php) [function.include]: failed to open stream: No such file or directory in /home/websitef/public_html/fredometro.com/wp-content/themes/blueshadow/footer.php on line 15

Warning: include() [function.include]: Failed opening '/home/www/web421/html/phpTraffic/write_logs.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/websitef/public_html/fredometro.com/wp-content/themes/blueshadow/footer.php on line 15