The best way to learn (according to us) is to see a lot of sample code. Below is a collection of VBA code ”snippets” from LIME Pro and our aim is to continue to add snippets to this collection, so come back later and hopefully you will find something new.

Display the user’s name at login

‘ Display the user’s name at login
Private Sub
Application_StartUp(Cancel As Boolean)
MsgBox ”Hi ” & ActiveUser.Name, vbInformation
End Sub

Adding a Command Button

‘ Adding a command button
Dim WithEvents
Button As CommandButtonPrivate Sub Application_StartUp(Cancel As Boolean)
Set Button = CommandButtons.Add(”Click on me”)
End SubPrivate Sub Button_Click()
MsgBox ”Button clicked!!”, vbInformation
End Sub

Adding a Menu Item

‘ Adding a menu item
Dim
WithEvents mnu As MenuItemPrivate Sub Application_StartUp(Cancel As Boolean)
Set mnu = MenuBar.Menu(”Verktyg”).SubMenu.Add(”&Choose me”)
End SubPrivate Sub mnu_Click()
MsgBox ”Hi there!”, vbInformation
End Sub

‘ adds a separator to a menu
Set mnu = MenuBar.Menu(”Verktyg”).SubMenu.Add(”-”)

Handling Tabs

‘ Handling tabs
Sub
Tabs()
Dim exp As ExplorerFor Each exp In Explorers
MsgBox exp.Name, vbInformation
Next expEnd Sub

Sub ActiveTab()
MsgBox ”Active tab is ” & ActiveExplorer.Name, vbInformation
End Sub

Sub ShowCompany()
Set Application.Explorers.ActiveExplorer = Application.Explorers(”Companies”)
End Sub

Filters and Views

‘ Filters and Views
Sub
ShowActiveFilter()
MsgBox ActiveExplorer.ActiveFilter.Name, vbInformation
End SubSub SetFilter()
Set Explorers(”Companies”).ActiveFilter = Explorers(”Companies”).Filters(”Active Retailers”)
End SubSub ShowActiveView()
MsgBox ActiveExplorer.ActiveView.Name, vbInformation
End Sub

Adding a Record

‘ Adding a record
Sub AddCompany()
Dim Records As New Records
Dim Record As RecordRecords.Open Classes(”Company”), EmptySet Record = Records.Add()

Record.Value(”Name”).Value = ”Lundalogik AB”
Record.Value(”Phone”).Value = ”046 – 270 48 00″

Record.Update

End Sub

Update a Record

‘ Update a record
Sub
UpdateRecord()
Dim recs As Records
Dim rec As Record

Set recs = Application.Explorers(”Companies”).Records

For Each rec In recs
If rec.Value(”Name”) = ”Lundalogik AB” Then
rec.Value(”Phone”) = ”046-555 43 21″
End If
Next rec

recs.Update

End Sub
‘ In the sample above we open all records, we could have used a specified Filter to return just a subset of the records like in:
Set recs = Application.Explorers(”Companies”).OpenRecords(”Active Retailers”)

Retrieve and List Linked Objects

‘ Retrieve and list linked objects
Sub ListPersons()
Dim recs As New Records
Dim rec As Recordrecs.Open ActiveExplorer.Class, ActiveExplorer.ActiveItem.ID, ”Person”‘ Display the linked persons as text
MsgBox recs(1).Value(”Person”).Text, vbInformation

For Each rec In recs(1).Value(”Person”).Records
MsgBox rec.Value(”Name”), vbInformation
Next rec

End Sub

Create Excel workbooks with a specified set of companies and mail them to defined receivers

‘ Create Excel workbooks with a specified set of companies and mail them to defined receivers
Sub InformAboutProspects()
Dim Records As New Records
Dim filter As New lde.filter
Dim x As Record
Dim e As New Excel.Application
Dim wb As New Excel.Workbook
Dim ws As New Excel.Worksheet
Dim theRow As Integer
Dim thePath As String
Dim theRetailer(10) As String ‘ <- Change no of recipients here
Dim i As Integer
Const SAVE_PATH = ”C:\TEMP\”theRetailer(1) = ”IXX”
theRetailer(2) = ”lars.andersson@lundalogik.se”
theRetailer(3) = ”Xter”
theRetailer(4) = ”lars.andersson@lundalogik.se”
theRetailer(5) = ”IT-gården”
theRetailer(6) = ”lars.andersson@lundalogik.se”
theRetailer(7) = ”Philips”
theRetailer(8) = ”lars.andersson@lundalogik.se”
theRetailer(9) = ”Cepus”
theRetailer(10) = ”lars.andersson@lundalogik.se”For i = 1 To UBound(theRetailer) / 2

‘ Create a filter where ÅF = theRetailer
filter.AddCondition ”ÅF”, lkOpEqual, theRetailer(i * 2 – 1)

‘ Open these records
Records.Open Database.Classes(”Företag”), filter

If Records.Count > 0 Then

‘ Create a new Workbook and add a new Worksheet
Set wb = e.Workbooks.Add
Set ws = wb.Worksheets(1)

‘ Add titles
ws.Cells(1, 1) = ”Företagsnamn”
ws.Cells(1, 2) = ”Telefon”
ws.Cells(1, 3) = ”Adress”
ws.Cells(1, 4) = ”Köpstatus”

theRow = 2

‘ Add info about every company who has this retailer
For Each x In Records
ws.Cells(theRow, 1) = x.Value(”Företagsnamn”)
ws.Cells(theRow, 2) = x.Value(”Telefon”)
ws.Cells(theRow, 3) = Replace(x.Value(”Adress”), Chr(10), ”, ”) ‘ Replace LF with (,)
ws.Cells(theRow, 4) = x.Value(”Köpstatus”)
theRow = theRow + 1
Next x

‘ Save file as name + date/time
wb.SaveAs FileName:=SAVE_PATH & theRetailer(i * 2 – 1) & Format(Now, ”YYMMDDHHMMSS”) & ”.XLS”

‘ Send as email
wb.SendMail theRetailer(i * 2), ”Prospects from Lundalogik”

‘ Close Excel
e.Quit

‘ Avallokera objekten
Set ws = Nothing
Set wb = Nothing
Set e = Nothing

End If

Next i

MsgBox ”Done!” & vbCrLf & ”Sent ” & i – 1 & ” lists.” & vbCrLf & ”The lists are found in ‘” & SAVE_PATH & ”‘.”

End Sub

Add a Todo to the Selected Company

‘ Add a todo to the selected company
Sub AddActivityToActiveCompany()
Dim companyAs New Record
Dim activityAs Record
company.Open Application.ActiveExplorer.ActiveItem.Class, Application.ActiveExplorer.ActiveItem.ID Set activity = company.Value(”planned_activites”).Records.Add
activity.Value(”type_of_activity”).Value = ”Follow up”
activity.Value(”description”).Value = ”Promised to call back with prices”
activity.Value(”begin_date”).Value = DateAdd(”d”, Now(), 7)
activity.Value(”due_date”).Value = DateAdd(”d”, Now(), 7)
activity.UpdateEnd Sub