Pages

Saturday, April 21, 2012

Display Image on Mouse Over on Command Button in Access Form

If you want to display image on mouse over on "Command Button "  in access form.


Step 1 . Open the form in design mode


Step 2.  Add a label to it and keep the caption blank( Note Label size should be  bigger than Command button and command button should be added/moved on label . Snapshot below)


Step 3 Add a command button above the label and image which you want to display.

Check the snapshot below in design mode-



Add below code to form on which you have added all the label.button and image.



Option Compare Database

Private Sub Command0_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Image1.Visible = True Then Exit Sub
Image1.Visible = True
End Sub



Private Sub Form_Load()
Image1.Visible = False
End Sub
Private Sub Label4_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Image1.Visible = False
End Sub





Friday, April 20, 2012

Create Dependent Combo Boxes on Access Form

If you want to link or make dependent  two or more combo boxes in user form . For Example -

 In above case when you select any zone . It should show all locations from selected  zone only.

Add below code to the form event


Option Compare Database

' Rep_Name is table name which is having list of all zones and locations

Private Sub Combo0_Change()
' combo6 - location box
' Rep_Name.location - Rep_Name is table and location is field name(column header) having list of locations
Combo6.Value = ""
Combo6.RowSource = "SELECT DISTINCT Rep_Name.location FROM Rep_Name WHERE (((Rep_Name.[zone])='" & Combo0.Value & "'));"
End Sub



Private Sub Form_Load()
' combo0 - zone box
' Rep_Name.zone - Rep_Name is table and zone is field name(column header) having list of zones
Combo0.RowSource = "SELECT DISTINCT Rep_Name.Zone FROM Rep_Name"
End Sub

Private Sub Form_Open(Cancel As Integer)
Combo0.Value = ""
Combo6.Value = ""
End Sub

Download Working File

Saturday, January 21, 2012

Send Christmas , Republic Day, Diwali , New year wishes to your client or staff using Outlook and Access

If you want to send your clients , friends or staff Christmas , Republic Day, Diwali , etc wishes. Snapshot below

Christmas -


Republic Day -



And you have all the employees or client details in a access table (Table name Data).Snapshot




we can easily write a macro to send greetings to staff or client using outlook .

Download Macros

The first thing required is to choose the image from any online photo sharing websites like-
http://www.glitter-graphics.com or http://photobucket.com/ , etc. and pic its code to be used in macro

For example in above cases For Christmas greetings . I have taken image from below link

http://www.glitter-graphics.com/graphics/387296



Before find and replace

<a href="http://www.glitter-graphics.com"><img src="http://dl9.glitter-graphics.net/pub/788/788439qwetsgfyyb.gif" width=573 height=385 border=0></a><br><a href="http://www.glitter-works.org" target=_blank>glitter-graphics.com</a>


Snapshot -





<a href='http://www.glitter-graphics.com'><img src='http://dl9.glitter-graphics.net/pub/788/788439qwetsgfyyb.gif' width=573 height=385 border=0></a><br><a href='http://www.glitter-works.org' target=_blank>glitter-graphics.com</a>



Also we don't require any lines after border=0></a> so we can delete them



<a href='http://www.glitter-graphics.com'><img src='http://dl9.glitter-graphics.net/pub/788/788439qwetsgfyyb.gif' width=573 height=385 border=0></a><br>



Now we will add the image code to vba


Sub christmas_greetings()

' tools ->refrence -> microsoft outlook



'FOR HTML http://www.echoecho.com/htmltext06.htm

'http://www.w3schools.com/html/html_fonts.asp

' image taken from http://www.glitter-graphics.com/graphics/387296

' <br> is used to insert a new line

' to chnage the image code please change in this line

'"<a href='http://www.glitter-graphics.com'><img src='http://dl9.glitter-graphics.net/pub/788/788439qwetsgfyyb.gif' width=573 height=385 border=0></a><br></p>" & _

Dim rs As DAO.Recordset

Dim olApp As Outlook.Application

Dim olMail As MailItem

Set olApp = New Outlook.Application

' open the access table using dao

Set rs = CurrentDb.OpenRecordset("Data", dbOpenDynaset)

' goto first row or recordset

rs.MoveFirst

'run a loop till end of table

Do While Not rs.EOF

Set olMail = olApp.CreateItem(olMailItem)

With olMail

' email id is taken from column b

' rs(1).Value = second column or email id

'rs(0).Value = first column or name

.To = rs(1).Value

.Subject = "Wish you a Merry Christmas!!! "

' NAME OF CLIENT/STAFF from column a

.HTMLBody = "<p><font size='4' face='arial' color='red'><i>Dear " & rs(0).Value & "," & _

" <br></font></p><br><p align='CENTER'><font size='4' face='COMIC SANS' color='Blue'>Wish you a Merry Christmas</p><br><br></font><p align='CENTER'>" & _

"<a href='http://www.glitter-graphics.com'><img src='http://dl9.glitter-graphics.net/pub/788/788439qwetsgfyyb.gif' width=573 height=385 border=0></a><br></p>" & _

"<left>Thanks & Regards <br>Ashish Koul<br>www.excelvbamacros.com/</p>"

.Send

End With

Set olMail = Nothing

rs.MoveNext

Loop

rs.Close

Set olApp = Nothing

End Sub





Now In Case of republic day . I have taken image from

http://media.photobucket.com/image/republic%20day/gopalmurmu/HappyRepublicDay3.jpg?o=6


Before find and replace


<a href="http://photobucket.com/images/republic%20day" target="_blank"><img src="http://i223.photobucket.com/albums/dd30/gopalmurmu/HappyRepublicDay3.jpg" border="0" alt="Happy-Republic-Day Pictures, Images and Photos"/></a>



<a href='http://photobucket.com/images/republic%20day' target='_blank'><img src='http://i223.photobucket.com/albums/dd30/gopalmurmu/HappyRepublicDay3.jpg' border='0' alt='Happy-Republic-Day Pictures, Images and Photos'/></a>


Also we do not require "alt="Happy-Republic-Day Pictures, Images and Photos" so we can delete it


<a href='http://photobucket.com/images/republic%20day' target='_blank'><img src='http://i223.photobucket.com/albums/dd30/gopalmurmu/HappyRepublicDay3.jpg' border='0'/></a>


Now we can add this image code to vba macro



Option Compare Database



Sub republic_day_greetings()

' tools ->refrence -> microsoft outlook

'FOR HTML http://www.echoecho.com/htmltext06.htm

'http://www.w3schools.com/html/html_fonts.asp

' image taken from http://media.photobucket.com/image/republic%20day/gopalmurmu/HappyRepublicDay3.jpg?o=6

'change image code in this line

'"<a href='http://photobucket.com/images/republic%20day' target='_blank'><img src='http://i223.photobucket.com/albums/dd30/gopalmurmu/HappyRepublicDay3.jpg' width=450 height=412 border=0></a><br><br><br></p>" & _

' <br> is used to insert a new line

Dim olApp As Outlook.Application

Dim olMail As MailItem

Set olApp = New Outlook.Application

' open the access table using dao

Set rs = CurrentDb.OpenRecordset("Data", dbOpenDynaset)

' goto first row or recordset

rs.MoveFirst

'run a loop till end of table

Do While Not rs.EOF

Set olMail = olApp.CreateItem(olMailItem)

With olMail

' email id is taken from column b



' rs(1).Value = second column or email id

'rs(0).Value = first column or name

.To = rs(1).Value

.Subject = "Wish you a very Happy Republic Day!!! "

' NAME OF CLIENT/STAFF from column a

.HTMLBody = "<p><font size='4' face='arial' color='red'><i>Dear " & rs(0).Value & "," & _

" <br></font></p><br><p align='CENTER'><font size='4' face='COMIC SANS' color='Blue'>Wish you a Happy Republic Day</p><br><br></font><p align='CENTER'>" & _

"<a href='http://photobucket.com/images/republic%20day' target='_blank'><img src='http://i223.photobucket.com/albums/dd30/gopalmurmu/HappyRepublicDay3.jpg' width=450 height=412 border=0></a><br><br><br></p>" & _

"<left>Thanks & Regards <br>Ashish Koul<br>www.excelvbamacros.com/</p>"

.Send

End With

Set olMail = Nothing

rs.MoveNext

Loop

rs.Close

Set olApp = Nothing

End Sub








Download Macros

Saturday, January 14, 2012

Export Data from Access Tables to Tables Existing in PowerPoint Presentation

If you want to copy the value from access to table existing in a PowerPoint Presentation the first thing which we have to do find out -

1 Slide Number

2. Table no ( on that particular slide) like its table 1 or 2, etc on slide no 1 , or 2 ,etc

3. Row No.

4. Col No.

Download Presentation
Access Database


Here is the code-


Sub export_data_from_access_to_powerpt()
' TO FIND OUT TABLE NO , ROW NO, COL NO, ETC
' tools -> refrence select -> Microsoft powerpoint
Dim PPApp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPSlide As PowerPoint.Slide
Dim oPS As PowerPoint.Slide
Dim Shp As Object
Dim i, j As Integer
Set PPApp = New PowerPoint.Application
PPApp.Visible = True
' open the sample ppt in which u have already made tables ,etc.
Set PPPres = PPApp.Presentations.Open("C:\Documents and Settings\user\Desktop\acess tutorials\Export Access To Powerpoint.ppt")
For Each oPS In PPPres.Slides
w = 1
' go through all the tables in each slides and find the table no , row no , col no
For Each Shp In oPS.Shapes
' check if the shape is table or not
If Shp.HasTable Then
For i = 1 To Shp.Table.Rows.Count
For j = 1 To Shp.Table.Columns.Count

' "Table " TABLE NO ON THAT SLIDE
' ROW - ROW NO OF CELL

' COL - COLUMN NO OF CELL
Shp.Table.Cell(i, j).Shape.TextFrame.TextRange.Text = "Table " & w & vbCrLf & "Row " & i & vbCrLf & "Col " & j
Next
Next
w = w + 1
End If
Next
Next
End Sub


Once you know the table no , row no , and col no

I have three tables in access database "north","south","west" and I want to export the data from each table to the table already existing in PowerPoint slides.

Access Database

Here is the code-

Option Compare Database

Sub export_data_from_access_to_powerpt_tables()

' tools -> refrence select -> Microsoft powerpoint
Dim RS As DAO.Recordset
Dim PPApp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPSlide As PowerPoint.Slide
Dim oPS As PowerPoint.Slide
Set PPApp = New PowerPoint.Application
PPApp.Visible = True
' open the sample ppt in which u have already made tables ,etc.
Set PPPres = PPApp.Presentations.Open("C:\Documents and Settings\user\Desktop\acess tutorials\Export Access To Powerpoint.ppt")

' Slides(2) 2 is slide no
'Shapes(1) 1 is table no on that particluar slide
'Cell(2, 2) 2, 2, is row and column no.
' COPY NORTH TABLE
Set RS = CurrentDb.OpenRecordset("NORTH", dbOpenDynaset)
RS.MoveFirst
Do While Not RS.EOF
For i = 2 To 4
PPPres.Slides(2).Shapes(1).Table.Cell(i, 1).Shape.TextFrame.TextRange.Text = RS.Fields![Rep].Value
PPPres.Slides(2).Shapes(1).Table.Cell(i, 2).Shape.TextFrame.TextRange.Text = RS.Fields![Loc].Value
PPPres.Slides(2).Shapes(1).Table.Cell(i, 3).Shape.TextFrame.TextRange.Text = RS.Fields![Sales].Value
RS.MoveNext
Next
Loop
RS.Close

' COPY SOUTH TABLE
Set RS = CurrentDb.OpenRecordset("SOUTH", dbOpenDynaset)
RS.MoveFirst
Do While Not RS.EOF
For i = 2 To 4
PPPres.Slides(3).Shapes(1).Table.Cell(i, 1).Shape.TextFrame.TextRange.Text = RS.Fields![Rep].Value
PPPres.Slides(3).Shapes(1).Table.Cell(i, 2).Shape.TextFrame.TextRange.Text = RS.Fields![Loc].Value
PPPres.Slides(3).Shapes(1).Table.Cell(i, 3).Shape.TextFrame.TextRange.Text = RS.Fields![Sales].Value
RS.MoveNext
Next
Loop
RS.Close

' COPY WEST TABLE
Set RS = CurrentDb.OpenRecordset("WEST", dbOpenDynaset)
RS.MoveFirst
Do While Not RS.EOF
For i = 2 To 4
PPPres.Slides(4).Shapes(1).Table.Cell(i, 1).Shape.TextFrame.TextRange.Text = RS.Fields![Rep].Value
PPPres.Slides(4).Shapes(1).Table.Cell(i, 2).Shape.TextFrame.TextRange.Text = RS.Fields![Loc].Value
PPPres.Slides(4).Shapes(1).Table.Cell(i, 3).Shape.TextFrame.TextRange.Text = RS.Fields![Sales].Value
RS.MoveNext
Next
Loop
RS.Close
Set PPPres = Nothing
Set PPApp = Nothing
Set RS = Nothing
End Sub

Friday, January 13, 2012

Export data from access table to table existing in word doc or template

If you want to export data from access table to word table , which already exists in the word document. Snapshot below-


Download word Document

Download Access Database

Here is the code-


Sub add_data_word_table()
Dim doc As Word.Application
Dim cll As Word.Cell
Dim i As Long, j As Long
Set doc = New Word.Application
doc.Visible = True
' TOOL -> REFRENCE-> MICROSOFT WORD
doc.Documents.Open "C:\Documents and Settings\user\Desktop\acess tutorials\access to word.docx"
' below code u can use to find out the cell row and col in the table
Set rs = CurrentDb.OpenRecordset("export to word table", dbOpenDynaset)
rs.MoveFirst
Do While Not rs.EOF
For i = 2 To 6
doc.ActiveDocument.Tables(1).Cell(i, 1).Range.Text = rs.Fields![Name].Value
doc.ActiveDocument.Tables(1).Cell(i, 2).Range.Text = rs.Fields![Region].Value
doc.ActiveDocument.Tables(1).Cell(i, 3).Range.Text = rs.Fields![Sales].Value
rs.MoveNext
Next
Loop
rs.Close
'if you want to change the font size of table
doc.ActiveDocument.Tables(1).Range.Font.Size = 12
doc.ActiveDocument.Tables(1).Range.Font.Name = "Arial"
' center align text
doc.ActiveDocument.Tables(1).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
'if we add background color to header
'doc.ActiveDocument.Tables(1).Rows(1).Range.Shading.BackgroundPatternColorIndex = 6

End Sub

Wednesday, January 11, 2012

Save Outlook Attachments On Your Desktop and Import the data from attachments to Access Table

Suppose Every month or week you receive monthly or weekly reports from multiple Reps or Person. and you have to manually check all emails then open the attachments and then copy the data of attachments to single excel sheet .

I have written a small macro which check all the unread emails in inbox(or specific folder) then

look for emails with subject "Sales Report" then download the attachments to the desktop

and then import the attachments(in CSV format) to access table "Data".

Sample File 1
Sample File 2

Download Access Database

Here is the code-

Option Compare Database
Sub save_outlook_attachmnets_to_local_desktop_and_import_files_to_database()
'reference -> microsoft outlook
Dim oitem As Outlook.MailItem
Dim ol As Outlook.Application
Dim olns As Outlook.Namespace
Dim oinbox As Outlook.Folder
Dim att As Outlook.Attachment
Dim rcd As DAO.Recordset
Dim dir_name As String
Dim Filename As String
Set ol = New Outlook.Application
Set olns = ol.GetNamespace("MAPI")
' check the inbox folder
Set oinbox = olns.GetDefaultFolder(olFolderInbox)
'if you want to run loop in a specific folder
' Set oinbox = oinbox.Folders("Ashish")
' we will create a new directory to save the attachments
dir_name = Application.CurrentProject.Path & "\" & Format(Now(), "DD_MMM_YYYY_HH_MM_SS") & "\"
MkDir dir_name
Set rcd = CurrentDb.OpenRecordset("attachments", dbOpenDynaset)
'Do Until oinbox.Items.Restrict("[UnRead] = True").Count = 0

' run loop through each unread email
For Each oitem In oinbox.Items.Restrict("[UnRead] = True")
' check for matching subject lines
If oitem.Subject = "Sales Report" Then
' run loop through each aatachments
For Each att In oitem.Attachments
Filename = ""
With rcd
.AddNew
.Fields![Subject].Value = oitem.Subject
.Fields![Sender EmailAddress].Value = oitem.SenderEmailAddress
.Fields![Received Time].Value = oitem.ReceivedTime
.Fields![Sender Name].Value = oitem.SenderName
.Fields![File Name].Value = att.Filename
' if you know your file names are unique then use this method if you are not sure,
'then we can use below two lines to give unique names to attachments at runtime
' add one more column with orginal attachment name and new attachment name
'Application.Wait (Now + TimeValue("0:00:1"))
'.Fields![Sales].Value = Format(Now(), "dd_mmm_yyyy_hh_mm_ss") & att.Filename
Filename = dir_name & "\" & att.Filename
.Fields![File Path].Value = Filename
.Fields![Saved Date].Value = Now()
att.SaveAsFile Filename

If Right(Filename, 3) = "CSV" Then
Call adddata(Filename)
.Fields![Records Added Date].Value = Now()
End If
.Update
End With

RefreshDatabaseWindow
Next att
' mark emails read
oitem.UnRead = False
End If
'oitem.UnRead = False
Next
'Loop
rcd.Close
MsgBox "Done"
End Sub
Sub adddata(filepath As String)
DoCmd.SetWarnings (False)
Dim sqlqry As String
sqlqry = ""
' data is table name
DoCmd.TransferText acImportDelim, "", "Data", filepath, True
' add 'Data Added Date
sqlqry = "UPDATE Data SET [Data Added Date] =# " & Now() & "# where [Data Added Date] IS nULL"
DoCmd.RunSQL sqlqry
DoCmd.SetWarnings (True)
End Sub

Save Outlook Attachments On Your Desktop

If you want to run a loop through all unread emails in your inbox or any specific folder and then save all attachments from each mail on to your desktop.

The below macro will save all the attachments and also will add the following information in your access table for further use-



Download Access Database

Here is the code-


Option Compare Database
Sub save_outlook_attachmnets_to_local_desktop()
'reference -> microsoft outlook
Dim oitem As Outlook.MailItem
Dim ol As Outlook.Application
Dim olns As Outlook.Namespace
Dim oinbox As Outlook.Folder
Dim att As Outlook.Attachment
Dim rcd As DAO.Recordset
Dim dir_name As String
Dim Filename As String
Set ol = New Outlook.Application
Set olns = ol.GetNamespace("MAPI")
' check the inbox folder
Set oinbox = olns.GetDefaultFolder(olFolderInbox)
'if you want to run loop in a specific folder
' Set oinbox = oinbox.Folders("Ashish")
' we will create a new directory to save the attachments
dir_name = Application.CurrentProject.Path & "\" & Format(Now(), "DD_MMM_YYYY_HH_MM_SS") & "\"
MkDir dir_name
Set rcd = CurrentDb.OpenRecordset("attachments", dbOpenDynaset)
Do Until oinbox.Items.Restrict("[UnRead] = True").Count = 0

' run loop through each unread email
For Each oitem In oinbox.Items.Restrict("[UnRead] = True")
' check for matching subject lines
If oitem.Subject = "Sales Report" Then
' run loop through each aatachments
For Each att In oitem.Attachments
Filename = ""
With rcd
.AddNew
.Fields![Subject].Value = oitem.Subject
.Fields![Sender EmailAddress].Value = oitem.SenderEmailAddress
.Fields![Received Time].Value = oitem.ReceivedTime
.Fields![Sender Name].Value = oitem.SenderName
.Fields![File Name].Value = att.Filename
' if you know your file names are unique then use this method if you are not sure,
'then we can use below two lines to give unique names to attachments at runtime
' add one more column with orginal attachment name and new attachment name
'Application.Wait (Now + TimeValue("0:00:1"))
'.Fields![Sales].Value = Format(Now(), "dd_mmm_yyyy_hh_mm_ss") & att.Filename
Filename = dir_name & "\" & att.Filename
.Fields![File Path].Value = Filename
.Fields![Saved Date].Value = Now()
.Update
End With
att.SaveAsFile Filename
RefreshDatabaseWindow
Next att
' mark emails read
oitem.UnRead = False
End If
Next
Loop
rcd.Close
End Sub

Download Access Database