FredixBlog


Just my log about anything I could find enjoyable.



How can I e-mail a file from Microsoft Access?

author Posted by: fredometro on date Apr 22nd, 2004 | filed Filed under: MS-Access

Hello Anna!

Thanks to ask your question at my website. I am pleased to answer to your question:

How can I e-mail a file from Microsoft Access to other people?

Here is a simple function which sends an email with an attachment.


Public Function MailWithAttachment(Recipient as String, Subject as String, Body as String, Attachment as String)
' Start Outlook.
' If it is already running, you'll use the same instance...
Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")

' Logon. Doesn't hurt if you are already running and logged on...
Dim olNs As Outlook.NameSpace
Set olNs = olApp.GetNamespace("MAPI")
olNs.Logon

' Send a message to your new contact.
Dim olMail As Outlook.MailItem

Set olMail = olApp.CreateItem(olMailItem)
' Fill out & send message...
olMail.To = Recipient
olMail.Subject = Subject
olMail.Body = Body

olMail.Attachments.Add Attachment

olMail.Send
' Clean up...
'MsgBox "All done...", vbMsgBoxSetForeground
olNs.Logoff
Set olNs = Nothing
Set olMail = Nothing
Set olAppt = Nothing
Set olItem = Nothing
Set olApp = Nothing

End Function

Just paste this function in a module. Make sure you have a reference to ‘Microsoft Outlook 9.0 Object library’ checked in the
‘Tools/References’ menu, reachable from the module.

Test the function from the debug window by typing from instance:


?MailWithAttachment("mailbox@domainname.com"," _
   & "YourSubjectTextHere", " _
   & "YourBodyTextHere", " _
   & "c:\\MyFile.txt")

The parameters are easy:
Recipient = email address to sent your mail TO
Subject = Subject of your message
Body = Body text of your message
Attachment = filename for the attachment

Once the function is finished, you should have an email in your Outlook outbox, ready to be sent next time you check your email.

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

Create ODBC DSN

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

Creates a system ODBC DSN thru a program.
As you can see, we create the DSN by making an entry in the registry.
In your Project you need Form1 and a command button Command1. Here’s the code for the General Declarations:


    Option Explicit

    Private Const REG_SZ = 1 'Constant for a string variable type.
    Private Const HKEY_LOCAL_MACHINE = &H80000002

    Private Declare Function RegCreateKey Lib "advapi32.dll" Alias _
       "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, _
       phkResult As Long) As Long

    Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias _
       "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
       ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal _
       cbData As Long) As Long

    Private Declare Function RegCloseKey Lib "advapi32.dll" _
       (ByVal hKey As Long) As Long

Next go to the click event of the Command1 button and enter the following:

Private Sub Command1_Click()

   Dim DataSourceName As String
   Dim DatabaseName As String
   Dim Description As String
   Dim DriverPath As String
   Dim DriverName As String
   Dim LastUser As String
   Dim Regional As String
   Dim Server As String

   Dim lResult As Long
   Dim hKeyHandle As Long

   'Specify the DSN parameters.

   DataSourceName = " "
   DatabaseName = ""
   Description = "<a>"
   DriverPath = " "
   LastUser = ""
   Server = ""
   DriverName = "SQL Server"</a>

   'Create the new DSN key.

   lResult = RegCreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE\ODBC\ODBC.INI\" &amp; _
        DataSourceName, hKeyHandle)

   'Set the values of the new DSN key.

   lResult = RegSetValueEx(hKeyHandle, "Database", 0&, REG_SZ, _
      ByVal DatabaseName, Len(DatabaseName))
   lResult = RegSetValueEx(hKeyHandle, "Description", 0&, REG_SZ, _
      ByVal Description, Len(Description))
   lResult = RegSetValueEx(hKeyHandle, "Driver", 0&, REG_SZ, _
      ByVal DriverPath, Len(DriverPath))
   lResult = RegSetValueEx(hKeyHandle, "LastUser", 0&, REG_SZ, _
      ByVal LastUser, Len(LastUser))
   lResult = RegSetValueEx(hKeyHandle, "Server", 0&, REG_SZ, _
      ByVal Server, Len(Server))

   'Close the new DSN key.

   lResult = RegCloseKey(hKeyHandle)

   'Open ODBC Data Sources key to list the new DSN in the
ODBC Manager.
   'Specify the new value.
   'Close the key.

   lResult = RegCreateKey(HKEY_LOCAL_MACHINE, _
      "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources",
hKeyHandle)
   lResult = RegSetValueEx(hKeyHandle, DataSourceName, 0&,
REG_SZ, _
      ByVal DriverName, Len(DriverName))
   lResult = RegCloseKey(hKeyHandle)

   End Sub


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